Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

i want to get datetime in a common format. if datetime in '05/08/2015 5:37 PM'

or '08/05/2015 5:37 PM'. i am using

select convert(varchar,'05/08/2015 5:37 PM',126 )

but not getting the required result. ;(


thanks...
Posted
Comments
ZurdoDev 12-May-15 8:57am    
What is the required result then?
_Asif_ 12-May-15 9:08am    
What is a common format?

First of all why convert string to string?! You probably meant:
SQL
convert(datetime, '05/08/2015 5:37 PM', 126)

But code 126 expects ISO8601 format...
For your format you may use 101 or 103, depending on wether it is dd/mm or mm/dd...
 
Share this answer
 
Further to Solution 1 you might find the comments on the documentation on Set DateFormat[^] useful to explain what is going on.

Also note the link to set Language[^]

KEP also mentioned ISO 8601 format - here is a link[^] to explain - it's main benefit is that is unambiguous regardless of the culture settings on either PC or Server (which may be different)

Finally - your use of varchar on the convert makes me suspicious that you may be attempting to store dates as varchar on your database. Don't. Always use the most appropriate data type for the data you are trying to store.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900