Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

Help with parsing SQL Server query

$
0
0

Hey all you database experts - I have pretty basic SQL knowledge, can do a few things here and there, but I have an odd question. I have a SQL query that I am trying to create that parses one column into 3 different columns. The data in this column is separated by comma (item1, item2, item3) but I can't for the life of me figure out how to do this. On top of that, I am outputting this data into a text file, hence some of the weird formatting choices in my query. My current code is -

DECLARE @text NVARCHAR(100)
SELECT
'',
EmplName AS "Employee Name",
TicketDate AS "Date",
'',
convert(VARCHAR(max), isnull(Comments, '')) AS "Comments"
FROM TimeTicketDet
WHERE WorkCntr IN ('501') AND convert(DATETIME,TicketDate) between dateadd(dd,-6,getdate()) AND dateadd(dd,-0,getdate())
UNION
SELECT
'',
EmplName,
convert(VARCHAR(8), getdate(),1)+ '  -',
'',
'----------------------------------------------------------------------------------------------------------------------' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
FROM TimeTicketDet
WHERE WorkCntr IN ('501') AND convert(DATETIME,TicketDate) between dateadd(dd,-6,getdate()) AND dateadd(dd,-0,getdate())
GROUP BY EmplName
ORDER BY EmplName, TicketDate;

So I need to parse the "Comments" column into 3 columns, titled Area, Code, and Reason.

Can anyone help?


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>