[22007][Microsoft][ODBC Driver 17 for SQL Server][SQL Server]When converting date and/or time from a string, the conversion failed. (241) Filter illegal date format data

This error occurs because you try to convert an invalid string to a date or time format in SQL Server. To solve this problem, you need to filter out illegal data.

You can use the TRY_CONVERT function to attempt to convert a string to a date or time format, returning NULL if the conversion fails. Then you can use IS NOT NULL to filter out these NULL values. For example:

SELECT *
FROM your_table
WHERE TRY_CONVERT(date, your_column) IS NOT NULL;

This will return all rows that can be successfully converted to date format, while filtering out those that cannot be converted.

If you want more fine-grained control, you can use the TRY_CAST or TRY_PARSE functions to attempt to convert a string to a specific data type. For example:

SELECT *
FROM your_table
WHERE TRY_CAST(your_column AS datetime) IS NOT NULL;

or:

SELECT *
FROM your_table
WHERE TRY_PARSE(your_column AS datetime USING 'en-US') IS NOT NULL;

These functions help you deal with different date and time formats, as well as different languages ​​and locales.

Guess you like

Origin blog.csdn.net/u014421013/article/details/130579013