Calculated date as column name SQL Server 2008 -


there numerous questions here naming column variable, don't believe duplicate of these, use calculation.

i name column after previous month. expect work:

select isnull(sum(case when datepart(mm,rc.datetime) = datepart(month, getdate()) -1 1 else null end),0) datename(mm, dateadd(mm, -1, getdate())) 

or readability:

... datename(mm, dateadd(mm, -1, getdate())) 

but not, incorrect syntax error. achievable? in advance given.

try dynamic sql,

declare @sql nvarchar(max) = ''  set @sql = 'select isnull(sum(case                  when datepart(mm, rc.datetime) = ' + convert(varchar(10), datepart(month, getdate())) + ' - 1                     1                 else null                 end), 0) ' + datename(mm, dateadd(mm, - 1, getdate()))  exec sp_executesql @sql 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -