Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How do I to convert varchar datatype to datetime Expand / Collapse
Author
Message
Posted Sunday, August 05, 2012 1:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, June 11, 2013 1:54 PM
Points: 38, Visits: 220
Does anyone know how to convert varchar datatype to datetime in TSQL?
Post #1340245
Posted Sunday, August 05, 2012 6:59 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, March 27, 2014 12:03 AM
Points: 75, Visits: 437
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
Post #1340263
Posted Sunday, August 05, 2012 7:07 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:28 PM
Points: 1,616, Visits: 16,484
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 !
__________________________________________________________________
Post #1340264
Posted Sunday, August 05, 2012 7:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:36 PM
Points: 5,470, Visits: 23,487
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
Post #1340269
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse