3

mysql如何判断某些日期不合法

 1 year ago
source link: https://blog.51cto.com/u_15869608/6003563
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

mysql如何判断某些日期不合法

精选 原创

在系统集成或从其它表里面转存时间字段,用字符串存储时间字段时,可能有一些不合法数据导致系统运行报错。而由于数据量较多无法用肉眼拍错,这时候就需要用sql语句筛选出不合法的数据。

如图下图中有一字段st_to_date是varchar类型的字段我们在里面存一些合法不合法的数据。

mysql如何判断某些日期不合法_字段

请从表里面筛选出不是这种1986-06-26数据类型的数据,不能转换成时间类型的也筛出来。也就是主键为10001和10002的数据,假设数据很多有几万条。

select emp_no,str_to_date(st_date,'%Y-%m-%d') as date from dept_emp

我们可以用字符串转日期函数看一下转换结果。当不能转换时结果为空因此就可以很轻易的得出筛选条件

mysql如何判断某些日期不合法_字符串_02

执行完在条sql就能得到想要的数据

select emp_no,str_to_date(st_date,'%Y-%m-%d') as cc from dept_emp where str_to_date(st_date,'%Y-%m-%d') is null
mysql如何判断某些日期不合法_字段_03

我们可以用其它类型的时间字段测试一下比如说用20220208在种时间格式实验一下。

select emp_no,str_to_date(st_date,'%Y%m%d') as cc from dept_emp where str_to_date(st_date,'%Y%m%d') is null
mysql如何判断某些日期不合法_字符串_04

OK效果很好完美收工。记得要注意调一下str_to_date(st_date,'%Y-%m-%d')函数里的时间格式。

求点赞                 2023-01-12 9:09分编写完成。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK