Home > Back-end >  How to safely convert strings to date?
How to safely convert strings to date?

Time:12-08

I'm using the DATE function to get the date from three variables for year, month and date, like this:

DATE(year_var, month_var, day_var) AS date_ymd

However, occasionally this would result in an invalid date, e.g., month_var may be 11 and day_var 31. In these cases DATE would fail, as 2022-11-31 is not a valid date.

Instead, I would need to have NULL as the result .

I'm looking for something like SAFE_CAST, but for dates.

Any hints are greatly appreciated!

CodePudding user response:

Use SAFE. prefix for safe function calls.

SELECT SAFE.DATE(2022, 11, 31) AS date_ymd;

enter image description here

  • Related