SQL Server convert select a column and convert it to a string - sql

Is it possible to write a statement that selects a column from a table and converts the results to a string?
Ideally I would want to have comma separated values.
For example, say that the SELECT statement looks something like
SELECT column
FROM table
WHERE column<10
and the result is a column with values
|column|
--------
| 1 |
| 3 |
| 5 |
| 9 |
I want as a result the string "1, 3, 5, 9"

You can do it like this:
Fiddle demo
declare #results varchar(500)
select #results = coalesce(#results + ',', '') + convert(varchar(12),col)
from t
order by col
select #results as results
| RESULTS |
-----------
| 1,3,5,9 |

There is new method in SQL Server 2017:
SELECT STRING_AGG (column, ',') AS column FROM Table;
that will produce 1,3,5,9 for you

select stuff(list,1,1,'')
from (
select ',' + cast(col1 as varchar(16)) as [text()]
from YourTable
for xml path('')
) as Sub(list)
Example at SQL Fiddle.

SELECT CAST(<COLUMN Name> AS VARCHAR(3)) + ','
FROM <TABLE Name>
FOR XML PATH('')

The current accepted answer doesn't work for multiple groupings.
Try this when you need to operate on categories of column row-values.
Suppose I have the following data:
+---------+-----------+
| column1 | column2 |
+---------+-----------+
| cat | Felon |
| cat | Purz |
| dog | Fido |
| dog | Beethoven |
| dog | Buddy |
| bird | Tweety |
+---------+-----------+
And I want this as my output:
+------+----------------------+
| type | names |
+------+----------------------+
| cat | Felon,Purz |
| dog | Fido,Beethoven,Buddy |
| bird | Tweety |
+------+----------------------+
(If you're following along:
create table #column_to_list (column1 varchar(30), column2 varchar(30))
insert into #column_to_list
values
('cat','Felon'),
('cat','Purz'),
('dog','Fido'),
('dog','Beethoven'),
('dog','Buddy'),
('bird','Tweety')
)
Now – I don’t want to go into all the syntax, but as you can see, this does the initial trick for us:
select ',' + cast(column2 as varchar(255)) as [text()]
from #column_to_list sub
where column1 = 'dog'
for xml path('')
--Using "as [text()]" here is specific to the “for XML” line after our where clause and we can’t give a name to our selection, hence the weird column_name
output:
+------------------------------------------+
| XML_F52E2B61-18A1-11d1-B105-00805F49916B |
+------------------------------------------+
| ,Fido,Beethoven,Buddy |
+------------------------------------------+
You can see it’s limited in that it was for just one grouping (where column1 = ‘dog’) and it left a comma in the front, and additionally it’s named weird.
So, first let's handle the leading comma using the 'stuff' function and name our column stuff_list:
select stuff([list],1,1,'') as stuff_list
from (select ',' + cast(column2 as varchar(255)) as [text()]
from #column_to_list sub
where column1 = 'dog'
for xml path('')
) sub_query([list])
--"sub_query([list])" just names our column as '[list]' so we can refer to it in the stuff function.
Output:
+----------------------+
| stuff_list |
+----------------------+
| Fido,Beethoven,Buddy |
+----------------------+
Finally let’s just mush this into a select statement, noting the reference to the top_query alias defining which column1 we want (on the 5th line here):
select top_query.column1,
(select stuff([list],1,1,'') as stuff_list
from (select ',' + cast(column2 as varchar(255)) as [text()]
from #column_to_list sub
where sub.column1 = top_query.column1
for xml path('')
) sub_query([list])
) as pet_list
from #column_to_list top_query
group by column1
order by column1
output:
+---------+----------------------+
| column1 | pet_list |
+---------+----------------------+
| bird | Tweety |
| cat | Felon,Purz |
| dog | Fido,Beethoven,Buddy |
+---------+----------------------+
And we’re done.
You can read more here:
FOR XML PATH in SQL server and [text()]
https://learn.microsoft.com/en-us/sql/relational-databases/xml/use-path-mode-with-for-xml?view=sql-server-2017
https://www.codeproject.com/Articles/691102/String-Aggregation-in-the-World-of-SQL-Server

This a stab at creating a reusable column to comma separated string. In this case, I only one strings that have values and I do not want empty strings or nulls.
First I create a user defined type that is a one column table.
-- ================================
-- Create User-defined Table Type
-- ================================
USE [RSINET.MVC]
GO
-- Create the data type
CREATE TYPE [dbo].[SingleVarcharColumn] AS TABLE
(
data NVARCHAR(max)
)
GO
The real purpose of the type is to simplify creating a scalar function to put the column into comma separated values.
-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Rob Peterson
-- Create date: 8-26-2015
-- Description: This will take a single varchar column and convert it to
-- comma separated values.
-- =============================================
CREATE FUNCTION fnGetCommaSeparatedString
(
-- Add the parameters for the function here
#column AS [dbo].[SingleVarcharColumn] READONLY
)
RETURNS VARCHAR(max)
AS
BEGIN
-- Declare the return variable here
DECLARE #result VARCHAR(MAX)
DECLARE #current VARCHAR(MAX)
DECLARE #counter INT
DECLARE #c CURSOR
SET #result = ''
SET #counter = 0
-- Add the T-SQL statements to compute the return value here
SET #c = CURSOR FAST_FORWARD
FOR SELECT COALESCE(data,'') FROM #column
OPEN #c
FETCH NEXT FROM #c
INTO #current
WHILE ##FETCH_STATUS = 0
BEGIN
IF #result <> '' AND #current <> '' SET #result = #result + ',' + #current
IF #result = '' AND #current <> '' SET #result = #current
FETCH NEXT FROM #c
INTO #current
END
CLOSE #c
DEALLOCATE #c
-- Return the result of the function
RETURN #result
END
GO
Now, to use this. I select the column I want to convert to a comma separated string into the SingleVarcharColumn Type.
DECLARE #s as SingleVarcharColumn
INSERT INTO #s VALUES ('rob')
INSERT INTO #s VALUES ('paul')
INSERT INTO #s VALUES ('james')
INSERT INTO #s VALUES (null)
INSERT INTO #s
SELECT iClientID FROM [dbo].tClient
SELECT [dbo].fnGetCommaSeparatedString(#s)
To get results like this.
rob,paul,james,1,9,10,11,12,13,14,15,16,18,19,23,26,27,28,29,30,31,32,34,35,36,37,38,39,40,41,42,44,45,46,47,48,49,50,52,53,54,56,57,59,60,61,62,63,64,65,66,67,68,69,70,71,72,74,75,76,77,78,81,82,83,84,87,88,90,91,92,93,94,98,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,120,121,122,123,124,125,126,127,128,129,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159
I made my data column in my SingleVarcharColumn type an NVARCHAR(MAX) which may hurt performance, but I flexibility was what I was looking for and it runs fast enough for my purposes. It would probably be faster if it were a varchar and if it had a fixed and smaller width, but I have not tested it.

ALTER PROCEDURE [dbo].[spConvertir_CampoACadena]( #nomb_tabla varchar(30),
#campo_tabla varchar(30),
#delimitador varchar(5),
#respuesta varchar(max) OUTPUT
)
AS
DECLARE #query varchar(1000),
#cadena varchar(500)
BEGIN
SET #query = 'SELECT #cadena = COALESCE(#cadena + '''+ #delimitador +''', '+ '''''' +') + '+ #campo_tabla + ' FROM '+#nomb_tabla
--select #query
EXEC(#query)
SET #respuesta = #cadena
END

You can use the following method:
select
STUFF(
(
select ', ' + CONVERT(varchar(10), ID) FROM #temp
where ID<50
group by ID for xml path('')
), 1, 2, '') as IDs
Implementation:
Declare #temp Table(
ID int
)
insert into #temp
(ID)
values
(1)
insert into #temp
(ID)
values
(3)
insert into #temp
(ID)
values
(5)
insert into #temp
(ID)
values
(9)
select
STUFF(
(
select ', ' + CONVERT(varchar(10), ID) FROM #temp
where ID<50
group by ID for xml path('')
), 1, 2, '') as IDs
Result will be:

--------------------------- easy I Found Like it ----------------
SELECT STUFF((
select ','+ name
from tblUsers
FOR XML PATH('')
)
,1,1,'') AS names
name
---------
mari, joan, carls
---------

Use LISTAGG function,
ex. SELECT LISTAGG(colmn) FROM table_name;

Use simplest way of doing this-
SELECT GROUP_CONCAT(Column) from table

+------+----------------------+
| type | names |
+------+----------------------+
| cat | Felon |
| cat | Purz |
| dog | Fido |
| dog | Beethoven |
| dog | Buddy |
| bird | Tweety |
+------+----------------------+
select group_concat(name) from Pets
group by type
Here you can easily get the answer in single SQL and by using group by in your SQL you can separate the result based on that column value. Also you can use your own custom separator for splitting values
Result:
+------+----------------------+
| type | names |
+------+----------------------+
| cat | Felon,Purz |
| dog | Fido,Beethoven,Buddy |
| bird | Tweety |
+------+----------------------+

Related

How can I do something like "STRING_AGG" in Sql Server Compact?

everybody
I want to make something like STRING_AGG in Sql Server Compact.
For example, I want to flatten the code column in the table below:
+----+--------+
| Id | Code |
+----+--------+
| 1 | 256987 |
| 1 | 256985 |
| 1 | 356994 |
+----+--------+
So I will get something like that:
+----+------------------------+
| Id | Codes |
+----+------------------------+
| 1 | 256987, 256985, 356994 |
+----+------------------------+
Thanks in advance!
Your best bet will be to do it using C# (string.Join).
There a couple of ways to do this:
1: Using COALESCE
DECLARE #Tbl TABLE
(
Name VARCHAR(20)
);
INSERT INTO #Tbl VALUES
('Jim'),
('Tim'),
('Kim');
DECLARE #ReturnVar VARCHAR(256);
SELECT *
FROM #Tbl;
SELECT #ReturnVar = COALESCE(#ReturnVar + ', ', '') + Name
FROM #Tbl;
SELECT #ReturnVar;
2: Using XML
DECLARE #Tbl TABLE
(
Name VARCHAR(20)
);
INSERT INTO #Tbl VALUES
('Jim'),
('Tim'),
('Kim');
DECLARE #ReturnVar VARCHAR(256);
SELECT STUFF((SELECT ',' + Name
FROM #Tbl
FOR XML PATH('')),1,1,'') AS Name;
You can find a bit more detail here.

PostgreSQL: Check if substring is in a string and if true then replace the entire string with the substring

I have a column in a table like this:
| biz_name |
---------------
| www.Dog.com |
| Dog LLC. |
| Dog Inc. |
| www.Cat.com |
| Cat Corp |
And for each element in the column, I want to check if it contains the substring 'Dog' or 'Cat' and if it does, then to replace the entire string with either 'Dog' or 'Cat'.
The results should be:
| biz_name |
---------------
| Dog |
| Dog |
| Dog |
| Cat |
| Cat |
Is there a way to do this without using CASE WHEN as such:
CASE
WHEN biz_name ilike '%Dog%' THEN 'Dog'
WHEN biz_name ilike '%Cat%' THEN 'Cat'
ELSE biz_name END as biz_name
The above table is just an example to demonstrate what I'm trying to do, but the actual table I'm working on has many more substrings I'm trying to search for and replace with that using multiple CASE WHEN statements can get tedious. Does anyone have a more efficient way to do it? Thanks.
1) can you do a quick insert into a temp table
--assuming table name is biz
declare #name varchar(max)
declare #TEMP table (Name varchar(max))
-- insert all your values
insert into #TEMP values ('dog')
insert into #TEMP values ('cat')
set #name = select top 1 name from #TEMP
delete from #temp where name = #name -- remove from que /table
WHILE #name IS NOT NULL
BEGIN
update dbo.biz
set biz_name = #name
where biz_name like '%' + #name '%'
set #name = select top 1 name from #TEMP
delete from #TEMP where name = #name
END

SQL Evaluate formula stored in database

Is it possible to evaluate a string formula/expression stored in a column? (SQL Server 2014).
Example:
TABLE:
ID | Formula
1 | IIF(2<3,'A','B')
2 | IIF(3<4,'C','D')
3 | IIF(5<1,'E','F')
Query:
SELECT ID, Eval(Formula)
Output:
1 | A
2 | C
3 | F
With Dynamic SQL, but REALLY not recommended. Just in case you REALLY have to...
Declare #YourTable table (ID int,Formula varchar(100))
Insert Into #YourTable values
(1,'IIF(2<3,''A'',''B'')'),
(2,'IIF(3<4,''C'',''D'')'),
(3,'IIF(5<1,''E'',''F'')')
Declare #SQL varchar(max) = '>>>'
Select #SQL = Replace(#SQL + concat(' Union All Select ID=',ID,',Value=',Formula),'>>> Union All','')
From #YourTable
Exec (#SQL)
Returns
ID Value
1 A
2 C
3 F

SQL Column Names from a Excel Sheet

Im using SSMS 2014 and SQL Server 2014. I need to change the column names at the end of a query result by using a excel file or table with the data.
After some SELECT statements and stuff i get a table with data for example
+---------+---------+------+
| Col1 | Col2 | Col3 |
+---------+---------+------+
| Value 1 | Value 2 | 123 |
| Value 2 | Value 2 | 456 |
| Value 3 | Value 3 | 789 |
+---------+---------+------+
And the table or excelfile
+----+---------+-----------+-----------+
| ID | ColName | Language | Addition |
+----+---------+-----------+-----------+
| 1 | Col1 | D | 123 |
| 2 | Col2 | D | 456 |
| 3 | Col3 | D | 789 |
| 4 | Col1 | E | 123 |
| 5 | Col2 | E | 456 |
| 6 | Col3 | E | 789 |
+----+---------+-----------+-----------+
What i try to do is to get the addition value of each column and add it to the column name. It should only add the values with the specific language. #setLang = 'D'
Col1 + Addition
Col2 + Addition
Col3 + Addition
+-------------+-------------+---------+
| Col1 123 | Col2 456 | Col3789 |
+-------------+-------------+---------+
| Value 1 | Value 2 | 123 |
| Value 2 | Value 2 | 456 |
| Value 3 | Value 3 | 789 |
+-------------+-------------+---------+
I tried it over Information_Schema.Columns and filter with where table = 'resultTable' and Column_name = #cName. Maybe i need a loop to get each columnname.
Thanks for reading and trying to help me.
Give this a go - it uses a table, not an excel file (but that seems to be an option in your question). I have made some temporary tables and filled them with your values, but you will obviously not need to do this. You will need to replace out the references to the tempdb with the name of the database where your tables are kept and the temp tables #Original and #ExcelInfo with your table names.
I have also used a temp table to add an 'Id IDENTITY(1,1)` Column to the table holding your original data. This is needed to keep the unpivot in check; if you can modify your table to include an Id, that will make things easier, but if not, you can insert into a temp table as I have done.
The script is shorter than it looks - the whole first bit is just setting up the example; the real answer starts at the line that declares your language variable.
/*
The script between the first two dividing lines of dashes is just used to set up the example. The bit you want is from
the "-- Test Variables --" line.
*/
-----------------------------------------------------------------------------------------------------------------------
IF OBJECT_ID('tempdb..#Original') IS NOT NULL DROP TABLE #Original
IF OBJECT_ID('tempdb..#ExcelInfo') IS NOT NULL DROP TABLE #ExcelInfo
CREATE TABLE #Original
( Col1 VARCHAR(50)
,Col2 VARCHAR(50)
,Col3 VARCHAR(50))
CREATE TABLE #ExcelInfo
( Id INT IDENTITY(1,1) NOT NULL
,ColName VARCHAR(50) NOT NULL
,[Language] CHAR(1) NOT NULL
,Addition INT NOT NULL)
INSERT #Original
SELECT *
FROM
( SELECT 'Value 1' AS Col1,'Value 2' AS Col2 ,123 AS Col3
UNION SELECT 'Value 2' ,'Value 2' ,456
UNION SELECT 'Value 3' ,'Value 3' ,789) AS This
ORDER BY Col1
INSERT #ExcelInfo (ColName,[Language],Addition)
SELECT *
FROM
( SELECT 'Col1' AS ColName, 'D' AS [Language], 123 AS Addition
UNION SELECT 'Col2','D',456
UNION SELECT 'Col3','D',789
UNION SELECT 'Col1','E',123
UNION SELECT 'Col2','E',456
UNION SELECT 'Col3','E',789) AS This
ORDER BY [Language], Addition
-----------------------------------------------------------------------------------------------------------------------
-- Test Variables --
DECLARE #SetLang CHAR(1) = 'D'
-----------------------------------------------------------------------------------------------------------------------
-- make the default empty, not null on our dynamic string, so it can be added to
DECLARE #Columns VARCHAR(MAX) = ''
DECLARE #SQL VARCHAR(MAX)
CREATE TABLE #OriginalColumns
( Id INT IDENTITY(1,1)
,Name VARCHAR(50))
CREATE TABLE #BasicResult
(Id INT NOT NULL, Name VARCHAR(50), Value VARCHAR(50))
-- If you can add an id column to your original table, this bit is unecessary - you can use yours in place of this table
CREATE TABLE #Original_WITH_Id
( Id INT IDENTITY(1,1)
,Col1 VARCHAR(50)
,Col2 VARCHAR(50)
,Col3 VARCHAR(50))
INSERT #Original_WITH_Id
SELECT * FROM #Original
-----------------------------------------------------------------------------------------------------------------------
-- List out the columns and put the list in a variable.
INSERT #OriginalColumns
SELECT QUOTENAME(Col.name)
FROM tempdb.sys.columns AS Col
WHERE Col.object_id = OBJECT_ID('tempdb.dbo.#Original_WITH_Id')
-- we're not interested in the identity column at the moment
AND Col.name <> 'Id'
-- keep everything in the same order as they are listed on the table
ORDER BY Col.column_id
SELECT #Columns = #Columns + ',' + Name
FROM #OriginalColumns
-- clip off the leading comma
SELECT #Columns = SUBSTRING(#Columns,2,LEN(#Columns))
-- get a full list of everything, creating our new list of columns as we go, using the Id column to keep a mark on which
-- row each record originally came from
SET #SQL =
'INSERT #BasicResult
SELECT Id, New.Name, Value FROM
(SELECT Id, Name, Value
FROM #Original_WITH_Id
UNPIVOT (Value FOR Name IN (' + #Columns + ')) Unpvt) AS Old
JOIN (SELECT ColName, CONVERT(VARCHAR(50),ColName) + '' '' + CONVERT(VARCHAR(50),Addition) AS Name
FROM #ExcelInfo
WHERE [Language] = ''' + #SetLang + ''') AS New ON Old.Name = New.ColName'
PRINT #SQL
EXEC (#SQL)
-- now update our list of columns to be the new column headings
SET #Columns = ''
SELECT #Columns = #Columns + ',' + QUOTENAME(Name) FROM (SELECT DISTINCT Name FROM #BasicResult) AS Names
SELECT #Columns = SUBSTRING(#Columns,2,LEN(#Columns))
-- pivout our results back out to their original format, but with the new column headings (include the Id if you want)
SET #SQL =
'SELECT /*Id,*/ ' + #Columns + '
FROM
(SELECT Id, Name,Value
FROM #BasicResult) AS Up
PIVOT (MAX(Value) FOR Name IN (' + #Columns + ')) AS Pvt'
PRINT #SQL
EXEC (#SQL)
-- clean up --
DROP TABLE #OriginalColumns
DROP TABLE #BasicResult
Hope that helps! There may be a more efficient way to do this... I'm not sure.
Okay i tried it again but now without the Excelfile. I made a CSV out of the Excelfile and insert it with Bulk to my created Table:
IF NOT EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'CSVTest')
BEGIN
CREATE TABLE _DICTIONARY
( _TableName VARCHAR (20),
_ColumnName VARCHAR (20),
_Language VARCHAR (20),
_FieldShort VARCHAR (50),
_FieldMid VARCHAR (50),
_FieldLong VARCHAR (50)
)
BULK
INSERT _DICTIONARY
FROM 'C:\_DICTIONARY.csv'
WITH
(
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)
END
After that i rename all Columns by using a Cursor
DECLARE #dic_tableName as nvarchar(50),
#dic_columnName as nvarchar(50),
#db_tableName as nvarchar(50),
#db_columnName as nvarchar(50);
DECLARE C CURSOR FAST_FORWARD FOR
SELECT _TableName, _ColumnName FROM _DICTIONARY
OPEN C;
FETCH NEXT FROM C INTO #dic_tableName, #dic_columnName;
WHILE ##FETCH_STATUS = 0
BEGIN
IF EXISTS(SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = #dic_tableName AND COLUMN_NAME = #dic_columnName)
BEGIN
SET #db_tableName = #dic_tableName + '.' + #dic_columnName
SET #db_columnName = #dic_tableName + '_' + #dic_columnName
EXEC sp_rename #db_tableName, #db_columnName ,'COLUMN'
FETCH NEXT FROM C INTO #dic_tableName, #dic_columnName;
END
ELSE
BEGIN
FETCH NEXT FROM C INTO #dic_tableName, #dic_columnName;
END
END
CLOSE C;
DEALLOCATE C;
Its doing its job.

How to select a value from column in table, using column name from other table dynamically (reflection)

I am trying to select a value in a table with multiple columns, based on the column id in another table like this
foo:
+-------+--------+--------+--------+
| id | tag | col1 | col2 |
+-------+--------+--------+--------+
| 1 | XX | 1 | 2 |
+-------+--------+--------+--------+
bar:
+-------+--------+--------+
| id | field | tag |
+-------+--------+--------+
| 1 | col1 | XX |
+-------+--------+--------+
| 2 | col2 | XX |
+-------+--------+--------+
I guess what I am looking for is a mechanism like reflection, where I can map to a property value based on the name of that property, without having to know about the class or table ahead of time, since the columns can change over time, the resulting table should look something like this
foobar:
+--------+--------+
| tag | value |
--------++--------+
| XX | 1 |
--------++--------+
| XX | 2 |
--------++--------+
I tried something like the below code, however it gives me the column id in both result columns, I need the value of that particular field not the name of the column.
declare #SQL nvarchar(max)
declare #Pattern nvarchar(100)
set #Pattern = 'select bar.field, ''foo.[COL_NAME]'' as CurrentValue FROM bar INNER JOIN foo ON foo.[tag] = bar.[tag]'
select #SQL = stuff((select ' union all '+replace(#Pattern, '[COL_NAME]', field)
from bar
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 11, '')
exec (#SQL)
Don't think it's possible to do it dynamically with a single SQL statement. But you could do it with dynamic sql.
With the given data set, the simplest way to get the desired result would be with a case statement.
select foo.tag,
case
when bar.field = 'col1' then foo.col1
when bar.field = 'col2' then foo.col2
else null
end as value
from foo
inner join bar on foo.tag = bar.tag
Just the when...then... parts are dynamic but the rest is static. So loop over the bar table to build up the dynamic parts, and then concat it all together.
declare #sql varchar(2000)
set #sql=''
select #sql = #sql +' when bar.field='''+field+''' then foo.' + field
from (select distinct field from bar) sub
--print #sql
set #sql = 'select foo.tag,
case ' + #sql + '
else null
end as value
from foo
inner join bar on foo.tag = bar.tag'
--print #sql
exec(#sql)