简体   繁体   中英

How to check if date stored in varchar field is a valid date or not in where clause of mysql query?

I want to update data in mysql table on basis of date field value.

Table consist of four fields (id, file_id, date_of_purchase, error). date_of_purchase and error are varchar fields.

Now i want to update error field if value in date is not a valid date. I tried with STR_TO_DATE() function but it is not working in where clause in update query

Here is my query

UPDATE `temp_bulk_upload_data` SET `error`= 'Date of purchase: Date of purchase should be a valid date' WHERE `file_id`='190' AND STR_TO_DATE(`date_of_purchase`, '%m/%d/%Y') IS NULL AND (`date_of_purchase` != '' OR `date_of_purchase` IS NOT NULL)

In this query i am getting this error:

Error in query (1411): Incorrect datetime value: '27/8/2018' for function str_to_date

Use Regular expression in this case. Query

WHERE `date_of_purchase` NOT REGEXP '^(0[1-9]|1[0-2])\\/(0[1-9]|[1-2][0-9]|3[0-1])\\/[0-9]{4}$'

STR_TO_DATE传递的STR_TO_DATE应为mmddYYYY格式或更新STR_TO_DATE函数的第二个参数以接受ddmmYYYY格式的日期值。

UPDATE `temp_bulk_upload_data` SET `error`= 'Date of purchase: Date of purchase should be a valid date' WHERE `file_id`='190' AND STR_TO_DATE(`date_of_purchase`, '%d/%m/%Y') IS NULL AND (`date_of_purchase` != '' OR `date_of_purchase` IS NOT NULL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM