TSQL-specific CONVERT() can't be parsed
See original GitHub issueExample:
print(sqlglot.parse_one("SELECT convert([varchar](10),convert([datetime],[My Date],105),23)"))
According to the docs, this means the following:
- Convert
[My Date]
to datetime, using date formatdd-mm-yyyy
. - 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 producesexp.Cast
, but the semantics of the example actually meanexp.StrToTime
andexp.TimeToStr
.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.