1

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

1
  • In addition to the current answers, it's worth noting that if you are going to continue to convert to datetime rather than datetime2, you ought to replace the space between the date and time with the letter T. E.g. '2010-07-28T20:07:25.733' - otherwise, the conversion may fail if different language settings are in force. Commented Mar 15, 2012 at 14:17

3 Answers 3

3

You'll need to truncate.

SELECT CONVERT(DATETIME, CONVERT(CHAR(23), '2010-07-28 20:07:25.733000000'));
Sign up to request clarification or add additional context in comments.

4 Comments

but i need store complete value.i mean '2010-07-28 20:07:25.733000000'
...and as @Damien_The_Unbeliever points out, you'll want to be sure that your language and dateformat settings are appropriate, or just pass in yyyy-mm-ddThh:mm:ss.mmm in the first place.
Then you are using the wrong data type. If you are using SQL Server 2008, you should use datetime2. If not, then there is no way to store that in SQL Server 2005 or lower using a native datetime type. You could store it as a string but I don't recommend that. If these are always trailing zeros, why do you need to store them? You can always add them at presentation time.
@BhaskaravarmaDandu - A datetime in SQL Server 2005 can only store 3 digits of millisecond information (and the final digit is further restricted), so what you're asking for cannot be accomplished.
2

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

1 Comment

truncate to ensure you don't provide > 3 decimal places as in Aaron's answer, or do you mean store more millisecond precision?
1

If you remove the last 0:s in the miliseconds this will work:

SELECT CAST('2010-07-28 20:07:25:733' AS DATETIME)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.