January 7, 2010 at 11:05 am
I am new to SQL.
Can you tell me how I would convert a DateTime field to MMDDYY without any / or .
Example I would need the field to convert to 10409 for January 1, 2009.
Any help would be greatly appreciated.
Thanks
Phillip Gatte
January 7, 2010 at 11:09 am
You could convert using style 10, then wrap that in a replace that gets rid of the punctuation.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 7, 2010 at 11:22 am
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 1), '/', '') AS [MMDDYY]]
you would need to replace GETDATE() function with your date field. This will display as
010710.
Are the leading zeros a problem?
January 7, 2010 at 11:24 am
pgatte (1/7/2010)
I am new to SQL.Can you tell me how I would convert a DateTime field to MMDDYY without any / or .
Example I would need the field to convert to 10409 for January 1, 2009.
Just checking: how do you get 10409 from Jan. 1 2009? (I'm assuming this is a typo.)
Also, don't underestimate fixed characters for dates. 10409 can be interpreted as either Jan. 4, 2009 (1/04/09) or Oct. 4, 2009 (10/4/09). I'd suggest including a leading zero so it reads 010409 (and is consistent with the MMDDYY format). That way, there's no confusion.
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
January 7, 2010 at 11:28 am
Ray K (1/7/2010)
pgatte (1/7/2010)
I am new to SQL.Can you tell me how I would convert a DateTime field to MMDDYY without any / or .
Example I would need the field to convert to 10409 for January 1, 2009.
Just checking: how do you get 10409 from Jan. 1 2009? (I'm assuming this is a typo.)
Also, don't underestimate fixed characters for dates. 10409 can be interpreted as either Jan. 4, 2009 (1/04/09) or Oct. 4, 2009 (10/4/09). I'd suggest including a leading zero so it reads 010409 (and is consistent with the MMDDYY format). That way, there's no confusion.
Even then, if you don't know it is stored as MMDDYY, 010409 could still be confused as Jan 4, 2009 or Apr 1, 2009. Just something to be aware of depending on who will be seeing the data. You may want to be sure this is documented appropriately.
January 7, 2010 at 11:59 am
To even confuse a little more, 010409 might be as well interpreted as April 9th 2001...
I probably would even include the format in the string and truncate it where needed (e.g. 010409MDY). I don't expect it to be used in any calculation...
January 7, 2010 at 11:59 am
Thank you! That worked!
Phillip
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply