Convert SQL columns to rows - sql-server

I would like to split one records columns into multiple rows.
If I have a SQL statement like the following:
SELECT 1,2,3
Result:
1 | 2 | 3
How do I convert that to the following result?
1
2
3
I am currently using MS SQL 2008.

SELECT 1
UNION
SELECT 2
UNION
SELECT 3

To sum up the comments above:
If you want to convert columns into rows, you will need to use the
T-SQL UNPIVOT clause.
If you want to split a single column that contains comma separated
values, you will need to create a Function (example here)

Related

Filter repeating two digits pattern in SQL

I would like to filter some string numbers that have repeated two digits more than 3 times. For example, if it contains "121212", it will not be filtered, but if it contains "12121212" it will be filtered. I am trying to find a way to solve it using regular expression but could not find it.
In Oracle, use a regular expression to match two digits and then use a back-reference to check that the digits repeat 3-or-more times. Then to exclude those rows add NOT to the filter:
SELECT *
FROM table_name
WHERE NOT REGEXP_LIKE(value, '(\d\d)\1{3,}')
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT '121212' FROM DUAL UNION ALL
SELECT '12121212' FROM DUAL UNION ALL
SELECT '123121212' FROM DUAL;
Outputs:
VALUE
121212
123121212
fiddle

SQL Server testing for 1 value in multiple columns

I am testing a table and would like to find out if 10 columns of that table (integer fields) EQUAL the value 999. can ANY or the IN clause be used for this?
At a pure guess, and this is pseudo-SQL, but
SELECT {Columns}
FROM {YourTable}
WHERE '999' IN ({First Column},{Second Column},{Third Column},...,{Tenth Column});
The only way to test this is something like this:
select * from table1 where i1=999 and i1=i2 and i2=i3 and i3=i4 and i4=i5 and i5=16 and i6=i7 and i7=i8 and i8=i9;
where iX are column names. This will return rows which match all the value for all 9 columns.
TSQL is a MS implementation of various SQL standards.

use variable in select - tsql

Hi I have procedure which have parameter(#identFormat)
Example
"GUID"
"LotID|FeatureID"
And now I have Select query which should split this and use as columns.
Moreover result should be back combined.
Example:
Table:
Id LotID FeatureID
2 1 4
3 4 5
4 2 1
and if my #identFormat = "LotID|FeatureID" then it should return
Table:
1|4
4|5
2|1
Actually I have ncharchar #columns 'LotId + "|" + FeatureId'
Is it possible to use this like this:
Select #columns from Table ?
or using dynamic sql
EDIT:
Unfortunately combination of columns can be different. My purpose is send column names to procedure and select this columns from specific table. This is procedure to save data , but if something went wrong I must save this unique combination of columns in second table.
Unfortunatelly, it is not possible. You need to select separately and format the output

Binding literal list to SQL query as column that would return 1 row?

I have an SQL query that returns a single row, I have a list of numbers that I need to have returned as individual rows with the single row data bound to their row.
for example here's what I'm trying to do
select a,b,c, barcode
from database
join ('12345', '67890',...) as barcode
where a=1 and b=2 and c=3
I need to do it this way due to the fact i'm modifying some code that's looking for a specific format to come from the query, and modifying the code to match the literal list I have is far more difficult than doing something like this
Example Output:
a b c barcode
- - - -------
1 2 3 12345
1 2 3 67890
1 2 3 ....
1 2 3 ....
...
Easiest method would be to create a barcode table with a single column, insert the values you want here one at a time, then join to that table.
Could use a union to fudge it as well. Problem with join ('484','48583',...) is you are joining to a single row with multi columns, when you want one row per record.
pseudo coded:
select a,b,c, barcode
from database
join (select 12345 union all select 289384 union all...)a as barcode
where a=1 and b=2 and c=3
Basically, you could pass the list as a single CSV string and transform it into a row set of items. A table-valued function is often used in such cases, but there are actually many options to explore. A very comprehensive collection of various methods and their tests can be found in the set of articles by Erland Sommarskog: Arrays and Lists in SQL Server.
If it was e.g. a function, your query might look like this:
SELECT
t.a,
t.b,
t.c,
s.Value AS barcode
FROM yourtable t
CROSS JOIN dbo.Split('12345,67890', ',') s
WHERE t.a = 1
AND t.b = 2
AND t.c = 3

Simple SQL approach to transform columns into rows (SQL Server 2005 or 2008)

On SQL Server, executing the following SQL Statement:
SELECT 1,2,3
will return
(no column name) (no column name) (no column name)
1 2 3
Note that the columns don't have names and the number of columns is not definite (it can have 1 column or it can also have > 100 columns).
My question is - Does anybody know of a simple approach so I can get the following result:
(no column name)
1
2
3
What I'm really trying to do is come up with a SQL similar to the one below. I wish I could execute it as it is but of course we know that the Select 1,2,3 won't work, we have to somehow transform that into a table with the values in each row.
SELECT *
FROM NORTHWIND.DBO.CUSTOMERS
WHERE EMPLOYEEID IN (*Select 1,2,3*); -- *Select 1,2,3 will not work
Currently I'm thinking of creating a user defined function that returns a table by iterating through each column and dynamically creating multiple SQL statements combined by UNION similar to: SELECT 1 Col1 UNION SELECT 2 UNION SELECT 3. I'm not a fan of dynamic SQL and looping procedures in my queries as it can be expensive to process especially for an application with expected usage of 1000+ per minute. Also, there is that concern for SQL Injection Attacks with Dynamic SQL when I start using strings instead of integer values. I'm also trying to avoid temporary tables as it can even be more expensive to process.
Any ideas? Can we use UNPIVOT without the need for looping through the indefinite number of columns and dynamically creating the SQL text to execute it and transform the columnar values into rows? What about Common Table Expressions?
Get rid of the select and just specify a list of values:
SELECT * FROM NORTHWIND.DBO.CUSTOMERS
WHERE EMPLOYEEID IN (1,2,3);

Resources