Skip to content

Commit 55b99a9

Browse files
author
Matt Usher
authored
Merge pull request #72 from Azure-Samples/sqlextension
Adding the DATE_TRUNC function
2 parents f9dae07 + 602ab33 commit 55b99a9

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

SQL/Extension/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The Azure Synapse SQL Extension will install a collection of vies to your Synaps
3838

3939
### December 15, 2020
4040
What's new:
41+
- [Function]: The `microsoft.date_trunc` script installs a function that emulates the `DATE_TRUNC(date_part, expression)` IBM Netezza function.
4142
- [Function]: The `microsoft.firstdayofmonth` script installs a function that returns the first day of the specified month as DATETIME2.
4243
- [Function]: The `microsoft.firstdayofquarter` script installs a function that returns the first day of the specified quarter as DATETIME2.
4344
- [Function]: The `microsoft.firstdayofyear` script installs a function that returns the first day of the specified year as DATETIME2.

SQL/Extension/clean.sql

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
/*SELECT
2-
'IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('''+ SCHEMA_NAME(schema_id) +''') AND name = N'''+ name +''') DROP ' +
3-
CASE type
4-
WHEN 'FN' THEN 'FUNCTION'
5-
WHEN 'V' THEN 'VIEW'
6-
END + ' [' + SCHEMA_NAME(schema_id) + '].[' + name + '];'
7-
FROM
8-
sys.objects
9-
ORDER BY
10-
type,
11-
schema_id,
12-
name;
13-
*/
14-
151
-- Functions
162
PRINT 'Removing functions';
173
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'acosh') DROP FUNCTION [microsoft].[acosh];
184
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'asinh') DROP FUNCTION [microsoft].[asinh];
195
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'btrim') DROP FUNCTION [microsoft].[btrim];
6+
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'date_trunc') DROP FUNCTION [microsoft].[date_trunc];
207
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'dayoccurrence_of_month') DROP FUNCTION [microsoft].[dayoccurrence_of_month];
218
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'firstdayofmonth') DROP FUNCTION [microsoft].[firstdayofmonth];
229
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'firstdayofquarter') DROP FUNCTION [microsoft].[firstdayofquarter];

SQL/Extension/deploy.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ ECHO Deploying functions
4848
REM Functions
4949
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.acosh.sql
5050
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.asinh.sql
51+
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.date_trunc.sql
5152
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.dayoccurrence_of_month.sql
5253
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.firstdayofmonth.sql
5354
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.firstdayofquarter.sql

SQL/Extension/functions/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ The `microsoft.asinh` script installs a function that emulates the `ASINH(expres
99
## microsoft.btrim
1010
The `microsoft.btrim` script installs a function that emulates the `TRIM(BOTH 'trim_characters' FROM expression)` Teradata function.
1111

12+
## microsoft.date_trunc
13+
The `microsoft.date_trunc` script installs a function that emulates the `DATE_TRUNC(date_part, expression)` IBM Netezza function.
14+
1215
## microsoft.dayoccurrence_of_month
1316
The `microsoft.dayoccurrence_of_month` script installs a function that emulates the `DAYOCCURRENCE_OF_MONTH(expression)` Teradata function.
1417

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'date_trunc')
2+
DROP FUNCTION microsoft.date_trunc;
3+
GO
4+
5+
/*
6+
Note: The DATEDIFF function returns a 4-byte integer (int). Until Azure Synapse SQL supports DATEDIFF_BIG
7+
with an 8-byte integer return type, date_trunc can and likely will throw an overflow exception when
8+
truncating small date_part units like second or smaller.
9+
*/
10+
11+
CREATE FUNCTION microsoft.date_trunc(@unit VARCHAR(15), @expression VARCHAR(8000))
12+
RETURNS DATETIME2
13+
WITH SCHEMABINDING
14+
AS
15+
BEGIN
16+
17+
-- Declarations
18+
DECLARE @result DATETIME2;
19+
20+
SET @unit = UPPER(@unit);
21+
22+
SET
23+
@result =
24+
(
25+
CASE
26+
-- Year
27+
WHEN @unit = 'Y' OR @unit = 'YEAR' THEN DATEADD(YEAR, DATEDIFF(YEAR, 0, @expression), 0)
28+
29+
-- Month
30+
WHEN @unit = 'M' OR @unit = 'MONTH' THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, @expression), 0)
31+
32+
-- Day
33+
WHEN @unit = 'D' OR @unit = 'DAY' THEN DATEADD(DAY, DATEDIFF(DAY, 0, @expression), 0)
34+
35+
-- Hour
36+
WHEN @unit = 'H' OR @unit = 'HOUR' THEN DATEADD(HOUR, DATEDIFF(HOUR, 0, @expression), 0)
37+
38+
-- Minute
39+
WHEN @unit = 'MINUTE' THEN DATEADD(MINUTE, DATEDIFF(MINUTE, 0, @expression), 0)
40+
41+
-- Second
42+
WHEN @unit = 'S' OR @unit = 'SECOND' THEN DATEADD(SECOND, DATEDIFF(SECOND, 0, @expression), 0)
43+
44+
-- Millisecond
45+
WHEN @unit = 'MS' OR @unit = 'MILLISECOND' THEN DATEADD(MILLISECOND, DATEDIFF(MILLISECOND, 0, @expression), 0)
46+
47+
-- Microsecond
48+
WHEN @unit = 'MICROSECOND' THEN DATEADD(MICROSECOND, DATEDIFF(MICROSECOND, 0, @expression), 0)
49+
50+
-- Nanosecond
51+
WHEN @unit = 'NANOSECOND' THEN DATEADD(NANOSECOND, DATEDIFF(NANOSECOND, 0, @expression), 0)
52+
53+
-- Week
54+
WHEN @unit = 'W' OR @unit = 'WEEK' THEN DATEADD(DAY, -(DATEPART(WEEKDAY, @expression) - 1), @expression)
55+
56+
-- Quarter
57+
WHEN @unit = 'QUARTER' THEN microsoft.FirstDayOfQuarter(@expression)
58+
END
59+
);
60+
61+
RETURN @result;
62+
63+
END
64+
GO

0 commit comments

Comments
 (0)