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


Need to get YYYYMM from date ?


Need to get YYYYMM from date ?

Author
Message
KumarVelayutham
KumarVelayutham
SSC-Addicted
SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)

Group: General Forum Members
Points: 490 Visits: 1352
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98364 Visits: 38996
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);

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
GSquared
GSquared
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59971 Visits: 9730
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
KumarVelayutham
KumarVelayutham
SSC-Addicted
SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)SSC-Addicted (490 reputation)

Group: General Forum Members
Points: 490 Visits: 1352
Million times thanks for the help from 'GSquared'.

It got working .!!!!!!!!:-)

Regards,
Kumar
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222382 Visits: 42003
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
peacesells
peacesells
SSChasing Mays
SSChasing Mays (646 reputation)SSChasing Mays (646 reputation)SSChasing Mays (646 reputation)SSChasing Mays (646 reputation)SSChasing Mays (646 reputation)SSChasing Mays (646 reputation)SSChasing Mays (646 reputation)SSChasing Mays (646 reputation)

Group: General Forum Members
Points: 646 Visits: 1492
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.
balajisrm
balajisrm
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 93
But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);

i mean When we provide a hard coded date.
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19975 Visits: 10042
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
Problems are opportunities brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs

GSquared
GSquared
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59971 Visits: 9730
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
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