STRING_AGG replacement in SQL Server 2016 [duplicate] - sql-server

This question already has answers here:
Query to get multiple row into single row
(3 answers)
how to stuff date and time in SQL
(2 answers)
ListAGG in SQLSERVER
(4 answers)
Closed 3 years ago.
I would like to aggregate single column values with an separator in between and with some prefix.
Below is the code tried which works.
SELECT concat('TNB/IAG/',STRING_AGG(WF_ValStr, '/')) AS Result
FROM wfattrdata where wf_id=35262472 and WF_AttrID in (28,29,30,31,33);
I could get the below expected result in latest MSSql versions.
TNB/IAG/1/2/3/4/5
How to replace the above query to make it work in SQL Server 2016?

Check the below STUFF,XML code -
SELECT DISTINCT CONCAT('TNB/IAG/',T1.results) AS EXPECTED_RESULT FROM
(
select REPLACE(STUFF(CAST((
SELECT ' /' +CAST(c.WF_ValStr AS VARCHAR(MAX))
FROM (
SELECT distinct WF_ValStr
FROM wfattrdata
) c
FOR XML PATH(''), TYPE) AS VARCHAR(MAX)), 1, 2, ''),' ','') AS results
from wfattrdata t) T1;

Related

Calculate Date Difference in SQL Server 2019 [duplicate]

This question already has answers here:
Possible to store value of one select column and use it for the next one?
(4 answers)
How to use a calculated column to calculate another column in the same query using a subquery
(2 answers)
SQL Server Reference a Calculated Column
(6 answers)
Using CASE on a Calculated Column in SQL Server 2012
(2 answers)
Closed 6 months ago.
I am trying to calculate the date difference and keep getting a column name error. I am new to SQL and learning from books and YouTube. Any assistance would be appreciated. I commented out the code not working
declare #rpDT datetime
set #rpDT = getdate()
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,isnull([visDischargeDT],#rpDT)as disDT
,datediff(day,[visAdmitDT],disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
SQL Server and most of the RDBMS do not allow to reference column at the same level. The closest equivalent to achieve similar effect is CROSS APPLY:
declare #rpDT datetime = getdate();
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,s.disDT
,datediff(day,[visAdmitDT],s.disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
CROSS APPLY (SELECT COALESCE([visDischargeDT],#rpDT)) AS s(disDT);

How to replace multiple occurrence of comma in a single comma between two substring in SQL Server [duplicate]

This question already has answers here:
Replacing multiple commas with single comma in MS SQL
(8 answers)
Closed 2 years ago.
I have one input string 'abcdefg,,,,,,,ghijk,,,,,lmno'
and we want an output of 'abcdefg,ghijk,lmno' in SQL Server.
Based on this solution you can replace multiple , with a single , like this:
SELECT REPLACE(REPLACE(REPLACE('abcdefg,,,,,,,ghijk,,,,,lmno', ',', '<>'), '><', ''), '<>', ',')
demo on dbfiddle.uk

Query not running in SQL Server [duplicate]

This question already has answers here:
How do I 'subtract' sql tables?
(14 answers)
Closed 4 years ago.
I have this select statement that I am running in SQL Server. But it's throwing an error:
select count(*)
from
(select zip from A
minus
select zip from B)
Error:
Incorrect syntax near select
What is the issue here? I have also tried aliasing the subquery but same error happens.
There is nothing called minus in SQL Server, you need to use except.
Note, except in SQL Server is equivalent to minus of Oracle
Following query will work.
select count(*) ct
from
(
select zip from A
except
select zip from B
)t
Another issue with your code is that you need to give a alias name to the inner table you are creating.

Passing variable in Group By in SQL Server [duplicate]

This question already has answers here:
I need to pass column names using variable in select statement in Store Procedure but i cannot use dynamic query
(3 answers)
Where can parameters be used in SQL statements?
(2 answers)
Using SQL parameters to DROP INDEX in C#
(2 answers)
Closed 5 years ago.
How can I pass variable in Group BY expression.Here is my code.
and from #Varientsku I want to pass column name
Create Proc spIsUnique
#varientsku nvarchar(max) output
As
Begin
select #varientsku,
IIf (count(*)>1,'Dublicate','True') as Total from product group by
#varientsku
End.
and I get this error.
Each GROUP BY expression must contain at least one column that is not an outer reference.
Help me to solve this
You need dynamic sql
EXEC ('select '+#varientsku+',
IIf (count(*)>1,''Duplicate'',''True'') as Total
from product
group by '+#varientsku)
Note : Make sure #varientsku is sanitized, this code is vulnerable to sql injection

How to take out everything before dash(-) in SQL Server? [duplicate]

This question already has answers here:
getting all chars before space in SQL SERVER
(2 answers)
Closed 8 years ago.
I want to extract a certain part of a row. if there is something like this ..." abcd - productA", " zas1234 - productC", how do I extract everything before the dash(-)?
I tried a left(column name,'-') it didnt work. I am on SQL Server 2012.
The column from which I am trying to extract is a varchar column.
It would be great if someone could help me.
Try this,
select SUBSTRING(column_name, 1, CHARINDEX('-', column_name)-1) from table_name;
DECLARE #varStr VARCHAR(MAX)
SET #varStr='abcd - productA'
SELECT CHARINDEX('-',#varStr)
SELECT LEFT(#varStr,CHARINDEX('-',#varStr)-1)

Resources