Skip to content

Commit

Permalink
Fix: tsql datepart format casing closes #1869
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 30, 2023
1 parent 0197119 commit f4fb1f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions sqlglot/dialects/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def _format_time(args: t.List) -> E:
assert len(args) == 2

return exp_class(
this=args[1],
this=exp.cast(args[1], "datetime"),
format=exp.Literal.string(
format_time(
args[0].name,
args[0].name.lower(),
{**TSQL.TIME_MAPPING, **FULL_FORMAT_TIME_MAPPING}
if full_format_mapping
else TSQL.TIME_MAPPING,
Expand Down
13 changes: 7 additions & 6 deletions tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,20 @@ def test_datefromparts(self):

def test_datename(self):
self.validate_all(
"SELECT DATENAME(mm,'01-01-1970')",
write={"spark": "SELECT DATE_FORMAT('01-01-1970', 'MMMM')"},
"SELECT DATENAME(mm,'1970-01-01')",
write={"spark": "SELECT DATE_FORMAT(CAST('1970-01-01' AS TIMESTAMP), 'MMMM')"},
)
self.validate_all(
"SELECT DATENAME(dw,'01-01-1970')",
write={"spark": "SELECT DATE_FORMAT('01-01-1970', 'EEEE')"},
"SELECT DATENAME(dw,'1970-01-01')",
write={"spark": "SELECT DATE_FORMAT(CAST('1970-01-01' AS TIMESTAMP), 'EEEE')"},
)

def test_datepart(self):
self.validate_all(
"SELECT DATEPART(month,'01-01-1970')",
write={"spark": "SELECT DATE_FORMAT('01-01-1970', 'MM')"},
"SELECT DATEPART(month,'1970-01-01')",
write={"spark": "SELECT DATE_FORMAT(CAST('1970-01-01' AS TIMESTAMP), 'MM')"},
)
self.validate_identity("DATEPART(YEAR, x)", "FORMAT(CAST(x AS DATETIME2), 'yyyy')")

def test_convert_date_format(self):
self.validate_all(
Expand Down

0 comments on commit f4fb1f4

Please sign in to comment.