September 14, 2012 at 8:53 am
Hi Guys,
Please help me to get YYYYMM from Getdate()?
I have tried the below query.But it gives 20129 only not 201209 format?
ANy help will be appreciated!!!!!!!!!
Regards,
Kumar
September 14, 2012 at 8:57 am
KumarSQLDBA (9/14/2012)
Hi Guys,Please help me to get YYYYMM from Getdate()?
I have tried the below query.But it gives 20129 only not 201209 format?
ANy help will be appreciated!!!!!!!!!
SELECT cast(year(getdate()) as char(4)) + right('0' + cast(month(getdate()) as varchar), 2);
September 14, 2012 at 8:58 am
SELECT CONVERT(CHAR(6), GETDATE(), 112);
- 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
September 14, 2012 at 9:08 am
Million times thanks for the help from 'GSquared'.
It got working .!!!!!!!!:-)
Regards,
Kumar
September 14, 2012 at 3:18 pm
KumarSQLDBA (9/14/2012)
Million times thanks for the help from 'GSquared'.It got working .!!!!!!!!:-)
The next question is... do you understand why it works???
--Jeff Moden
Change is inevitable... Change for the better is not.
September 14, 2012 at 11:23 pm
I really appreciate the way how most of the frequent SQL gurus at SQL central try to feed the concept instead of just providing the answers.
September 15, 2012 at 8:59 am
But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);
i mean When we provide a hard coded date.
September 15, 2012 at 1:35 pm
balajisrm (9/15/2012)
But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);i mean When we provide a hard coded date.
Because that is not a hard-coded date. It is a hard-coded string defined as CHAR(10) being converted to another string defined as CHAR(6).
Modify it to this:
SELECT convert(char(6), cast('2012-09-15' As date), 112);
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
September 17, 2012 at 6:43 am
balajisrm (9/15/2012)
But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);i mean When we provide a hard coded date.
As mentioned, you need to convert that string to a date first.
But, even better yet, follow the practice of hard-coding dates following the ISO standard. YYYYMMDD, without hyphens. That avoids issues with localization and conversion.
- 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
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply