SQLTeam.com | Weblogs | Forums

C# sharp date string conversion


#1

Please need help in converting the C# date string as shown below. I tried using the below one. But the milli seconds are getting off...

select convert(datetime,left(replace('2015-06-15T13:23:47.377442Z','T',' '),23))

select convert(datetime,left(replace('2015-06-15T13:23:47.372442Z','T',' '),23))


#2

Try one of these (if you are on SQL 2008 or later

DECLARE @s VARCHAR(64) = '2015-06-15T13:23:47.377442Z';
SELECT 
	CAST(@s AS DATETIME2), 
	CONVERT(DATETIME2,@s,127);

#3

What data type should I put in the database.Can I convert it again like below:

CONVERT(DATETIME,CONVERT(DATETIME2,@s,127));


#4

Also I tried another type : 2015-06-10T09:00:00-07:00 and it worked too...Should I use the same for this as well


#5

Data type DATETIME2 has larger date range and larger fractional precision than DATETIME. If you want to preserve all those decimal digits of the seconds, you should use DATETIME2. It also allows you to specify the precision if you choose to.

So you can choose to save it into your database table into a column that is of data type DATETIME2 or cast it to DATETIME and save with the understanding that you will lose some precision