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
 General SQL Server Forums
 New to SQL Server Programming
 Get & combined 2 columns (Mth & Yr)

Author  Topic 

melon.melon
Yak Posting Veteran

76 Posts

Posted - 2009-08-18 : 05:14:00
Hi,
Any idea how to convert a nvarchar Mth to Month so that it can be in calendar month order like this:

This query should get Mth & Yr, combined Mth & Yr as Month_Year which are 2 columns separately and sort according to Year with calendar month:


select distinct Mth,Yr,Yr as Month_Year from t1
order by Month(Mth),Yr ASC


Mth - nvarchar
Yr - int

This error occurred:
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Thank you.

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-08-18 : 05:17:38
if distinct keyword is specified then u can order by only values in select list only........
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2009-08-18 : 05:34:59
select distinct Mth,Yr,Yr as Month_Year,Month(Mth) from t1
order by Month(Mth),Yr ASC

PBUH
Go to Top of Page

melon.melon
Yak Posting Veteran

76 Posts

Posted - 2009-08-18 : 05:41:27
looks complicated..will need to use distinct since there are repetition of Mth & Yr.
This will trigger error - The data types nvarchar and nvarchar are incompatible in the boolean AND operator.

select distinct Mth & convert(nvarchar(4), Yr) as Month_Year from t1


If it cant be done, its fine, wanted to put this in reporting filter.

But how about this can it be converted to a datetime or month so that it can be sorted in Months?

select distinct Month(Mth) as mth from t1 order by Mth


Go to Top of Page
   

- Advertisement -