I do it with FOR XML PATH.
You can use a union (UNION ALL) instead of VALUES; this has the added value that it still works on SQL-Server 2005 (we still have to support it in our company), and you can remove NULL values.
DECLARE @in_SearchTerm1 nvarchar(100)
DECLARE @in_SearchTerm2 nvarchar(100)
DECLARE @in_SearchTerm3 nvarchar(100)
DECLARE @in_SearchTerm4 nvarchar(100)
SET @in_SearchTerm1 = N'a'
SET @in_SearchTerm2 = N''
SET @in_SearchTerm3 = N'c'
SET @in_SearchTerm4 = N''
SELECT
COALESCE
(
STUFF
(
(
SELECT ' / ' + RPT_SearchTerm AS [text()]
FROM
(
SELECT NULLIF(@in_SearchTerm1, N'') AS RPT_SearchTerm, 1 AS RPT_Sort
UNION ALL SELECT NULLIF(@in_SearchTerm2, N'') AS RPT_SearchTerm, 2 AS RPT_Sort
UNION ALL SELECT NULLIF(@in_SearchTerm3, N'') AS RPT_SearchTerm, 3 AS RPT_Sort
UNION ALL SELECT NULLIF(@in_SearchTerm4, N'') AS RPT_SearchTerm, 4 AS RPT_Sort
) AS tempT
WHERE RPT_SearchTerm IS NOT NULL
ORDER BY RPT_Sort
FOR XML PATH(N''), TYPE
).value('.', 'nvarchar(MAX)')
,1
,3
,N''
)
,N''
) AS RPT_SearchTerms
Note the use of nvarchar - stop using varchar already.
You also have to order it, in order to preserve sequence.
So what does this do:
Goal:
Take the 4 search terms inputed in 4 separate filters in a report.
Display these 4 search terms in the report concatenated by ' / '
.
There should not be a ' / / '
if a search term is empty.
It should be displayed in sequence, that is term1/term2/term3/term4, and not e.g. term4/term2/term3/term1.
How to:
So you take the 4 search terms into a union, and add a sort value to preserve order .
You select the search terms and the separator from the union (separatur + null = null)
SELECT ' / ' + RPT_SearchTerm
FROM (UNION OF SEARCH TEMRS) AS tempT
Order it by RPT_Sort
Now select all the values (separater + text) in tempT into one XML file (FOR XML
), where all values are XML-elements with an empty-tagname (PATH(N'')
, and select the values XML-text (AS [text()]
) (aka element.innerXML).
get the result of that as XML-element (TYPE
) and retrieve the innerText string of that XML-element (.value('.', 'nvarchar(MAX)')
) (aka XML-decode).
Finally, remove the leading ' / ' (STUFF(var, 1,3, N'')
)
This is in principle exactly the same as
CONCAT_WS(' / ', @in_SearchTerm1, @in_SearchTerm2, @in_SearchTerm3, @in_SearchTerm4)
Now add nullif,
CONCAT_WS(' / ', NULLIF(@in_SearchTerm1, '') , NULLIF(@in_SearchTerm2, ''), NULLIF(@in_SearchTerm3, ''), NULLIF(@in_SearchTerm4, ''))
and you're there.
This is how you're still able to do CONCAT_WS in SQL-Server ...