SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do I to convert varchar datatype to datetime


How do I to convert varchar datatype to datetime

Author
Message
leonie6214
leonie6214
SSC Veteran
SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)

Group: General Forum Members
Points: 249 Visits: 229
Does anyone know how to convert varchar datatype to datetime in TSQL?
SomewhereSomehow
SomewhereSomehow
Old Hand
Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)Old Hand (396 reputation)

Group: General Forum Members
Points: 396 Visits: 469
CAST and CONVERT (Transact-SQL)
Is there any problem with that?


I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
Blog: http://somewheresomehow.ru
Twitter: @SomewereSomehow
J Livingston SQL
J Livingston SQL
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12570 Visits: 37673
leonie6214 (8/5/2012)
Does anyone know how to convert varchar datatype to datetime in TSQL?


please give example of what you want to convert

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16259 Visits: 25280
As previously stated you can use CAST or CONVERT. But like many items in T-SQL "It depends". As an example ... be aware of your system date format, and of course the format of the actual data.
DECLARE @T VARCHAR(40)
SET DATEFORMAT dmy
SET @T = '2012-08-05 09:13:50.053'
SELECT CAST(@T AS DATETIME)
SET @T = '20120805 09:13:50.053'
SELECT CAST(@T AS DATETIME)
Results:
2012-05-08 09:13:50.053
2012-08-05 09:13:50.053



As is obvious the results are dramatically different. If you would post a sample of your actual data, and your systems dateformat someone may be able to assist you furter.

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search