String conversion to DATETIME in SQL Server - sql-server

I need to convert a string to datetime. I need to store datetime with milliseconds in SQL Server 2005.
example:
SELECT CAST('2010-07-28 20:07:25.733000000' AS DATETIME)
when I try I am getting error like
Conversion failed when converting datetime from character string

You'll need to truncate.
SELECT CONVERT(DATETIME, CONVERT(CHAR(23), '2010-07-28 20:07:25.733000000'));

(With rounding) the milisecond range is 0-999 in a DATETIME, for more precision use DATETIME2 if your using SQL2K8.

If you remove the last 0:s in the miliseconds this will work:
SELECT CAST('2010-07-28 20:07:25:733' AS DATETIME)

Related

When converting string to datetime the milliseconds precision is changing

I'm trying to convert a string to datetime like this select CONVERT(datetime, '31-05-2022 04:00:00.105', 105) but the precision of the milliseconds changes. How is it possible ?
In my case it gives me 2022-05-31 04:00:00.107
Thanks for your help.
Datetime is only accurate to 3.33 milliseconds. If you try to wedge a value in that is more precise than that, MS SQL will round to the near acceptable value. More information is available here:
Milliseconds in my DateTime changes when stored in SQL Server
You could try the 'datetime2' (or 'datetimeoffset') column type to account for the greater precision required
select CONVERT(datetime2, '31-05-2022 04:00:00.105', 105)

SQL Server: convert StartDATE 20140804 Nvarchar to Datetime2

I'm using SQL Server 2008 and I did an import from a flat file. I couldn't import the datetime column properly so I specified it temporarily as a nvarchar(50).
Now I want to convert it to datetime2 format. However when doing so, I get the error
Conversion failed when converting date and/or time from character string.
The data that is currently in my nvarchar(50) column looks like this:
20140804
And I need to convert it to:
2014-08-04 00:00:00.0000000.
Note that I do not only want to convert one date, but all StartDATE values in my table and insert them to another table
Any help is appreciated.
Insert into targettab(datecol,<othercols....>)
select cast(datefield as datetime2),<othercols...> from sourcetab
You can use cast function
you need to convert to char first because converting to int adds those days to 1900-01-01
select CONVERT (datetime,convert(char(8),rnwl_efctv_dt ))
here are some examples
select CONVERT (datetime,5)
1900-01-06 00:00:00.000
select CONVERT (datetime,20100101)
blows up, because you can't add 20100101 days to 1900-01-01..you go above the limit
convert to char first
declare #i int
select #i = 20100101
select CONVERT (datetime,convert(char(8),#i))
SELECT convert(varchar, StartDATE , 113) from ur table

How can I convert a JSON date with timezone to a SQL Server datetime?

I saved the output of a JSON object in a CSV file. I want to import the data into SQL Server. I have tried to cast the JSON date column to a SQL Server datetime data type.
select cast('2009-06-18T16:44:20+0000' as datetime)
This causes an error to be raised: Conversion failed when converting date and/or time from character string.
How can I convert a JSON date with timezone to a SQL Server datetime?
As far as I know, SQL Server recognizes timezone with colon. You have to reformat timezone part as follows:
SELECT CONVERT(datetime2, STUFF('2009-06-18T16:44:20+0000', 23, 0, ':'))
According to MSDN ISO 8601 has YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm] format.
I had a similar issue; I was getting the same error though the date format for my dates are a little different. The solution for me was to convert it to a DATETIME2 value, instead of just DATETIME. Had I not seen Pawel's answer, I would not have tried converting to DATETIME2.
The dates that I was getting were from a C# object that was serialized to JSON using JSON.Net. Below is a sample SQL script showing what the datetime values were and how to convert it.
DECLARE #effectiveDateJSON VARCHAR(MAX) =
'{"EffectiveDate":"2017-02-07T00:00:00-06:00", "CreateDate":"2017-02-07T16:32:12.9130892-06:00"}';
SELECT *
FROM OPENJSON(#effectiveDateJSON)
WITH (EffectiveDate DATETIME2 '$.EffectiveDate',
CreateDate DATETIME2 '$.CreateDate'
);
For a timezone aware conversion
declare #offset datetimeoffset = JSON_VALUE('{"d": "2021-01-01T00:00:00+03:00"}', '$.d')
declare #inCurrentTimezone datetime = #offset AT TIME ZONE CURRENT_TIMEZONE_ID()
select #inCurrentTimezone
Docs:AT TIME ZONE, datetimeoffset

SQl query datetime from nvarchar error

In SQL,
declare #date datetime ='Jul15'
select CONVERT(varchar(10),#date,105)
getting error
Conversion failed when converting date and/or time from character
string.
Please help
It is because Jul15 is not a valid SQL Server Date or DateTime value, hence the error when it is trying to convert it to date/datetime to assign it to a datetime variable.
Ideally you should be using ANSI-Standard when working with date or datetime values in SQL Server, ANSI-Standard Date value has the format of YYYYMMDD, therefore your statement should look something like this.....
declare #date datetime ='20150701'
select CONVERT(varchar(10),#date,105)

could not convert varchar to datetime in SQL server

In my database i got problem when converting varchar to datetime due to different format of datetime as per below.
Conversion failed when converting date and/or time from character
string.
I have 2 select clause as per below
Select Getutcdate()
Select top 1 localmachinetime from perfmon
both return following format respectively
2012-05-28 06:54:45.753>
28-05-2012 03:03:07
and when i try to convert it to datetime using CAST then it gives me error in second case as per below.
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
You can use convert with a Date/Time style.
convert(datetime, '28-05-2012 03:03:07', 105)

Resources