question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TSQL-specific CONVERT() can't be parsed

See original GitHub issue

Example:

print(sqlglot.parse_one("SELECT convert([varchar](10),convert([datetime],[My Date],105),23)"))

According to the docs, this means the following:

  1. Convert [My Date] to datetime, using date format dd-mm-yyyy.
  2. Convert the result to varchar of length 10, using date format yyyy-mm-dd.

This issue has multiple aspects:

  • sqlglot doesn’t seem to allow quoted type names but they appear to be accepted by real-world DBMSs (this example works in TSQL; select '2022-09-27'::"date" works in Postgres)
  • the parser for CONVERT() expects MySQL semantics (convert(value, type)), but TSQL inverts the order of parameters (convert(type, value))
  • _parse_convert always produces exp.Cast, but the semantics of the example actually mean exp.StrToTime and exp.TimeToStr.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tobymaocommented, Sep 27, 2022

given that convert can do weird stuff like money and xml styles, i think for tsql it needs to stay convert, but i don’t have the bandwidth to add this. again happy to help with a PR though.

1reaction
tobymaocommented, Sep 27, 2022

i’m not going to support quoted type names for now. although it may be legal, it’s quite strange. identifiers are used for finding columns and tables, using them for existing types doesn’t make much sense.

the convert has been implemented

parse_convert should produce a cast since it’s equivalent, strtotime and timetostr are the implicit implementations of varchar to date.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding SQL Server's TRY_PARSE and ... - SQLShack
The TRY_Parse function is used to convert string data into numeric or date data types. It returns NULL if the conversion is not...
Read more >
Find Rows in table with a value that cannot convert to int
Starting with SQL Server 2012 you can use the TRY_CAST expression which returns NULL when cast cannot be performed:
Read more >
PARSE (Transact-SQL) - SQL Server - Microsoft Learn
Use PARSE only for converting from string to date/time and number types. For general type conversions, continue to use CAST or CONVERT. Keep...
Read more >
Type Conversion Functions | ClickHouse Docs
When converting to or from a string, the value is formatted or parsed using the same rules as for the TabSeparated format (and...
Read more >
Solved: DataFormat.Error: We couldn't parse the input prov...
DataFormat.Error: We couldn't parse the input provided as a DateTime value · 1. Split the column by 11 characters. · 2. Split the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found