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


Datetime format


Datetime format

Author
Message
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5354 Visits: 2767
Hi all,

Declare @date datetime= '18-06-2014 11:38:46.050'
select cast(@date as varchar)
Message: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

Declare @date datetime= '2014-06-18 11:40:04.753'
select cast(@date as varchar)
Output: Jun 18 2014 11:40AM

How can I convert different datetime format to a common datetime format so that it can work for different time formats/

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62960 Visits: 13298
You are trying to store the value 18 in a month, which is not possible of course.
You can either use CONVERT to convert the string to a datetime. You only need to specify the correct format.


DECLARE @date DATETIME= CONVERT(DATETIME,'18-06-2014 11:38:46.050',105); -- apparently the Italian format
SELECT CAST(@date AS VARCHAR);



Or you can use SET DATEFORMAT to override the implicit date formatting.


SET DATEFORMAT dmy;

DECLARE @date DATETIME= '18-06-2014 11:38:46.050';
SELECT CAST(@date AS VARCHAR);




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
GilaMonster
GilaMonster
SSC Guru
SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)

Group: General Forum Members
Points: 226339 Visits: 46325
Also

kapil_kk (6/18/2014)
select cast(@date as varchar)


Don't declare or cast varchars without a length definition. The default will bite you sooner or later.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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