SQL 2016 has been released now already since a couple of months. Many of us have been testing it, even developed applications on it, or fully migrated to it on production environments (thumbs up!). But did you know about the existence of following new syntax in SQL 2016 or vNext? There is more than what I will discuss in this post, but it’s a good start.
When you have long text strings or binary data saved in your database and want to keep your database size low, then the new COMPRESS and DECOMPRESS function will be very handy. It compresses the given value with the ZIP method. Possible data types are: nvarchar(n), nvarchar(max), varchar(n), varchar(max), varbinary(n), varbinary(max), char(n), nchar(n), or binary(n)
Return data type is: varbinary(max)
[html] INSERT INTO player (name, surname, info ) VALUES (N'Ovidiu', N'Cracium', COMPRESS(N'{"sport":"Tennis","age": 28,"rank":1,"points":15258, turn":17}')); [/html]
[html]SELECT _id, name, surname, datemodified,</pre> CAST(DECOMPRESS(info) AS NVARCHAR(MAX)) AS info FROM player;[/html]
In all SQL versions before 2016, you had to do a check in the meta data to see whether a table, procedure, column, constraint or trigger existed in case you wanted to create it again. SQL 2016 made it easier for us with the DROP .. IF EXITS syntax.
[html] --TABLES IF OBJECT_ID('dbo.TableX') IS NOT NULL DROP TABLE dbo.TableX --Can be replaced with DROP TABLE IF EXISTS dbo.TableX --PROCEDURES IF OBJECT_ID('dbo.sp_ProcedureX') IS NOT NULL DROP PROCEDURE dbo.sp_ProcedureX --Can be replaced with DROP PROCEDURE IF EXISTS dbo.sp_ProcedureX --TRIGGERS IF OBJECT_ID('dob.tr_TriggerX') IS NOT NULL DROP TRIGGER dbo.tr_TriggerX --Can be replaced with DROP TRIGGER IF EXISTS dbo.tr_TriggerX --COLUMNS IF EXISTS(SELECT * FROM sys.columns WHERE Name = N'ColumnB' AND OBJECT_ID = OBJECT_ID(N'dbo.TableX')) ALTER TABLE dbo.TableX DROP COLUMN ColumnB --Can be replaced with ALTER TABLE dbo.TableX DROP COLUMN IF EXISTS ColumnB --CONSTRAINTS IF OBJECT_ID('dbo.DF_TableX_ColumnA', 'C') IS NOT NULL ALTER TABLE dbo.TableX DROP CONSTRAINT DF_TableX_ColumnA --Can be replaced with ALTER TABLE dbo.TableX DROP CONSTRAINT IF EXISTS DF_TableX_ColumnA[/html]
This might be a syntax that a lot of people will be happy about. Previously, there was no way to have one script that created and updated a SP without removing it first. With the new CREATE OR ALTER PROCEDURE function its finally possible.
[html]IF OBJECT_ID('dbo.sp_ProcedureX') IS NOT NULL DROP PROCEDURE dbo.sp_ProcedureX GO CREATE PROCEDURE dbo.sp_ProcedureX AS BEGIN PRINT (1) END --Can be replaced by CREATE OR ALTER PROCEDURE dbo.sp_ProcedureX AS BEGIN PRINT (1) END[/html]
We all know that using the DELETE syntax is heavy on memory and tempdb. Before, it was the only option to remove for example a whole year of data from a FACT table. Now, there is an other option the TRUNCATE TABLE … WITH PARTITION. With this new functionality its possible to TRUNCATE a PARTITION, which of course will go faster then a DELETE.
[html]TRUNCATE TABLE PartitionTable1 WITH (PARTITIONS (2, 4, 6 TO 8));[/html]
JSON text is now fully supported. Extraction and creation.
[html] --Some example JSON data DECLARE @json NVARCHAR(4000) SET @json = N'{ "info":{ "type":1, "address":{ "town":"Bristol", "county":"Avon", "country":"England" }, "tags":["Sport", "Water polo"] }, "type":"Basic" }' --Extract single values SELECT JSON_VALUE(@json, '$.info.address.town') --Extract array of values SELECT * FROM OPENJSON(@json, '$.info.tags') --Creating a JSON dataset SELECT object_id, name FROM sys.tables FOR JSON PATH[/html]
A detailed description of what is possible with the JSON functionality in SQL can be found on TechNet
Version: vNext
Finally! Yes, it’s here. Many people were asking for it. Now we have a string function that removes leading as well as trailing spaces.
[html]SELECT LTRIM(RTRIM(' Why do I need to use 2 functions?   '))[/html]
can be done now with
[html] SELECT TRIM(' Why do I need to use 2 functions?   ')[/html]
Version: vNext
This function takes a variable number of arguments and concatenates them into a single string using the first argument as separator.
[html]SELECT CONCAT_WS(',','1 Microsoft Way', NULL, NULL, 'Redmond', 'WA', 98052) AS Address;[/html]
Result:
[html] 1 Microsoft Way,Redmond,WA,98052[/html]
Version: vNext
With this function that will come with vNext you can aggregate string column values with a specified seperator. Seperator will not added to last value.
[html]SELECT STRING_AGG (FirstName, ', ') AS csv FROM Person.Person;[/html]
Result:
[html]Tom, Patrick, Walter, Nick, Frederick, Stefaan[/html]
Version: vNext
The translate function returns the string provided as a first argument after some characters specified in the second argument are translated into a destination set of characters.
[html]SELECT TRANSLATE('a string value', 'abcdefghijklmnopqrstuvwxyz', 'zyxwvutsrqponmlkjihgfedcba'); --Result z hgirmt ezofv[/html]
I would not be shocked if 99% percent of database developers have a function on one or multiple databases to split a string into multiple rows. Now, we can ‘finally’ say this has been implemented as a default function in SQL 2016.
[html]SELECT * FROM string_split('ABC,DEF,GHI,JKL,MNO,PQR,STU,VWX,YZA', ',')[/html]
If you, for example, need to calculate the time difference in (seconds, nanosecond, …) between two dates that are way out of each other, then you would do this:
[html]SELECT DATEDIFF(SECOND,'19000101', '29991231')[/html]
The problem with this is, however, is that it doesn’t work. The DATEDIFF function returns INT, displaying the following error:
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
This is fixed with a new function DATEDIFF_BIG.
[html]SELECT DATEDIFF_BIG(SECOND,'19000101', '29991231')[/html]
© 2023 Kohera
Crafted by
© 2022 Kohera
Crafted by
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |