Click here to Skip to main content
15,888,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
 Posted an hour ago
i have made sql query for pivot Attendance ,my output is 

5-1-2019	6-1-2019	7-1-2019	8-1-2019
P	A 	P	A
 

But I want this Output 

5	6	7	8
P	A 	P	A


What I have tried:

ALTER Procedure [dbo].[PIvot_Attendance]  
As  
Begin 

SELECT DISTINCT (date) as date INTO #Datessss
FROM EmployeeAtt
ORDER BY date


DECLARE @cols NVARCHAR(4000)
SELECT  @cols = COALESCE(@cols + ',[' + CONVERT(varchar, (date), 106)
				+ ']','[' + CONVERT(varchar, date, 106) + ']')
FROM    #Datessss
ORDER BY date




-- Building the query with dynamic dates
DECLARE @qry NVARCHAR(4000)
SET @qry =
'SELECT * FROM
(SELECT empcode, Day ,(date)
FROM EmployeeAtt)emp
PIVOT (MAX(Day) FOR date IN (' + @cols + ')) AS stat'


-- Executing the query
EXEC(@qry)



End
Posted
Updated 18-Jan-19 2:01am

Try this:

SQL
DECLARE @qry NVARCHAR(4000) = 
N'SELECT * 
FROM (SElECT empcode, DATEPART(DAY, [date]) AS [DayOfMonth], [date]
      FROM EmployeeeAtt) AS emp
PIVOT (MAX(DATEPART(DAY, [date]) FOR [date] IN (' + cols + ')) AS stat'
 
Share this answer
 
Comments
akhter86 18-Jan-19 6:16am    
this error is coming
(6 row(s) affected)
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '('.
MadMyche 18-Jan-19 6:25am    
Then you need to troubleshoot it.
I actually was working on the query you need but with the very poor column and alias names (Day, Date) I didn't bother finishing.
#realJSOP 18-Jan-19 6:32am    
Be a programmer and fix it. I typed it off the top of my head without the benefit of ssms syntax checking.
Member 12314309 18-Jan-19 13:26pm    
Thanks John Simmons ,i have fixed my issued
Maciej Los 18-Jan-19 6:26am    
5ed!
Start by changing your database: don't store date or based items in text based fields, use DATE, DATETIME, or DATETIME2 instead.
The problems with text fields is that they can - and this will at some point - contain non-date information, or date info in a different format from that which you expect. And without the context provided by the user when it was input (which you don't have once it reaches the DB) you can't tell if 01/02/03 is the first of Feb 2003, the second of Jan 2003, or the third of Feb 2001.
When you add in that text based fields are sorted as text, not dates - so the comparison is entirely dependant on the first pair of different characters - it becomes both extremely clumsy and difficult to work with text based fields.
Using Date fields for date info means all that goes away, and you can use the SQL built in functions for data manipulation, such as DATEPART.

Seriously, change your DB now, before it starts to be a lot of work, or worse, bad "real world" data gets into your production DB.
 
Share this answer
 
What you seek is the DatePart function
DATEPART (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
Comments
Maciej Los 18-Jan-19 6:26am    
5ed!

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