Convert SQL Server result set into string - sql

I am getting the result in SQL Server as
SELECT StudentId FROM Student WHERE condition = xyz
I am getting the output like
StudentId
1236
7656
8990
........
The output parameter of the stored procedure is #studentId string and I want the return statement as
1236, 7656, 8990.
How can I convert the output in the single string?
I am returning single column [ie. StudentId]

Test this:
DECLARE #result NVARCHAR(MAX)
SELECT #result = STUFF(
( SELECT ',' + CONVERT(NVARCHAR(20), StudentId)
FROM Student
WHERE condition = abc
FOR xml path('')
)
, 1
, 1
, '')

DECLARE #result varchar(1000)
SELECT #result = ISNULL(#result, '') + StudentId + ',' FROM Student WHERE condition = xyz
select substring(#result, 0, len(#result) - 1) --trim extra "," at end

Use the COALESCE function:
DECLARE #StudentID VARCHAR(1000)
SELECT #StudentID = COALESCE(#StudentID + ',', '') + StudentID
FROM Student
WHERE StudentID IS NOT NULL and Condition='XYZ'
select #StudentID

Both answers are valid, but don't forget to initializate the value of the variable, by default is NULL and with T-SQL:
NULL + "Any text" => NULL
It's a very common mistake, don't forget it!
Also is good idea to use ISNULL function:
SELECT #result = #result + ISNULL(StudentId + ',', '') FROM Student

Use the CONCAT function to avoid conversion errors:
DECLARE #StudentID VARCHAR(1000)
SELECT #StudentID = CONCAT(COALESCE(#StudentID + ',', ''), StudentID)
FROM Student
WHERE StudentID IS NOT NULL and Condition='XYZ'
select #StudentID

The following is a solution for MySQL (not SQL Server), i couldn't easily find a solution to this on stackoverflow for mysql, so i figured maybe this could help someone...
ref: https://forums.mysql.com/read.php?10,285268,285286#msg-285286
original query...
SELECT StudentId FROM Student WHERE condition = xyz
original result set...
StudentId
1236
7656
8990
new query w/ concat...
SELECT group_concat(concat_ws(',', StudentId) separator '; ')
FROM Student
WHERE condition = xyz
concat string result set...
StudentId
1236; 7656; 8990
note: change the 'separator' to whatever you would like
GLHF!

This one works with NULL Values in Table and doesn't require substring operation at the end. COALESCE is not really well working with NULL values in table (if they will be there).
DECLARE #results VARCHAR(1000) = ''
SELECT #results = #results +
ISNULL(CASE WHEN LEN(#results) = 0 THEN '' ELSE ',' END + [StudentId], '')
FROM Student WHERE condition = xyz
select #results

The answer from brad.v is incorrect! It won't give you a concatenated string.
Here's the correct code, almost like brad.v's but with one important change:
DECLARE #results VarChar(1000)
SELECT #results = CASE
WHEN #results IS NULL THEN CONVERT( VarChar(20), [StudentId])
ELSE #results + ', ' + CONVERT( VarChar(20), [StudentId])
END
FROM Student WHERE condition = abc;
See the difference? :) brad.v please fix your answer, I can't do anything to correct it or comment on it 'cause my reputation here is zero. I guess I can remove mine after you fix yours. Thanks!

Use STRING_AGG:
SELECT STRING_AGG(sub.StudentId, ',') FROM
(select * from dbo.Students where Name = 'Test3') as sub
If you want to use e.g ORDER BY:
SELECT STRING_AGG(sub.StudentId, ',') WITHIN GROUP(Order by StudentId) FROM
(select * from dbo.Students where Name = 'Test3') as sub

or a single select statement...
DECLARE #results VarChar(1000)
SELECT #results = CASE
WHEN #results IS NULL THEN CONVERT( VarChar(20), [StudentId])
ELSE ', ' + CONVERT( VarChar(20), [StudentId])
END
FROM Student WHERE condition = abc;

Assign a value when declaring the variable.
DECLARE #result VARCHAR(1000) ='';
SELECT #result = CAST(StudentId AS VARCHAR) + ',' FROM Student WHERE condition = xyz

Related

How to put a comma separated value from a column in a table into SQL IN operator?

I have a table which has a column in which I am storing a comma separated text with single quotes for each of the comma separated values. These values are employee IDs. This is how it looks
Now, I have a SQL query wherein I need to put the value from this column into a SQL IN operator. Something like this:
select *
from EMPLOYEE_MASTER
where EMPLOYEEID IN (select CM_CONFIG_VALUE
from ADL_CONFIG_MAST_T
where CM_CONFIG_KEY like 'ATT_BIOMETRIC_OU_ID'
)
But this, does not work, the query when executed returns 0 rows whereas if I execute the query normally like below, it works.
select *
from EMPLOYEE_MASTER
where EMPLOYEEID IN('9F3DD4B791554DDE','C9B90D62851D43AB','828CB9E6204B4DDC')
Please suggest what I should do here. I have tried using substring to remove the first and the last character as well assuming that single quotes might be the issue, but that does not work either.
select * from EMPLOYEE_MASTER where EMPLOYEEID IN(select EMPLOYEEID from ADL_CONFIG_MAST_T where CM_CONFIG_KEY like 'ATT_BIOMETRIC_OU_ID')
column should be same in where COLUMNNAME IN (select COLUMNNMAE from tablename)
You can create a temp varible and then use exec command to get the desired result.
declare #temp varchar(200)
select #temp=CM_CONFIG_VALUE
from ADL_CONFIG_MAST_T
where CM_CONFIG_KEY like 'ATT_BIOMETRIC_OU_ID'
exec('select *
from EMPLOYEE_MASTER
where EMPLOYEEID IN (' + #temp + ')')
Try This:
DECLARE #ID VARCHAR(500);
DECLARE #Number VARCHAR(500);
DECLARE #comma CHAR;
SET #comma = ','
SET #ID = (select CM_CONFIG_VALUE
from ADL_CONFIG_MAST_T
where CM_CONFIG_KEY like %ATT_BIOMETRIC_OU_ID% + #comma);
Create table #temp (EMPLOYEEID varchar(500))
WHILE CHARINDEX(#comma, #ID) > 0
BEGIN
SET #Number = SUBSTRING(#ID, 0, CHARINDEX(#comma, #ID))
SET #ID = SUBSTRING(#ID, CHARINDEX(#comma, #ID) + 1, LEN(#ID))
Insert into #temp
select #Number
END
select *
from EMPLOYEE_MASTER
where EMPLOYEEID IN(select EMPLOYEEID from #temp)
The reason you are not getting it in your query is because your inner query returns only one row. So your query searches for '9F3DD4B791554DDE','C9B90D62851D43AB','828CB9E6204B4DDC' as as single record.
If your compatibility level is greater than or equal to 130 you can use STRING_SPLIT() function. Then your query would be
SELECT *
FROM EMPLOYEE_MASTER
WHERE EMPLOYEEID IN
(SELECT value AS empid
FROM ADL_CONFIG_MAST_T CROSS APPLY string_split(CM_CONFIG_VALUE, ',' )
WHERE CM_CONFIG_KEY LIKE 'ATT_BIOMETRIC_OU_ID' )
What this actually does is, it splits the CM_CONFIG_VALUE with ',' and returns them as rows. This is the value column I have referred. Then you use them with the IN clause.
Hope this helps!
Direct IN condition will not work here. You have split your string before searching. You can do that with XML options in SQL SERVER 2014
SELECT *
FROM EMP
WHERE EMPID IN (
SELECT a.c.value('.', 'VARCHAR(1000)')
FROM (
SELECT x = CAST('<a>' +
REPLACE(REPLACE(CM_CONFIG_VALUE , ',', '</a><a>'),'''','') + '</a>' AS XML )
FROM ADL_CONFIG_MAST_T
-- WHERE <your_condition>
) m
CROSS APPLY x.nodes('/a') a(c))
CHECK DEMO HERE
For the version 2016 and above you can use STRING_SPLIT with Compatibility level 130

Return comma-separated list from SQL Server

I have a stored procedure as below. Now I want to modify it so that it returns the value as a comma separated list.
CREATE PROCEDURE [dbo].[CUST_Notification_GetCompaniesFromEmails]
#EmailValue AS NTEXT
AS
SELECT DISTINCT
ISNULL(CompanyAddressesContacts.FirstName, '') + ' ' +
ISNULL(CompanyAddressesContacts.LastName, '') AS ContactName
,ISNULL(dbo.Companies.CompanyName,'') AS CompanyName,
CASE
WHEN CompanyName = 'Meraas Development' THEN '1'
WHEN CompanyName = 'Samsung C&T' THEN '2'
ELSE '3'
END AS id
FROM
dbo.CompanyAddressesContacts
INNER JOIN
dbo.Companies ON dbo.CompanyAddressesContacts.CompanyId = Companies.Id
WHERE
dbo.CompanyAddressesContacts.Email IN
(SELECT [value]
FROM dbo.SplitWords(#EmailValue,';'))
The above code returns values as a normal select statement would do . Now I want a modification in the code so that it returns a value as comma-separated list.
Thanks!
Try like this
DECLARE #List VARCHAR(8000)
SELECT #List = COALESCE(#List + ',', '') + CAST(OfferID AS VARCHAR)
FROM Emp
WHERE EmpID = 23
SELECT #List
Try to use SplitWords method as
dbo.SplitWords(#EmailValue +',','')
Have a visit
http://msmvps.com/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx
http://oops-solution.blogspot.in/2011/11/sql-server-convert-table-column-data.html

MSSQL - Concat the result of a query using IN

In a MSSQL table ("username"), I have the two following records:
name ¦ nickname
John Johny
Marc Marco
I'd like to do a query that would return "Johny, Marco" when I as k for the nicknames of John and Marc.
I've tried the following:
declare #consolidatedNicknames varchar(2000)
set #consolidatedNicknames = ''
select #consolidatedNicknames = #consolidatedNicknames + nickname + ';'
From username WHERE name IN ('John','Marc')
but it only returns me the nickname of 'John'.
How could it concat the nickname of 'John' AND 'Marc' ?
Many thanks.
Your sample code worked as expected for me. I've had a few problems with that method with large strings (50k characters or more). Here is a different version that I've found a bit more robust.
DECLARE #consolidatedNicknames varchar(2000)
SET #consolidatedNicknames = ''
SELECT #consolidatedNicknames =
STUFF((SELECT ', ' + username.nickname
FROM username
WHERE name IN ('John','Marc')
FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)')
, 1, 2, '')
STUFF and XML are what you need.
Have a look at this article
Here is a function I have build to do something very similar.
CREATE FUNCTION [dbo].[CapOwnerList]
(
#Seperator nvarchar(100) = ','
)
RETURNS #ConcatValues TABLE
(
ID DECIMAL(28,0) NOT NULL
,VALUE NVARCHAR(MAX)
)
AS
BEGIN
DECLARE #TempTable TABLE (ID INT, VAL VARCHAR(MAX));
INSERT INTO #TempTable (ID,VAL)
SELECT C2O.CAP_ID
,FP.NAME
FROM REL_CAP_HAS_BUS_OWNER C2O
INNER JOIN
FACT_PERSON FP
ON C2O.PERSON_ID = FP.ID
ORDER BY
FP.NAME
;
IF RIGHT(#Seperator,1)<>' '
SET #Seperator = #Seperator+' ';
INSERT #ConcatValues
SELECT DISTINCT
T1.ID
,STUFF((SELECT #Seperator + T2.VAL FROM #TempTable AS T2 WHERE T2.ID = T1.ID FOR XML PATH('')), 1, LEN(#Seperator), '') AS VALS
FROM #TempTable AS T1;
RETURN;
END
GO

Why does SQL Server conversion to bigint cause an error?

I am trying to convert varchar to bigint:
select convert(bigint, (select(Replace((select value from sometable), ' ', ''))))
Why is it giving error???
Error converting data type varchar to bigint.
thanks in advance
Update
This is part of query I am trying:
select top 1 *
into #tblTemp
from testTable
where Status = 0
order by Sno
declare #mobile varchar(50)
set #mobile = (select(Replace((select mobile from #tblTemp), ' ', '')))
--set #mobile = (select( Replace(' 9428017524', ' ', '')))
select convert(bigint, #mobile)
drop table #tblTemp
Try this
select convert(bigint, CASE WHEN ISNUMERIC(#mobile + 'e0') = 0
THEN 0 ELSE #mobile)
-- add this case statement to return only numeric values,
-- your query will fail for values like '123-415-6789', '(123)415-6789'
Check your mobile numbers data and see if there are any unexpected values in that column, you may have to replace '-' or '(' or ')' etc with ''.
SeLECT * FROM #tblTmp
WHERE ISNUMERIC(Replace(mobile, ' ', '') + 'e0') = 0;
I am not sure what your real string is but for safety you can check ISNUMERIC() before convertion.
DECLARE #mobile varchar(50)
SELECT #mobile = REPLACE(mobile, ' ','') --much simplified version
FROM #tblTemp
IF ISNUMERIC(#mobile)
SELECT CONVERT(bigint, #mobile)
ELSE
SELECT 0
Just by reading your queries, you don't need a temp table here at all. Everything can be done in a single query
SELECT TOP (1) CONVERT(bigint, CASE ISNUMERIC( REPLACE(mobile,' ','') )
WHEN 1 THEN REPLACE(mobile,' ','')
ELSE 0 END )
FROM testTable
WHERE Status = 0
ORDER By Sno

How to return multiple values in one column (T-SQL)?

I have a table UserAliases (UserId, Alias) with multiple aliases per user. I need to query it and return all aliases for a given user, the trick is to return them all in one column.
Example:
UserId/Alias
1/MrX
1/MrY
1/MrA
2/Abc
2/Xyz
I want the query result in the following format:
UserId/Alias
1/ MrX, MrY, MrA
2/ Abc, Xyz
Thank you.
I'm using SQL Server 2005.
p.s. actual T-SQL query would be appreciated :)
You can use a function with COALESCE.
CREATE FUNCTION [dbo].[GetAliasesById]
(
#userID int
)
RETURNS varchar(max)
AS
BEGIN
declare #output varchar(max)
select #output = COALESCE(#output + ', ', '') + alias
from UserAliases
where userid = #userID
return #output
END
GO
SELECT UserID, dbo.GetAliasesByID(UserID)
FROM UserAliases
GROUP BY UserID
GO
Well... I see that an answer was already accepted... but I think you should see another solutions anyway:
/* EXAMPLE */
DECLARE #UserAliases TABLE(UserId INT , Alias VARCHAR(10))
INSERT INTO #UserAliases (UserId,Alias) SELECT 1,'MrX'
UNION ALL SELECT 1,'MrY' UNION ALL SELECT 1,'MrA'
UNION ALL SELECT 2,'Abc' UNION ALL SELECT 2,'Xyz'
/* QUERY */
;WITH tmp AS ( SELECT DISTINCT UserId FROM #UserAliases )
SELECT
LEFT(tmp.UserId, 10) +
'/ ' +
STUFF(
( SELECT ', '+Alias
FROM #UserAliases
WHERE UserId = tmp.UserId
FOR XML PATH('')
)
, 1, 2, ''
) AS [UserId/Alias]
FROM tmp
/* -- OUTPUT
UserId/Alias
1/ MrX, MrY, MrA
2/ Abc, Xyz
*/
DECLARE #Str varchar(500)
SELECT #Str=COALESCE(#Str,'') + CAST(ID as varchar(10)) + ','
FROM dbo.fcUser
SELECT #Str
group_concat() sounds like what you're looking for.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
since you're on mssql, i just googled "group_concat mssql" and found a bunch of hits to recreate group_concat functionality. here's one of the hits i found:
http://www.stevenmapes.com/index.php?/archives/23-Recreating-MySQL-GROUP_CONCAT-In-MSSQL-Cross-Tab-Query.html
You can either loop through the rows with a cursor and append to a field in a temp table, or you could use the COALESCE function to concatenate the fields.
Sorry, read the question wrong the first time. You can do something like this:
declare #result varchar(max)
--must "initialize" result for this to work
select #result = ''
select #result = #result + alias
FROM aliases
WHERE username='Bob'