One of Date Island Solutions
Posted: May 27, 2022 Filed under: Uncategorized Leave a commentQuestion is from here:
I have come up with one solution:
https://gist.github.com/jingyang-li/2efff23413bec8bc201492a742e0a875
Improved String_Split Function in SQL Server 2022
Posted: May 24, 2022 Filed under: Uncategorized Leave a commentselect @@version
/* Microsoft SQL Server 2022 (CTP2.0) – 16.0.600.9 (X64) May 20 2022 13:29:42
Copyright (C) 2022 Microsoft Corporation Enterprise Evaluation Edition (64-bit)
on Windows 10 Pro 10.0 (Build 19044: )
*/
declare @s varchar(100)=’1,2,4,2,5,3,6,7′
select * from string_split(@s,’,’,1)
/*
value ordinal
1 1
2 2
4 3
2 4
5 5
3 6
6 7
7 8
*/
SQL Server 2022 Public Preview(CPT2.0)
Posted: May 24, 2022 Filed under: Uncategorized Leave a commentBob Ward announced from Linkedin post: the long wait SQL Server Public Preview is available for download today(May 24, 2022).
Here is the link I used to get my local copy:
https://info.microsoft.com/ww-landing-sql-server-2022.html
Will play with the new features in next couple of days and dive into it.
Split Values into Columns –XML Solution
Posted: May 12, 2022 Filed under: Uncategorized Leave a comment
Retrieve JSON File Values
Posted: May 10, 2022 Filed under: Uncategorized Leave a commentA question from Q&A:
https://docs.microsoft.com/en-us/answers/questions/823494/json-file-to-sql-table.html
Here are a few ways to get the result:
https://gist.github.com/jingyang-li/644d75ffa21234044d9442e01c027b31
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, WorkAddress VARCHAR(MAX));
INSERT INTO @t VALUES
(’89 Oliver”s Yard’ + CHAR(13) +
’21 City Rd’+ CHAR(13) +
‘London’+ CHAR(13) +
‘EC1Y 1HP’);
;with mycte as (
SELECT id, ‘[“‘+ REPLACE(WorkAddress, CHAR(13), ‘”,”‘) + ‘”] ‘ jsCol
FROM @t
)
select distinct id
,JSON_VALUE(jsCol, ‘$[0]’) AS Address1
,JSON_VALUE(jsCol, ‘$[1]’) AS Street
,JSON_VALUE(jsCol, ‘$[2]’) AS City
,JSON_VALUE(jsCol, ‘$[3]’) AS Postcode
FROM mycte
cross apply openjson(jsCol ) as j
–Select id
–,max(Case when [key]=0 then value else null end) LVL_1
–,max(Case when [key]=1 then value else null end) LVL_2
–,max(Case when [key]=2 then value else null end) LVL_3
–,max(Case when [key]=3 then value else null end) LVL_4
–FROM mycte
–cross apply openjson(jsCol ) d
–group by id
A T-SQL Solution for a Question on Q&A
Posted: May 10, 2022 Filed under: Uncategorized Leave a commentI came across a question “Need Help with SQL UPDATE where the Target and Source have a many to many relationship” on Microsoft Q&A. It is a complicate question and it took some time time to figure out the right solution after a few attempts. I posted this link here to record how I solved the problem.
By the way, I am a volunteer moderator on the site so I can see all revisions from all replies on the question.
https://gist.github.com/jingyang-li/2c5ea23a934c59641717c87f5831adff
Check Kaprekar number UDF with T-SQL
Posted: May 8, 2022 Filed under: Uncategorized Leave a commentA simplified version with T-SQL to find Kaprekar number under 1000.
https://gist.github.com/jingyang-li/f6a472cfb724e7a14035d7321ea5c8cc
FizzBuzz with T-SQL
Posted: May 2, 2022 Filed under: Uncategorized Leave a commentThere many ways to solve this problem and there are multiple ways to provide decent solutions in SQL Server as well. For the number in question we try to solve, any solution should work just fine. In SQL Server (database in general), we are expecting a SET based solution for obvious reasons. I think this thinking is important and that can help to prepare query solutions in a right direction for query performance optimization in applications.
I have compiled a few ways to solve FizzBuzz quiz in the following for my own reference:
FizzBuzz_Number_XML.sql
FizzBuzz_ThreeLoop.sql
FizzBuzz_JSON_Solutions.sql