Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Ordering dates

Author  Topic 

shantanu88d
Starting Member

35 Posts

Posted - 2011-04-15 : 07:17:05
Hi, I have a table having column which is going to contain date in format of MM YYYY. How can I order this table on basis of this date
E.g I am getting this table

bill_for bill_paid service_tax
-------------------- -------------------------------- ------------------------------------
Apr 2011 2000.00 20.00
Feb 2011 400.00 4.00
Jan 2011 2000.00 20.00
Mar 2011 6300.00 90.33

I want it to be

bill_for bill_paid service_tax
-------------------- -------------------------------- ------------------------------------
Jan 2011 2000.00 20.00
Feb 2011 400.00 4.00
Mar 2011 2000.00 20.00
Apr 2011 6300.00 90.33

Please HELP !!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-15 : 07:22:19
You can add the order by clause after casting it to datetime as in:
order by cast(bill_for as datetime)
The string 'Apr 2011' will get converted to the date of April 1, 2011.

This assumes that the bill_for column has the 3 character abbreviation for the month part. If it has values that are not well-defined for example, "Sept 2011" then you will need to do more work.
Go to Top of Page

shantanu88d
Starting Member

35 Posts

Posted - 2011-04-15 : 07:38:45
WOW, that was amazing !!! Didnt knw that sql server converts strings like apr 2011 into datetime values ! Thank you so much :)
Go to Top of Page
   

- Advertisement -