Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to safely convert strings to date?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

Use SAFE. prefix for safe function calls.

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading