Home  >  Article  >  Database  >  Validate date in MySQL using custom function

Validate date in MySQL using custom function

PHPz
PHPzforward
2023-09-10 16:33:03721browse

使用自定义函数验证 MySQL 中的日期

Let’s create a custom function to validate date in MySQL -

mysql> set global log_bin_trust_function_creators=1;
Query OK, 0 rows affected (0.03 sec)
mysql> delimiter //
mysql> create function isValidDate(actualDate varchar(255)) returns int
   -> begin
   -> declare flag int;
   -> if (select length(date(actualDate)) IS NOT NULL ) then
   -> set flag = 1;
   -> else
   -> set flag = 0;
   -> end if;
   -> return flag;
   -> end
   -> //
Query OK, 0 rows affected (0.11 sec)
mysql> delimiter ;

Case 1 −

When the parameters When it is a null value, the date to be checked is not the actual date. Calling the function using a SELECT statement −

mysql> select isValidDate(NULL);

This will produce the following output which is not a date -

+-------------------+
| isValidDate(NULL) |
+-------------------+
| 0                 |
+-------------------+
1 row in set (0.05 sec)

Case 2 -

When the argument is an actual date value time. Calling the function using a SELECT statement -

mysql> select isValidDate('2019-10-21');

This will produce the following output i.e. 1, actual date -

+---------------------------+
| isValidDate('2019-10-21') |
+---------------------------+
| 1                         |
+---------------------------+
1 row in set (0.00 sec)

The above is the detailed content of Validate date in MySQL using custom function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete