This question already has answers here:
Split a CSV field into different rows in SQL
(3 answers)
Closed 2 years ago.
I want to transpose different values within a column separated by a specific character without involving PIVOT function
For the below example different set of courses are separated by semi-colon. The intention is to create different rows whenever it will find a semi-colon. I am not sure how many different set of values will be there under courses separated by semi-colon.
The solution I am looking for using ANSI SQL.
Course_Year Courses
2004 A|B|C|D;E|F|G|H
2005 A1|B1|C1|D1
2006 X1|X2|X3|X4;Y1|Y2|Y3|Y4;Z1|Z2|Z3|Z4
Output I am looking for
Course_Year Course
2004 A|B|C|D
2004 E|F|G|H
2005 A1|B1|C1|D1
2006 X1|X2|X3|X4
2006 Y1|Y2|Y3|Y4
2006 Z1|Z2|Z3|Z4
If you are using sql server then you can easily achieve that with stuff and for xml path():
select distinct t1.Course_Year,
STUFF(
(SELECT '; ' + convert(varchar(10), t2.Course, 120)
FROM courses t2
where t1.Course_Year = t2.Course_Year
FOR XML PATH (''))
, 1, 1, '') AS Courses
from courses t1;
Output:
To transform ';' separated strings from a column to row you can use strin_split() function with cross apply in sql server as below:
select Course_Year,value course from course cross apply string_split(courses,';')
Input:
Output:
Related
This question already has answers here:
Pad a string with leading zeros so it's 3 characters long in SQL Server 2008
(18 answers)
Closed 10 months ago.
I have to add a leading 0 in a column called houses, where it is a unique list of 3 digits, but I wanted have to add a leading 0 to it --> so 4 digits instead of 3.
Can someone help me with this query as the code is incompatible in the '|' operator.
The code is as follows:
select houses from house_numbers
order by houses;
select houses,
case
when len(houses)=3 then '0' | houses
when len(houses)=4 then '' | houses
end as houses
from house_numbers
The string concatenation operator in SQL Server is +, not ||, so you should use:
CASE
WHEN LEN(houses) = 3 THEN '0' + houses
WHEN LEN(houses) = 4 THEN '' + houses
END AS houses
However, a better way to do this would be to just left pad with zero to a length of 4:
RIGHT('0000' + ISNULL(houses, ''), 4) AS houses
You are looking for the CONCAT function here, assuming you are dealing with varchars
when len(houses)=3 then CONCAT('0' , houses)
You could simplify like so
select houses from house_numbers
order by houses;
select houses,
case
when len(houses)=3 then CONCAT('0' , houses)
else houses
end as houses
from house_numbers
Always add the Zero and then strip of the right 4 chars, I believe this might be less resource intensive than doing a case
SELECT RIGHT('0'+CONVERT(VARCHAR(10),345),4) AS Three_Digit_Example,
RIGHT('0'+CONVERT(VARCHAR(10),1345),4) AS Four_Digit_Example
I have a column which has inconsistent data. The column named ID and it can have values such as
0897546321
ABC,0876455321
ABC,XYZ,0873647773
ABC,
99756
test only
The SQL query should fetch only Ids which are of 10 digit in length, should begin with a 08 , should be not null and should not contain all characters. And for those values, which have both digits and characters such as ABC,XYZ,0873647773, it should only fetch the 0873647773 . In these kind of values, nothing is fixed, in place of ABC, XYZ , it can be anything and can be of any length.
The column Id is of varchar type.
My try: I tried the following query
select id
from table
where id is not null
and id not like '%[^0-9]%'
and id like '[08]%[0-9]'
and len(id)=10
I am still not sure how should I deal with values like ABC,XYZ,0873647773
P.S - I have no control over the database. I can't change its values.
SQL Server generally has poor support regular expressions, but in this case a judicious use of PATINDEX is viable:
SELECT SUBSTRING(id, PATINDEX('%,08[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9],%', ',' + id + ','), 10) AS number
FROM yourTable
WHERE ',' + id + ',' LIKE '%,08[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9],%';
Demo
If you normalise your data, and split the delimited data into parts, you can achieve this some what more easily:
SELECT SS.value
FROM dbo.YourTable YT
CROSS APPLY STRING_SPLIT(YT.YourColumn,',') SS
WHERE LEN(SS.value) = 10
AND SS.value NOT LIKE '%[^0-9]%';
If you're on an older version of SQL Server, you'll have to use an alternative String Splitter method (such as a XML splitter or user defined inline table-value function); there are plenty of examples on these already on Stack Overflow.
db<>fiddle
I have a database with 2 columns
A B
-- --
X 1995
Y 2005
C 1962
D 2003
I'm trying to create a SQL statement that will take a string of comma delimited values and return a json list of values in B where any value in the string is in A
so if the comma delimited string was 'X,C' the json list would be [1995,1962]
I've been using json path to try this, but I can't get it exactly like I want it and I've been spinning my wheels for too long
This is what I've tried:
Select mt.B as json_list_b_values
From [dbo].[myTable] mt
Where mt.A in (Select value From String_Split('X,C', ',')) for json path
This is the ouput:
[ {"json_list_b_values":"1995"}, {"json_list_b_values":"1962"} ]
As you're on 2016 you can't use STRING_AGG, but you can use the old tried and tested FOR XML PATH and STUFF method:
DECLARE #list varchar(8000) = 'X,C';
WITH VTE AS(
SELECT *
FROM(VALUES('X',1995),
('Y',2005),
('C',1962),
('D',2003)) V(A,B))
SELECT '[' + STUFF((SELECT CONCAT(',',V.B)
FROM VTE V
CROSS APPLY STRING_SPLIT(#list,',') SS
WHERE SS.[value] = V.A
FOR XML PATH('')),1,1,'') + ']';
I am no SQL expert. So please bear with me.
Step 1 - Handle the CSV input
You are already doing this by using the IN clause. I would store these results in a table variable
Step 2 - Convert the query results to a simple JSON array
I can think of the function STRING_AGG(). This will concatenate the rows into a flat string.
E.g. Join the FirstName column into a comma delimited string
SELECT STRING_AGG ( ISNULL(FirstName,'N/A'), ',') AS csv
FROM Person.Person;
Will produce the following
John,N/A,Mike,Peter,N/A,N/A,Alice,Bob
Code snippet based on your example
I am using a table variable #result1 to hold the result from Step 1.
SELECT '[ "' + string_agg( json_list_b_values, '", "') + '" ]' FROM #result1
MSDN reference for STRING_AGG()
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017
CORRECTION
The function STRING_AGG() is available in SQL 2017.
I'm using this code
(SELECT (MAX(CODE) +1 WHERE ISNUMERIC([code]) = 1)
I want to max +1 only my numbers of my column preventing characters characters.
NOTE: THIS QUESTION WAS TAGGED MYSQL WHEN THIS ANSWER WAS POSTED.
You can use substring_index() to split the values and then re-unite them:
(SELECT CONCAT(SUBSTRING_INDEX(MAX(Code), '-', 1), '-',
SUBSTRING_INDEX(MAX(CODE), '-', -1) + 1
)
FROM . . .
WHERE code LIKE '%NEW-1%'
)
This assumes that the wildcards do not have hyphens in them, and that the values after the "1" are all numbers.
Also, this doesn't pad the number is zeroes, but that is a good idea for such codes -- it ensures that they are always the same length and that they sort correctly.
The MAX() function accepts expressions, not just column names:
SELECT MAX(CASE ISNUMERIC(code) WHEN 1 THEN code END)+1 as next_code
FROM (
SELECT '15' AS code
UNION ALL SELECT ' 98 ' AS code
UNION ALL SELECT 'New-45' AS code
) foo
WHERE ISNUMERIC(code)=1;
16
(Link is to SQL Server 2005, docs for SQL Server 2000 are apparently no longer on line, but MAX() belongs to SQL standard anyway.)
This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 8 years ago.
Using simple query , I can do something like
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
and get:
shopping
fishing
coding
but instead I just want 1 row, 1 col:
shopping, fishing, coding
for ref-- Can I concatenate multiple MySQL rows into one field?
I want to do this in sql server ??
SQL Server doesn't have great support for aggregate string concatenation. But you can do:
select stuff((select ', ' + hobbies
from peoples_hobbies
where person_id = 5
for xml path ('')
), 1, 2, '') as hobbies;