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
 Other SQL Server 2008 Topics
 Converting Datetime

Author  Topic 

viper
Starting Member

6 Posts

Posted - 2009-09-21 : 22:48:25
quote]Hi I have a Query to convert Datetime as follows

  (SELECT     CAST(MONTH([Date]) AS varchar) + '-' + CAST(YEAR([Date]) AS varchar) AS 'MonthYear', SUM(Amount) AS 'Total'

quote:
Question How can I can I convert DateTime to Jan-2009 instead of 1-2009
Thank you[/quote]


VIPER

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-21 : 22:54:30
use datename(month, [Date])


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

viper
Starting Member

6 Posts

Posted - 2009-09-23 : 20:06:38
THANKS KHTAN FOR YOUR REPLY



VIPER
Go to Top of Page

viper
Starting Member

6 Posts

Posted - 2009-09-23 : 22:50:05
quote:


This is my Updated SQL
SELECT MonthYear, SUM(Total) AS Total
FROM (SELECT CAST(DATENAME(M,Date) AS varchar) + '-' + CAST(YEAR([Date]) AS varchar) AS 'MonthYear', SUM(Amount) AS 'Total'
FROM Neon
GROUP BY DATENAME(M,Date)
UNION ALL
SELECT CAST(DATENAME(M,Date) AS varchar) + '-' + CAST(YEAR([Date]) AS varchar) AS 'MonthYear', SUM(Amount) AS 'Total'
FROM InsuranceNeon
GROUP BY DATENAME(M,Date) AS tbTemp
GROUP BY MonthYear
quote:

I get the following error " Incorrect syntax near the keyword 'AS' "

Can anyone Help
Tank you

VIPER
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-24 : 02:55:00
GROUP BY DATENAME(M,Date) AS tbTemp
GROUP BY MonthYear

should be

GROUP BY DATENAME(M,Date) , MonthYear

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

viper
Starting Member

6 Posts

Posted - 2009-09-25 : 18:21:42
THANKS KHTAN

VIPER
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-29 : 02:16:17
quote:
Originally posted by madhivanan

GROUP BY DATENAME(M,Date) AS tbTemp
GROUP BY MonthYear

should be

GROUP BY DATENAME(M,Date) , MonthYear

Madhivanan

Failing to plan is Planning to fail


So, you didn't see my previous reply?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

viper
Starting Member

6 Posts

Posted - 2009-09-29 : 23:10:29
Thanks madhivanan
for your info.

VIPER
Go to Top of Page
   

- Advertisement -