2

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!

Sebastian
  • 831
  • 2
  • 13
  • 36

1 Answers1

6

Use SAFE. prefix for safe function calls.

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

enter image description here

Jaytiger
  • 11,626
  • 2
  • 5
  • 15