Can someone please explain me why it returns only two records instead of three? I want the same result when I hard-code the delimiter as well as use the in-built SQL function(s).
SELECT 'HASH(IFNULL(COST_CENTER, '''') || IFNULL(MATCH_DATE, ''''))::bigint'
UNION
SELECT CONCAT('HASH(', CONCAT_WS(' || ', 'IFNULL(COST_CENTER, '''')', 'IFNULL(MATCH_DATE, '''')' ), ')::bigint')
UNION
SELECT CONCAT('HASH(', CONCAT_WS('||', ' IFNULL(COST_CENTER, '''') ', ' IFNULL(MATCH_DATE, '''') ' ), ')::bigint')
;
Your first 2 lines are the same, so when you use a UNION, it dedupes them down to a single record. If you want all three lines, use a UNION ALL instead.
Related
I have a lot of questions from a survey im using for a pivot table. To collect all the questions to my pivot dynamically im using stuff and for xml path. However it seems like question text > 130 in length is not showing.
And i can select all the columns from my cte Questions, so I know the data is there.
UPDATE: If I select my output, my total length is around 8.000 could it be something about the nvarchar(max) not storing more than 8.000 even though it should be able to store around 2gb?
What am I doing wrong?
SELECT QuestionList = cast(STUFF((
SELECT ',' + QUOTENAME(cast(question AS NVARCHAR(max)))
FROM questions
ORDER BY [AgpdbQuestionID]
FOR XML PATH('')
), 1, 1, '') AS NVARCHAR(max))
This is because of QUOTENAME, if input is larger than 128 it returns NULL because it is supposed to handle sysname, not (N)VARCHAR:
"character_string is sysname and is limited to 128 characters. Inputs greater than 128 characters return NULL."
Instead try:
SELECT QuestionList = cast(STUFF((
SELECT ',' + '[' + (cast(question AS NVARCHAR(max)) + ']')
FROM (
VALUES (REPLICATE('a', 130))
)q(question)
FOR XML PATH('')
), 1, 1, '') AS NVARCHAR(max))
Just as another way of achieving this. This method achieves the same without using XML so that you aren't restricted to certain characters. It itterates through your table, building the string with each row, with the last instance being set to your variable #QuestionList.
Declare #QuestionList AS NVARCHAR(max)
SELECT
#QuestionList = isnull(#QuestionList + ', ', '') + question
FROM
questions
ORDER BY
AgpdbQuestionID
It is important to use the isnull, as this achives ommiting the first comma when the existing string is null.
I'd be intregeagued to see how efficient this is compared to the XML method, but this has been useful for myself when I've needed ceratin characters like >, <, " and '
I'm playing how I can insert some spec char in my stored procedure in SQL so my SSRS box will put them in new lines (* please don't confuse with any SSRS formatting/expression, char(10), vbcrlf etc...). Additionally my input comes from xml like in snippet below, I tried all and nothing worked, I also tried to insert $ and later replace in SSRS box and this also strangely didn't work, is it possible to do ideally without going into SSRS formatting ?
Thanks all !
*** =Replace("$",vbcrlf)
; WITH cte AS (SELECT 'Line aaaaaa' Order_Desc UNION Select 'Line 22222' Proc_CODE UNION SELECT 'Line 33333333' Proc_Code )
select
STUFF((SELECT
', ' + ord.Order_Desc As [text()]
--',$' + ord.Order_Desc As [text()]
FROM cte ord
ORDER BY ord.Order_Desc
FOR XML PATH('')
),1,2,'') AS Order_All
DECLARE #LineBreak NVARCHAR(100)
SET #LineBreak = 'First line content.' + CHAR(13)+CHAR(10) + 'Second line
content.'
PRINT #LineBreak
SELECT REPLACE('10,6 7 7,900 11,027,900', ' ', '')
SELECT REPLACE('10,2 27,900 10,6 7 7,900 11,027,900', ' ', '')
Bad Result:
10,677,90011,027,900
10,227,90010,677,90011,027,900
Good Result:
10,677,900 11,027,900
10,227,900 10,677,900 11,027,900
This is an odd requirement. Before this goes downhill, I suggest you normalize your table properly. Anyway, if you're stuck with what you have for now, here is a way to solve your problem.
First, you need a string splitter, to split strings by comma. I use DelimitedSplit8K, written by Jeff Moden and improved by the members of SQL Server Central community.
After splitting the string, check if the value of each item after the space is removed has a length of 3. If yes, concatenate the new string (space removed). Else, concatenate the original item.
WITH Tbl(OriginalString) AS(
SELECT '10,6 7 7,900 11,027,900' UNION ALL
SELECT '10,2 27,900 10,6 7 7,900 11,027,900'
),
TblSplitted(originalString, ItemNumber, Item) AS (
SELECT *
FROM Tbl t
CROSS APPLY dbo.DelimitedSplit8K(t.OriginalString, ',')
)
SELECT *
FROM Tbl t
CROSS APPLY(
SELECT STUFF((
SELECT ',' +
CASE
WHEN LEN(REPLACE(s.Item, ' ', '')) = 3 THEN REPLACE(s.Item, ' ', '')
ELSE s.Item
END
FROM TblSplitted s
WHERE s.originalString = t.OriginalString
ORDER BY s.ItemNumber
FOR XML PATH('')
), 1, 1, '')
) x(NewString);
Try to pull first and last name from a database combine into one value, and then combine the results with a different server database. The problem being my database has first and last separate, the target database has first and last combine in one string. Basically trying to get a list from both databases matching on the full name.
select a.empid,
select (SELECT REPLACE(RTRIM(COALESCE(a.FNAM + ' ', '') +
COALESCE(a.LNAM, '')), ' ', ' '))name1,
a.Email
from [db]..[user].[table] a, [server].[db].[dbo].[tblUsers] t
where name1 = t.Name
Witht he above, it just says invalid column anme1, which makes sense because it is just a result set column name. How can I make this full name value from my DB and then match it with the full name value of column t.Name
select a.empid,
a.FNAM, a.LNAM, t.Name
from [db]..[user].[table] a
join [server].[db].[dbo].[tblUsers] t
on Replace(a.FNAM + a.LNAM, ' ', '')
= Replace(t.Name, ' ', '')
I have a stored procedure that is receiving as a parameter a list of values to include in a query. For example NY, NJ, CT, PA
In my dynamic SQL query in the stored procedure, I want to select records
WHERE state IN ('NY', 'NJ', 'CT', 'PA').
If I use the parameter as it gets to the SP, I would be doing
WHERE state IN 'NY, NJ, CT, PA'.
Therefore, I need to convert
'NY, NJ, CT, PA'
to
('NY','NJ','CT','PA').
Is there a function to do that?
Thanks!
Hey, I think Kevin pointed in the right direction here. All I need to do is:
WHERE state IN REPLACE(('NY, NJ, CT, PA'), ',' , ''',''')
Or, since I really have it in a variable...
WHERE state IN REPLACE ((#StateList), ',' , ''',''')
Here's a fun way to do it without a function:
DECLARE #StateList VARCHAR(50)='NY, NJ, CT, PA' --(substitute for your parameter)
SELECT * FROM Table WHERE ',' + REPLACE(#StateList,' ','') + ',' LIKE '%,' + State + ',%'
There are many ways to do this. One simple way would be to insert them to a temp table (e.g. #temp_states) and do this:
WHERE state in (SELECT state from #temp_states)
Instead of inserting them to a table, you can use a function like this one to generate the table for you:
WHERE state in (SELECT * from dbo.Split(',', 'NY,NJ,CT,PA'))
A very simple option is to do:
WHERE CHARINDEX ( CONCAT (', ', state,',') , CONCAT (', ', #PARAM,',')) > 0