Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need to get YYYYMM from date ? Expand / Collapse
Author
Message
Posted Friday, September 14, 2012 8:53 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 5:28 AM
Points: 102, Visits: 1,050
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
Post #1359362
Posted Friday, September 14, 2012 8:57 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:21 PM
Points: 22,506, Visits: 30,219
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);



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)
Post #1359369
Posted Friday, September 14, 2012 8:58 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
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
Post #1359372
Posted Friday, September 14, 2012 9:08 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 5:28 AM
Points: 102, Visits: 1,050
Million times thanks for the help from 'GSquared'.

It got working .!!!!!!!!


Regards,
Kumar
Post #1359389
Posted Friday, September 14, 2012 3:18 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:00 PM
Points: 35,967, Visits: 30,258
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."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1359683
Posted Friday, September 14, 2012 11:23 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, April 14, 2014 3:12 PM
Points: 214, Visits: 929
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.
Post #1359779
Posted Saturday, September 15, 2012 8:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 10, 2013 1:07 PM
Points: 26, Visits: 88
But this one is not working for SELECT CONVERT(CHAR(6), '2012-09-15', 112);

i mean When we provide a hard coded date.


Post #1359809
Posted Saturday, September 15, 2012 1:35 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 11:17 AM
Points: 4,379, Visits: 9,470
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 opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #1359841
Posted Monday, September 17, 2012 6:43 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
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
Post #1360160
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse