TRANSLATE()–Replace multiple characters in single function call


TRANSLATE() is a new SQL Server 2017 string function which supports replacing multiple characters in one function call, This will help us in avoid using multiple replace statements

Syntax

TRANSLATE(InputString, StringPattern, StringReplacement)

Example Usage

In this example, we have values delimited by , for field terminator and ~ for row terminator, which we are going to replace , (comma) with | (pipe) and ~ (Tilde) with # (Hash)

DECLARE @strInput VARCHAR(255) = ‘Test1,Test2,Test3,Test4,Test5~Test11,Test12,Test13,Test14,Test15~Test21,Test22,Test23,Test24,Test25’

SELECT @strInput [Input],
REPLACE(REPLACE(@strInput, ‘,’, ‘|’), ‘~’, ‘#’) [SQL 2016 & Before – Output],
TRANSLATE(@strInput, ‘,~’, ‘|#’) [SQL 2017 Output]

Output

capture20171006114812618

As you can see, in SQL Server 2016 and prior, we need to use 2 REPLACE() function calls, in SQL Server 2017, we can use 1 TRANSLATE() function call to implement the same functionality

Hope you find this post useful !!!

Advertisement

STRING_AGG() – Concatenate values in Rows with Separator


SQL Server 2017 introduces a very helpful String function to concatenate values from rows as one value with separator and main advantage is it doesn’t add the separator in the end of the value

Syntax for the function is as follows

STRING_AGG ( expression, separator ) [
WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] ) ]

Simple Example Usage – Building CSV from one field

SELECT String_Agg(strData, ‘,’) AS CSV FROM dbo.Test

capture20171005142123779

Example Usage with ORDER BY option on different field

SELECT String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV FROM dbo.Test

capture20171005142257083

Example Usage with GROUP BY and ORDER BY

SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV1
FROM dbo.Test
GROUP BY Category

capture20171005142405536

What is not supported ?

ORDER BY is supported only for one field or one set of fields and one direction only, , see below example, We cannot use ORDER BY intPK DESC for first STRING_AGG() and ORDER BY intPK ASC for next STRING_AGG

SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV1,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK ASC) AS CSV2
FROM dbo.Test
GROUP BY Category

 

The above query will return the below error message

Msg 8711, Level 16, State 1, Line 15
Multiple ordered aggregate functions in the same scope have mutually incompatible orderings.

Alternative way to get around this is use multiple queries and join them.

;WITH cteA
AS
(
SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK DESC) AS CSV1
FROM dbo.Test
GROUP BY Category
),
cteB
AS
(
SELECT
Category,
String_Agg(strData, ‘,’) WITHIN GROUP (ORDER BY intPK ASC) AS CSV2
FROM dbo.Test
GROUP BY Category
)
SELECT
A.Category, A.CSV1, B.CSV2
FROM cteA A
LEFT JOIN cteB B ON A.Category = B.Category

capture20171005143658060

Hope you find this post helpful !!!

TRIM()–New String function in SQL Server 2017


SQL Server 2017 introduces long awaited string function which supports both LTRIM() and RTRIM() together

Syntax for the function is as follows

TRIM ( [ characters FROM ] string )

Example usage

SELECT TRIM(‘   SQLXpertise   ‘) [SQL2017]

Output

image

In addition to trimming spaces, TRIM() function supports removing other characters in prefix and suffix as well, see below example, we are providing #, ~, <SPACE> as characters to removed from Prefix and Suffix

Example with Character removal

SELECT TRIM(‘#~ ‘ FROM ‘#   SQLXpertise   ~’) [SQL2017]

 

Output

image

This new function will be helpful in using one TRIM() call rather than 3 function calls of LTRIM(), RTRIM() and REPLACE()

Hope  you find this post helpful !!!

CONCAT_WS() – Concatenate with Separator–New String Function in SQL Server 2017


SQL Server 2017 has a new String function which supports concatenating multiple values with separator, ex. Comma (,) or Pipe (|)

This function will come handy, if you want to generate a CSV file quickly

Syntax for the function is as follows

CONCAT_WS ( separator, argument1, argument1 [, argumentN]… )

Example Usage

CREATE TABLE [dbo].[Test](
[intPK] [int] IDENTITY(1,1) NOT NULL,
[strData] [varchar](50) NOT NULL,
PRIMARY KEY CLUSTERED
(
[intPK] ASC
)
)
GO

INSERT INTO dbo.test (strData) VALUES (‘Test1’)
INSERT INTO dbo.test (strData) VALUES (‘Test2’)
INSERT INTO dbo.test (strData) VALUES (‘Test3’)
INSERT INTO dbo.test (strData) VALUES (‘Test4’)
INSERT INTO dbo.test (strData) VALUES (‘Test5’)

SELECT CONCAT_WS(‘|’, intPK, strData) AS Result FROM dbo.Test

In above example we are concatenating 2 fields using pipe as separator

Output

capture20171003120119644

NULL Handling

If any of the data fields has NULL, then we need to use ISNULL() or COALESCE() to handle this, else, the structure of the output will change

ALTER TABLE dbo.Test ADD strNullData VARCHAR(50) NULL

UPDATE dbo.Test SET strNullData=’A’ WHERE intPK=1
UPDATE dbo.Test SET strNullData=’B’ WHERE intPK=3
UPDATE dbo.Test SET strNullData=’C’ WHERE intPK=5

SELECT CONCAT_WS(‘|’, intPK, strNullData, strData) Result FROM dbo.Test

In the below output, rows 1,3 and 5 has 3 fields and rows 2 and 4 has only 2 fields

capture20171003125644345

Query with ISNULL() Handling

SELECT CONCAT_WS(‘|’, intPK, ISNULL(strNullData, ”), strData) Result FROM dbo.Test

Now the below output will have consistent list of fields

capture20171003125845846

Hope you find this helpful !!!

Upgrading SQL Server 2017 on Ubuntu Linux to RTM in 4 steps


SQL Server 2017 RTM is released and available on Windows, Linux and Docker, in this article, we will see the steps for upgrading SQL Server 2017 CTM Release on Ubuntu to RTM Version

Step 1: Connect to Ubuntu Server using your favorite Shell, I use Bash Shell on Windows, since its built-in Windows 10 now

Step 2: Remove old repository

sudo add-apt-repository "$(curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017-gdr.list)"

Step 3: Add new repository

sudo add-apt-repository "$(curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"

Step 4: Upgrade SQL Server 

sudo apt-get update

sudo apt-get install mssql-server

If you are using Availability groups in Linux, Please upgrade the primary machine first and then secondary replica

Have fun with SQL Server 2017

%d bloggers like this: