Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for NULL or empty variable (MySQL stored procedure)

Tags:

sql

mysql

Can someone indicate the proper syntax to determine whether a variable is NULL or empty and then take appropriate action to set the variable depending on the result.

I currently have a test for NULL which works fine (part of a MySQL SELECT statement / stored procedure).

IFNULL(@previousTs, '2017-00-04 00:00:01') ts

I want to include (in the same single line) a test for empty (NULL or empty both results in @previousTs being set to 2017-00-04 00:00:01).

like image 978
Active Avatar asked Sep 16 '25 21:09

Active


1 Answers

You can try like this:

IF(@previousTs IS NULL or @previousTs= '', '2017-00-04 00:00:01', @previousTs )
like image 158
Rahul Tripathi Avatar answered Sep 19 '25 11:09

Rahul Tripathi