Skip to content

Commit ecff4b1

Browse files
author
Matt Usher
authored
Merge pull request #65 from Azure-Samples/sqlextension
Adding the TO_CHAR function to the SQL Extension toolkit
2 parents 49806fb + 63313e4 commit ecff4b1

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

SQL/Extension/Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ The Azure Synapse SQL Extension will install a collection of vies to your Synaps
3131

3232
## Version History
3333

34+
### December 1, 2020
35+
What's new:
36+
- [Function]: The `microsoft.to_char` script installs a function that emulates the `TO_CHAR(expression, format)` Teradata function.
37+
3438
### November 12th, 2020
3539
What's new:
3640

SQL/Extension/deploy.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functio
5959
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.next_day.sql
6060
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.rpad.sql
6161
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.rtrim.sql
62+
sqlcmd -S %_server% -d %_database% -U %_username% -P %_password% -I -i .\functions\microsoft.to_char.sql
6263

6364
ECHO Deploying views
6465

SQL/Extension/functions/Readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ The `microsoft.next_day` script installs a function that emulates the `NEXT_DAY(
3737
The `microsoft.rpad` script installs a function that emulates the `RPAD(expression, length [, fill])` Teradata function.
3838

3939
## microsoft.rtrim
40-
The `microsoft.rtrim` script installs a function that emulates the `TRIM(TRAILING 'trim_characters' FROM expression)` Teradata function.
40+
The `microsoft.rtrim` script installs a function that emulates the `TRIM(TRAILING 'trim_characters' FROM expression)` Teradata function.
41+
42+
## microsoft.to_char
43+
The `microsoft.to_char` script installs a function that emulates the `TO_CHAR(expression, format)` Teradata function.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
IF EXISTS (SELECT * FROM sys.objects WHERE schema_id=SCHEMA_ID('microsoft') AND name = N'to_char')
2+
DROP FUNCTION microsoft.to_char;
3+
GO
4+
5+
CREATE FUNCTION microsoft.to_char(@date datetime2, @format VARCHAR(32) = '')
6+
RETURNS VARCHAR(128)
7+
WITH SCHEMABINDING
8+
AS
9+
BEGIN
10+
11+
-- Fast opt out
12+
IF (@date IS NULL OR TRIM(@format) = '')
13+
RETURN @date;
14+
15+
DECLARE @format_internal VARCHAR(48) = @format;
16+
DECLARE @date_internal VARCHAR(256);
17+
18+
-- Special case for Day of Year (DDD or D3)
19+
IF ( (CHARINDEX('DDD', @format_internal COLLATE Latin1_General_CS_AS) > 0) OR (CHARINDEX('D3', @format_internal COLLATE Latin1_General_CS_AS) > 0) )
20+
BEGIN
21+
-- Ideally will support THROW here in a future Azure Synapse SQL release.
22+
RETURN 'The format ''DDD'' (Day of Year) is not supported in T-SQL. Please try using the DATEPART(DAYOYYEAR, expression) syntax.'
23+
END
24+
25+
-- Special cases for hour separator
26+
SET @format_internal = REPLACE(@format_internal COLLATE Latin1_General_CS_AS, 'h', '\h');
27+
28+
-- Special case for minute separator
29+
SET @format_internal = REPLACE(@format_internal COLLATE Latin1_General_CS_AS, 'm', '\m');
30+
31+
-- Special case for second separator
32+
SET @format_internal = REPLACE(@format_internal COLLATE Latin1_General_CS_AS, 's', '\s');
33+
34+
-- Replacements for year
35+
SET @format_internal = REPLACE(@format_internal, 'YYYY', 'yyyy');
36+
SET @format_internal = REPLACE(@format_internal, 'Y4', 'yyyy');
37+
SET @format_internal = REPLACE(@format_internal, 'YY', 'yy');
38+
39+
-- Replacements for month
40+
SET @format_internal = REPLACE(@format_internal, 'M4', 'MMMM');
41+
SET @format_internal = REPLACE(@format_internal, 'M3', 'MMM');
42+
43+
-- Replacements for day
44+
SET @format_internal = REPLACE(@format_internal, 'DD', 'dd');
45+
SET @format_internal = REPLACE(@format_internal, 'D', 'd ');
46+
47+
-- Replacements for hour
48+
49+
-- Replacements for minute
50+
SET @format_internal = REPLACE(@format_internal, 'MI', 'mm');
51+
52+
-- Replacements for 12/24 hour clock
53+
SET @format_internal = REPLACE(@format_internal, 'T', 'tt');
54+
55+
-- Replacements for seconds
56+
SET @format_internal = REPLACE(@format_internal, 'SS', 'ss');
57+
58+
-- Replacements for day of the week
59+
SET @format_internal = REPLACE(@format_internal, 'EEEE', 'dddd');
60+
SET @format_internal = REPLACE(@format_internal, 'E4', 'dddd');
61+
62+
SET @format_internal = REPLACE(@format_internal, 'EEE', 'ddd');
63+
SET @format_internal = REPLACE(@format_internal, 'E3', 'ddd');
64+
65+
-- Replacements for separator B | b
66+
SET @format_internal = REPLACE(@format_internal, 'B', ' ');
67+
SET @format_internal = REPLACE(@format_internal, 'b', ' ');
68+
69+
SET @date_internal = FORMAT(@date, @format_internal);
70+
71+
RETURN @date_internal;
72+
73+
END
74+
GO

0 commit comments

Comments
 (0)