stored procedures - How to display date as column name in a SQL Server table -
i worked on stored procedure in sql server few days ago. created stored procedure code:
alter procedure [dbo].[getemployeehours] (@currentdate datetime) begin declare @startdate datetime declare @enddate datetime set @startdate = dateadd(day, 1 - datepart(weekday, @currentdate), cast(@currentdate date)) set @enddate = dateadd(day, 7 - datepart(weekday, @currentdate), cast(@currentdate date)) --select @startdate --select @enddate select case when grouping(a.firstname +' '+a.lastname)=1 'total day' else a.firstname +' '+a.lastname end [employeename] , sum(case when datename(dw,workdate)='monday' [hours] else 0 end) monday, sum(case when datename(dw,workdate)='tuesday' [hours] else 0 end) tuesday, sum(case when datename(dw,workdate)='wednesday' [hours] else 0 end) wednesday, sum(case when datename(dw,workdate)='thursday' [hours] else 0 end) thursday, sum(case when datename(dw,workdate)='friday' [hours] else 0 end) friday, sum(case when datename(dw,workdate)='saturday' [hours] else 0 end) saturday, sum(case when datename(dw,workdate)='sunday' [hours] else 0 end) sunday, sum(hours) [totalhours] project_timesheet b inner join project_employee on b.employeeid =a.id workdate >= @startdate , workdate<= @enddate group a.firstname +' '+a.lastname rollup end
in stored procedure, getting result of each day , giving name of week column name. first want use pivot found on stack overflow use this. want display date column name in place of week name. can please tell me how can this? try assigning value declare variable not working
thanks
you need use dynamic sql. if examples of dynamic pivot, you'll find code. beware, application consumes results need smart enough discover column name...
Comments
Post a Comment