Comma separated values in to multiple rows in SQL Server - sql-server

I have a table where it contains data in below format
How to achieve this in MS SQL Server.

This uses DelimitedSplit8K, as information on the ordinal position is required (something STRING_SPLIT and many other splitters don't supply). The below is Pseudo SQL as well, as the OP has provided images, rather that textual data:
SELECT {YourColumns}
FROM YourTable YT
CROSS APPLY dbo.DelimitedSplit8K(YT.Qualification,',') DSq
CROSS APPLY dbo.DelimitedSplit8K(YT.Instituion,',') DSi
WHERE DSq.ItemNumber = DSi.ItemNumber;
The true answer here, as has been mentioned in the comments, however, is to fix the data model.
An alternative method would be to use OPENJSON. This is something I have only been introduced to recently, and I don't have access to a SQL Server 2016 instance to test this against (I have used SQL Fiddle to test it runs though, but not against the image provided for my same reason above). I beleive this should also achieve your goal though:
SELECT OJq.[value], OJi.[Value]
FROM YourTable YT
CROSS APPLY (SELECT ca.[Key], ca.[value]
FROM OPENJSON('["' + REPLACE(YT.Qualification,',','","') + '"]') ca) OJq
CROSS APPLY (SELECT ca.[Key], ca.[value]
FROM OPENJSON('["' + REPLACE(YT.Instituion,',','","') + '"]') ca) OJi
WHERE OJq.[Key] = OJi.[Key];

Related

CROSS APPLY function in Presto/AWS

I'm trying to convert 4 date columns titled Created, Approved, Processed & Realized to a single column with all 4 dates AND a second column with the status of each of those dates.
The image at the end shows the data issue visually (apologies I'm still figuring out how to attach tables in textual form on stackoverflow)
To solve this, I successfully executed the CROSS APPLY
function in SQL server (see below), but I now need to do the same in AWS Simba
Athena or the Presto language. Can someone please guide me on what is the AWS/Presto equivalent of a CROSS APPLY function? Thank you in advance
SELECT
V.Date,
V.Status
From Table C
CROSS APPLY
(VALUES
(C.Created, 'Opened'),
(C.Approved, 'Approved'),
(C.Processed, 'Processed'),
(C.Realized, 'Realized')
) AS V([Date], Status)
I want to convert the following table:
You should be able to use UNNEST for this:
SELECT v.date, v.status
FROM m_table
CROSS JOIN UNNEST(ARRAY[
ROW(C.Created, 'Opened'),
ROW(C.Approved, 'Approved'),
ROW(C.Processed, 'Processed'),
ROW(C.Realized, 'Realized')
]) AS v(date, status);
This works in the latest Presto version, 337.
In Athena you probably still cannot UNNEST array or ROW
in ANSI SQL manner, so you may need some modifications.

Converting Oracle statement to SQL Server format

Below is an Oracle script that I need to execute on an SQL Server.
SELECT
records.pr_id,
SUBSTR (REPLACE (REPLACE (XMLAGG (XMLELEMENT ("x", prad4.selection_value)
ORDER BY prad4.selection_value),'</x>'),'<x>',' ; '),4) as teva_role
FROM records
Thanks for the help,
Barry
I programmed in SQL for years in several environments and it is about 75% the same. So, the SQL statement should work as is, however the functions (REPLACE, SUBSTR) will be what you need to research and change.
Also, you get columns from prad4 without including it in the FROM statement which is a problem.
And, finally, your parentheses aren't balanced which, I would think, would be a problem in Oracle as well.
This is basically concatenating a set of strings with a delimiter. The common way to do this, is using FOR XML PATH('') which seems to be the equivalent of the combination of XMLELEMENT() in Oracle, but with a different syntax. You can also use XML functions to prevent change of certain characters not allowed in XML. The STUFF takes care of the SUBSTR() part of your code. For a more detailed explanation, you can read this article on Creating a comma-separated list.
The code should look similar to this:
SELECT records.pr_id,
STUFF(( SELECT ' ; ' + prad4.selection_value
FROM prad4
WHERE prad4.pr_id = records.pr_id
ORDER BY prad4.selection_value
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)'), 1, 3, '')
FROM records;
Of course, with the improvements of SQL Server 2017, the code can be simplified to something like this:
SELECT records.pr_id,
STRING_AGG( selection_value, ' ; ') WITHIN GROUP (ORDER BY selection_value ASC)
FROM records;

Using FOR XML PATH, TYPE).value('.[1]','nvarchar(max)')

I was checking that using this is good to handle special characters but at the same time is over complicating the query generating a "cardinality estimate warning"
If I use FOR XML PATH(''), the query plan is much better and the cardinatity is gone. Anybody faced this issue before? is there any workaround to continue using FOR XML PATH, TYPE).value('.[1]','nvarchar(max)') and get rid of the cardinality issue?
SELECT r.ServiceId,
STUFF(
(
SELECT '; ' + u.Name
FROM dbo.UsedFor u
inner join dbo.ServiceUsedRelation r2
on u.UsedId = r2.UsedId
where
r2.ServiceId = r.ServiceId
FOR XML PATH, TYPE).value('.[1]','nvarchar(max)')
, 1
, 1
, ''
) as Name
FROM dbo.ServiceUsedRelation r
GROUP BY r.ServiceId
Stop using FOR XML PATH for concatenation. If you are using SQL Server 2017 you can use STRING_AGG. If not, you can implement the SQL String Utility Functions - look for the Concatenate class. More information ca be found here.
Having function that concatenate strings but it is aggregate at the same time, gives you the ability to write more complex grouping queries. It also simplify the used T-SQL syntax and improve performance.
For example, your query will looks like:
SELECT ServiceId
,[dbo].[Concatenate] (Name)
FROM dbo.ServiceUsedRelation
GROUP BY ServiceId;

Need Help Converting Oracle Query to SQL Server

Several weeks ago I made a post to get help with converting a comma delimited list of values into a format that it could be used as part of an IN clause in Oracle. Here is a link to the post.
Oracle invalid number in clause
The answer was to split up the list into an individual row for each value. Here's the answer that I ended up using.
SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
FROM ( SELECT '1,2,3,4' str FROM dual )
CONNECT BY instr(str, ',', 1, LEVEL - 1) > 0
Is there a way that I can do something similar in SQL Server without having to create a custom function? I noticed that there's a STRING_SPLIT function, but I don't seem to have access to that on this SQL Server.
Any advice you might have would be greatly appreciated. I've been trying to take a stab at this for the majority of the day.
String_split function is available in MS SQL Server starting from version 2016. If you use older version you can write a few lines of code which do the same.
declare #str varchar(100)='1,2,3,4' --initial string
;with cte as (--build xml from the string
select cast('<s>'+replace(#str,',','</s><s>')+'</s>' as xml) x
)
--receive rows
select t.v.value('.[1]','int') value
from cte cross apply cte.x.nodes('s') t(v)

What is a good way to paginate out of SQL 2000 using Start and Length parameters?

I have been given the task of refactoring an existing stored procedure so that the results are paginated. The SQL server is SQL 2000 so I can't use the ROW_NUMBER method of pagination. The Stored proc is already fairly complex, building chunks of a large sql statement together before doing an sp_executesql and has various sorting options available.
The first result out of google seems like a good method but I think the example is wrong in that the 2nd sort needs to be reversed and the case when the start is less than the pagelength breaks down. The 2nd example on that page also seems like a good method but the SP is taking a pageNumber rather than the start record. And the whole temp table thing seems like it would be a performance drain.
I am making progress going down this path but it seems slow and confusing and I am having to do quite a bit of REPLACE methods on the Sort order to get it to come out right.
Are there any other easier techniques I am missing?
There are two SQL Server 2000 compliant answers in this StackOverflow question - skip the accepted one, which is 2005-only:
No, I'm afraid not - SQL Server 2000 doesn't have any of the 2005 niceties like Common Table Expression (CTE) and such..... the method described in the Google link seems to be one way to go.
Marc
Also take a look here
http://databases.aspfaq.com/database/how-do-i-page-through-a-recordset.html
scroll down to Stored Procedure Methods
Depending on your application architecture (and your amount of data, it's structure, DB server load etc.) you could use the DB access layer for paging.
For example, with ADO you can define a page size on the record set (DataSet in ADO.NET) object and do the paging on the client. Classic ADO even lets you use a server side cursor, though I don't know if that scales well (I think this was removed altogether in ADO.NET).
MSDN documentation: Paging Through a Query Result (ADO.NET)
After playing with this for a while there seems to be only one way of really doing this (using Start and Length parameters) and that's with the temp table.
My final solution was to not use the #start parameter and instead use a #page parameter and then use the
SET #sql = #sql + N'
SELECT * FROM
(
SELECT TOP ' + Cast( #length as varchar) + N' * FROM
(
SELECT TOP ' + Cast( #page*#length as varchar) + N'
field1,
field2
From Table1
order by field1 ASC
) as Result
Order by Field1 DESC
) as Result
Order by Field 1 ASC'
The original query was much more complex than what is shown here and the order by was ordered on at least 3 fields and determined by a long CASE clause, requiring me to use a series of REPLACE functions to get the fields in the right order.
We've been using variations on this query for a number of years. This example gives items 50,000 to 50,300.
select top 300
Items.*
from Items
where
Items.CustomerId = 1234 AND
Items.Active = 1 AND
Items.Id not in
(
select top 50000 Items.Id
from Items
where
Items.CustomerId = 1234 AND
Items.Active = 1
order by Items.id
)
order by Items.Id

Resources