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)
 casting month column

Author  Topic 

rama108
Posting Yak Master

115 Posts

Posted - 2012-05-17 : 16:11:39
Hello All,
I have the following query:

I am storing the start month in a variable called @StartMonth. Then I want to cast 'month1' as the month in the @StartMonth

select ISNULL(AVG(month1),0) AS DATENAME(month,@StartMonth)

The date store is 'Jul 1 2008 12:00AM', so you can also try the following, but it complains: "incorrect syntac near 'month'. What is wrong with the following?

select ISNULL(AVG(month1),0) AS DATENAME(month,'Jul 1 2008 12:00AM')

Thank you,
Rama

rama108
Posting Yak Master

115 Posts

Posted - 2012-05-17 : 16:57:40
I figured out the solution. It was very simple but my brain was not working:

Set @Mth1 = DATENAME(month,@StartDate)

and then use @Mth1 in my dynamic query.
Go to Top of Page
   

- Advertisement -