What's New in SQL Server for the Developer: SQL Server 2017

How many of us still type LTRIM(RTRIM()) today? You don't have to anymore. SQL Server 2017 finally fixed that, along with a few other string headaches. This is part 3 of my "What's New in SQL Server for the Developer" series, based on my session. So far we've covered 2012 and 2016. Today it's 2017.
A Little Background
For background, 2017 is the release where SQL Server left Windows for the first time. That's the headline everybody remembers. For developers, though, the part I use every day is the set of string functions that came with it.
SQL Server on Linux and Docker
SQL Server 2017 shipped on SUSE Linux Enterprise Server v12 SP2, Ubuntu 16.04, and Docker Engine 1.8+. For developers, Docker is the big one. You can spin up a full SQL Server instance on your laptop in about a minute, test against it, and throw it away.
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=YourStrong!Passw0rd" \
-p 1433:1433 --name sql2017 -d mcr.microsoft.com/mssql/server:2017-latestChange the password before you run it. Then connect from SSMS or Visual Studio Code to localhost, port 1433, as sa.
Keep in mind that not everything came over to Linux in 2017. Features like the Distributed Transaction Coordinator (MSDTC) and Machine Learning Services were added later in SQL Server 2019, so check the feature list before moving a production workload.
CONCAT_WS
CONCAT_WS stands for concatenate with separator. You give it the separator once, then the list of values, and it handles the rest. Here's an address table with some empty address lines:
CREATE TABLE dbo.Addresses
(
AddressId INT NOT NULL IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
UserId INT NOT NULL,
AddressLine1 VARCHAR(100) NULL,
AddressLine2 VARCHAR(100) NULL,
AddressLine3 VARCHAR(100) NULL,
City VARCHAR(50) NULL,
[State] VARCHAR(2) NULL,
ZipCode VARCHAR(10) NULL
);
INSERT INTO dbo.Addresses (UserId, AddressLine1, AddressLine2, AddressLine3, City, [State], ZipCode)
VALUES
(1, '123 Main Street', NULL, NULL, 'Jacksonville', 'FL', '32225'),
(2, '5th Ave South', 'Suite 500', NULL, 'New York', 'NY', '01032'),
(3, 'Attn: Returns Department', '1 Google Way', 'Building 25', 'San Francisco', 'CA', '66435');
SELECT
CONCAT_WS(', ', AddressLine1, AddressLine2, AddressLine3, City, [State], ZipCode) AS MailingAddress
FROM
dbo.Addresses;Notice that the first address comes back as "123 Main Street, Jacksonville, FL, 32225". No double commas where the NULL lines would be. CONCAT_WS skips NULL values completely, so you don't have to wrap every column in ISNULL or build the string with a pile of CASE expressions.
However, there is a catch: it only skips NULLs, not empty strings. If your data has '' instead of NULL, you'll get ", ," in the output. Clean those up with NULLIF(AddressLine2, '') first.
TRIM
TRIM replaces LTRIM(RTRIM()), and it can also strip characters other than spaces from both ends of a string.
SELECT TRIM(' test '); -- 'test'
SELECT TRIM('.,! ' FROM '# test .'); -- '# test'
SELECT TRIM('#. ' FROM '# test .'); -- 'test'
SELECT TRIM('-' FROM CAST(NEWID() AS VARCHAR(36))); -- dashes in the middle stay
SELECT TRIM('.' FROM '.5E905525.CE5B.'); -- '5E905525.CE5B'Notice the last two. TRIM removes characters only from the start and end of the string.
The period in the middle of '5E905525.CE5B' stays put, and so do the dashes in the middle of a GUID. If you want every dash gone, that's still a job for REPLACE:
SELECT REPLACE(CAST(NEWID() AS VARCHAR(36)), '-', '');Keep in mind that in 2017, TRIM always works on both sides. LEADING and TRAILING options were added later, in SQL Server 2022.
TRANSLATE
Have you ever written something like this?
SELECT
REPLACE(
REPLACE(
REPLACE(
REPLACE('2*[3+4]/{7-2}', '[', '('), ']', ')'), '{', '('), '}', ')');Four nested REPLACE calls to swap brackets and braces for parentheses. TRANSLATE does the same thing in one call. Each character in the second argument is swapped for the character in the same position in the third argument.
SELECT TRANSLATE('2*[3+4]/{7-2}', '[]{}', '()()'); -- 2*(3+4)/(7-2)Here's a real-world one: converting GeoJSON points to Well-Known Text (WKT) format and back again.
SELECT
TRANSLATE('[137.4, 72.3]', '[,]', '( )') AS Point, -- (137.4 72.3)
TRANSLATE('(137.4 72.3)', '( )', '[,]') AS Coordinates; -- [137.4,72.3]TRANSLATE is also always Supplemental Character (SC) collation-aware.
However, there is a catch: the characters and translations arguments must be exactly the same length, or you get an error. And it's one character for one character. If you need to replace a whole word, you still need REPLACE.
STRING_AGG with WITHIN GROUP
STRING_AGG is the opposite of STRING_SPLIT from 2016. It rolls a set of rows up into one delimited string. Add WITHIN GROUP, and you control the order.
SELECT
City,
STRING_AGG(EmailAddress, ';') WITHIN GROUP (ORDER BY EmailAddress ASC) AS Emails
FROM
dbo.Users
GROUP BY
City;That's one row per city with every email address in alphabetical order, ready to drop into a mailing list. Before 2017, this was the FOR XML PATH('') and STUFF trick, and I don't miss it.
Keep in mind that if the input is VARCHAR or NVARCHAR (not MAX), the result is capped at 8,000 bytes, and a big group throws an error. Cast the column to VARCHAR(MAX) or NVARCHAR(MAX) inside STRING_AGG when the list could get long.
Conclusion
SQL Server 2017 doesn't have the long feature list of 2016, but CONCAT_WS, TRIM, TRANSLATE, and STRING_AGG clean up a lot of ugly string code. And being able to run SQL Server in Docker on a laptop changed how a lot of us build and test.
Next up in the series is SQL Server 2019, including the truncation error message everyone has been waiting years for.
If you have any questions, or you've got a nested REPLACE you're itching to convert, feel free to reach out or comment below!


Comments