SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Convert Not returning correct date format
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mayoorsubbu
Yak Posting Veteran

India
94 Posts

Posted - 07/14/2010 :  12:20:45  Show Profile  Reply with Quote
Hi Folks,

When I use the following syntax
select  convert(datetime, '01-12-2010',105)


I get

2010-12-01 00:00:00.000

while I want it to return

01-12-2010 00:00:00.000

Any help?

Thanks

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 07/14/2010 :  13:15:33  Show Profile  Reply with Quote
This is a display issue. Thes style 105 applies to convert a datetime to a varchar, not vice versa.

set dateformat dmy
select convert(varchar(20),convert(datetime, '01-12-2010'),105)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

dattatreysindol
Starting Member

India
20 Posts

Posted - 07/14/2010 :  14:27:31  Show Profile  Reply with Quote
Hi There -

Check this article on Books Online.
http://msdn.microsoft.com/en-us/library/ms189491.aspx


Dattatrey Sindol
http://mytechnobook.blogspot.com/

This information is provided "AS IS" with no warranties, and confers no rights.
Go to Top of Page

Kristen
Test

United Kingdom
22415 Posts

Posted - 07/14/2010 :  14:34:49  Show Profile  Reply with Quote
quote:
Originally posted by jimf

Thes style 105 applies to convert a datetime to a varchar, not vice versa


Don't think that's the case Jim.

SELECT CONVERT(datetime, '01/02/03', 1)	-- mm/dd/yy
SELECT CONVERT(datetime, '01/02/03', 3)	-- dd/mm/yy

but DATEFORMAT dmy is probably a better bet - even better, IMHO, is to only ever pass dates as text strings in the format "yyyymmdd" (NO hyphens) or "yyyy-mm-ddThh:mm:ss.sss" (hyphens REQUIRED!) both of which are treated by SQL as being unambiguous WHATEVER the DATEFORMAT setting is

SELECT CONVERT(datetime, '20030201', 1)
SELECT CONVERT(datetime, '20030201', 3)

Edited by - Kristen on 07/14/2010 14:37:25
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 07/14/2010 :  15:27:38  Show Profile  Reply with Quote
Yoicks! I even tested one. I should never volunteer unrequested information, especially when it's wrong! But this breaks
SELECT CONVERT(datetime, '01/02/2003', 1)


which is why I am finally getting in the habit of
"yyyymmdd" (plus you there's less possibility of me typing
'030/1/2003'

Thanks,
Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Kristen
Test

United Kingdom
22415 Posts

Posted - 07/15/2010 :  02:05:47  Show Profile  Reply with Quote
". I should never volunteer unrequested information"

hehehe ... although perhaps the best way to learn. Somewhere in this forum I said exactly the same thing, and Nigel put me straight )(see I still remember that bit even ...)

SELECT CONVERT(datetime, '01/02/2003', 1)

would be wrong because it included a century, therefore you need a "style" of 101, rather than 1.

I only ever use yyyymmdd too now - except perhaps for some sort of data import-cleanup (but then I use DATEFORMAT). Pity Sybase (period to Microsoft) ever added the "Any date format will do, we'll try to convert it, even if its ambiguous"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000