Sunday, 16 December 2012

Converting datetime to YYYYMM format


I was working To select/convert only YYYYMM from date in SQL .

after a lot of search i could not find the complete answer on how to convert DateTime to YYYYMM format .

in sql we havrious datetime formats avaible but which format to choose.
Sometimes when working it takes a lot of time to find out which number refers to which datetime format in sql.

Below are some of the formats which are available in SQL :

1) YYYYMMDD
2)DDMMYYYY
3)MMDDYYYY
4)DDYYYY

so same we can have all the possible combination of date time in SQL.

My requiremnet was to get the date using getdate() and the ouptut should be in YYYYMM format in SQL :

so uused the below datetime format in Sql to get the desired output i.e. datetime in YYYYMM format :
 
tyr this simple statement in Sql:

YYYYMM  = Covert(varchar(6), getdate(),112)

No comments:

Post a Comment