Home > Database > Mysql Tutorial > Validate date in MySQL using custom function

Validate date in MySQL using custom function

PHPz
Release: 2023-09-10 16:33:03
forward
766 people have browsed it

使用自定义函数验证 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 ;
Copy after login

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);
Copy after login

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

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

Case 2 -

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

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

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

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template