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)
 get max month and year from number

Author  Topic 

scabral7
Yak Posting Veteran

57 Posts

Posted - 2011-03-02 : 14:57:21
Hi,

i have a table that stores month and year in 2 seperate fields as a number, ie month = 12
year = 2010

month = 01
year = 2011

i need to be able to get the max month and year, but if i just say select max(month) i get 12 instead of 01 (01 is greater in this instance becasue of the year portion. Any way to do this using t-sql?

thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-02 : 15:08:03
SELECT TOP 1 [month], [year]
FROM YourTable
ORDER BY [year] DESC, [month] DESC

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

scabral7
Yak Posting Veteran

57 Posts

Posted - 2011-03-02 : 15:14:27
cool,

didn't think about doing that way....

thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-02 : 15:16:45
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -