Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

The following code is working fine but I am not getting in month order. Can you suggest me any idea?

SQL
SELECT SUM(TaxableAmt) [NSV], (UPPER(CONVERT(VARCHAR,LEFT(DATENAME(MM,invoicedate),3)))+'/'+ CONVERT(VARCHAR,RIGHT(DATENAME(YYYY,invoicedate),2))) AS MONTHYEAR 

FROM SALESDATA where (invoicedate >='4-1-2009' and invoicedate <='4-30-2010')

GROUP BY (UPPER(CONVERT(VARCHAR,LEFT(DATENAME(MM,invoicedate),3)))+'/'+ CONVERT(VARCHAR,RIGHT(DATENAME(YYYY,invoicedate),2)))




Result set is:

NSV MONTHYEAR

45099947.4300001 APR/09

83295380.4299992 NOV/09

95838138.2 AUG/09

74326454.2599992 DEC/09

94144688.5400001 JUL/09

60688678.260001 MAR/10

58451739.9700001 APR/10

87555926.7200027 FEB/10

128036311.36 JAN/10

50949078.1699996 JUN/09

45232741.8099997 MAY/09

72846524.389999 OCT/09

62841500.5999995 SEP/09


Regards,

N.SRIRAM
Posted
Updated 11-Jan-11 0:02am
v3
Comments
Hiren solanki 11-Jan-11 6:06am    
you can always accept answer if it solved.

1 solution

Just put ORDER BY at the end, Like

SQL
Order By MONTH(invoicedate)


That will give you data by sorting month wise regardless of year but if you want to sort month among Year then you can use below Syntax

SQL
Order By YEAR(invoicedate),Month(invoicedate)


Hope it will work for you.
 
Share this answer
 
Comments
Hiren solanki 11-Jan-11 6:06am    
Hi friends I got the answer.

Below is the query.Here I included year and month.

Collapse
SELECT SUM(TaxableAmt) [NSV], (UPPER(CONVERT(VARCHAR,LEFT(DATENAME(MM,invoicedate),3)))+'/'+ CONVERT(VARCHAR,RIGHT(DATENAME(YYYY,invoicedate),2))) AS MONT,YEAR(invoicedate),Month(invoicedate)
FROM SALESDATA where (invoicedate >='4-1-2008' and invoicedate <='4-30-2011')
GROUP BY (UPPER(CONVERT(VARCHAR,LEFT(DATENAME(MM,invoicedate),3)))+'/'+ CONVERT(VARCHAR,RIGHT(DATENAME(YYYY,invoicedate),2))),YEAR(invoicedate),Month(invoicedate)
Order By YEAR(invoicedate),Month(invoicedate)
Hiren solanki 11-Jan-11 6:06am    
This is the same what i suggested, If it helped then don't post your answer just accept it, everybody will know it later on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900