Repository: tobymao/sqlglot
Issue category: Bug Report

Issue:
TSQL fails on parsing DATEDIFF function with literals
Hello,
I just discovered that this code generates an error while parsing DATEDIFF second argument:
```
import sqlglot
statement = """
SELECT DATEADD(DAY, DATEDIFF(DAY, -3, CREATION_TIME_NEW), '08:00:00')
FROM y
"""

ast = sqlglot.parse_one(statement, 'tsql')
print(ast.sql('tsql'))
```

Error:
```
Python312\Lib\site-packages\sqlglot\dialects\tsql.py:235, in _build_date_delta.<locals>._builder(args)

    232 if start_date and start_date.is_number:
    233     # Numeric types are valid DATETIME values
    234     if start_date.is_int:
--> 235         adds = DEFAULT_START_DATE + datetime.timedelta(days=int(start_date.this))
    236         start_date = exp.Literal.string(adds.strftime("%F"))
    237     else:
    238         # We currently don't handle float values, i.e. they're not converted to equivalent DATETIMEs.
    239         # This is not a problem when generating T-SQL code, it is when transpiling to other dialects.

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'Literal'
```
start_date at this point is:
```
Neg(
  this=Literal(this=3, is_string=False))
```

So `int(start_date.this)` fails.


Hints:

Patch:
diff --git a/sqlglot/dialects/tsql.py b/sqlglot/dialects/tsql.py
index 856142edc4..1acc5ca8a7 100644
--- a/sqlglot/dialects/tsql.py
+++ b/sqlglot/dialects/tsql.py
@@ -232,7 +232,7 @@ def _builder(args: t.List) -> E:
         if start_date and start_date.is_number:
             # Numeric types are valid DATETIME values
             if start_date.is_int:
-                adds = DEFAULT_START_DATE + datetime.timedelta(days=int(start_date.this))
+                adds = DEFAULT_START_DATE + datetime.timedelta(days=start_date.to_py())
                 start_date = exp.Literal.string(adds.strftime("%F"))
             else:
                 # We currently don't handle float values, i.e. they're not converted to equivalent DATETIMEs.


Test patch:
diff --git a/tests/dialects/test_tsql.py b/tests/dialects/test_tsql.py
index 61365994bd..e8cd69648b 100644
--- a/tests/dialects/test_tsql.py
+++ b/tests/dialects/test_tsql.py
@@ -1579,6 +1579,11 @@ def test_date_diff(self):
             },
         )
 
+        self.validate_identity(
+            "SELECT DATEADD(DAY, DATEDIFF(DAY, -3, GETDATE()), '08:00:00')",
+            "SELECT DATEADD(DAY, DATEDIFF(DAY, CAST('1899-12-29' AS DATETIME2), CAST(GETDATE() AS DATETIME2)), '08:00:00')",
+        )
+
     def test_lateral_subquery(self):
         self.validate_all(
             "SELECT x.a, x.b, t.v, t.y FROM x CROSS APPLY (SELECT v, y FROM t) t(v, y)",