Validate the datetime value: Difference between revisions
Jump to navigation
Jump to search
m (→MySQL approach) |
|||
| Line 4: | Line 4: | ||
== MySQL approach == | == MySQL approach == | ||
=== Verify the value is not 0000-00-00 00:00:00 === | |||
Schema of column value | |||
* Type: datetime | |||
* Default value: 0000-00-00 00:00:00 | |||
<pre> | |||
SELECT `my_date_column` | |||
FROM `my_table` | |||
WHERE `my_date_column` LIKE '0000-00-00 00:00:00'; | |||
</pre> | |||
=== Verify the value is null === | |||
Verify the value of {{kbd | key=DAY()}}<ref>[https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions]</ref> is NULL | Verify the value of {{kbd | key=DAY()}}<ref>[https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions]</ref> is NULL | ||
<pre> | <pre> | ||
| Line 19: | Line 33: | ||
</pre> | </pre> | ||
Verify the value should be year/month/day format | === Verify the value should be year/month/day format === | ||
<pre> | <pre> | ||
SELECT `my_date_column`, STR_TO_DATE(`my_date_column`, '%Y/%m/%d') | SELECT `my_date_column`, STR_TO_DATE(`my_date_column`, '%Y/%m/%d') | ||
| Line 28: | Line 42: | ||
</pre> | </pre> | ||
Verify the value should be hour:minute:second format e.g. {{CURRENTTIME}}:06 | === Verify the value should be hour:minute:second format === | ||
e.g. {{CURRENTTIME}}:06 | |||
<pre> | <pre> | ||
SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%H:%i:%S') | SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%H:%i:%S') | ||
| Line 37: | Line 52: | ||
</pre> | </pre> | ||
Verify the value should be year-month-day hour:minute:second format e.g. {{Template:Today}} {{CURRENTTIME}}:06 | === Verify the value should be year-month-day hour:minute:second format === | ||
e.g. {{Template:Today}} {{CURRENTTIME}}:06 | |||
<pre> | <pre> | ||
SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%Y-%m-%d %H:%i:%S') | SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%Y-%m-%d %H:%i:%S') | ||
| Line 47: | Line 63: | ||
Verify the value not start with four number, which means year | === Verify the value not start with four number, which means year === | ||
{{exclaim}} Not recommended! | |||
<pre> | <pre> | ||
SELECT `my_date_column` | SELECT `my_date_column` | ||
Revision as of 12:09, 25 April 2022
Validate the value should be well-formated value e.g. year-month-day 2026-05-06
MySQL approach
Verify the value is not 0000-00-00 00:00:00
Schema of column value
- Type: datetime
- Default value: 0000-00-00 00:00:00
SELECT `my_date_column` FROM `my_table` WHERE `my_date_column` LIKE '0000-00-00 00:00:00';
Verify the value is null
Verify the value of DAY()[1] is NULL
SELECT `my_date_column` FROM `my_table` WHERE DAY(`my_date_column`) IS NULL;
SELECT `my_date_column`, STR_TO_DATE(`my_date_column`, '%Y-%m-%d') FROM `my_table` WHERE STR_TO_DATE(`my_date_column`, '%Y-%m-%d') IS NULL;
Verify the value should be year/month/day format
SELECT `my_date_column`, STR_TO_DATE(`my_date_column`, '%Y/%m/%d') FROM `my_table` WHERE STR_TO_DATE(`my_date_column`, '%Y/%m/%d') IS NULL;
Verify the value should be hour:minute:second format
e.g. 17:12:06
SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%H:%i:%S') FROM `my_table` WHERE STR_TO_DATE(`my_time_column`, '%H:%i:%S') IS NULL;
Verify the value should be year-month-day hour:minute:second format
e.g. 2026-05-06 17:12:06
SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%Y-%m-%d %H:%i:%S') FROM `my_table` WHERE STR_TO_DATE(`my_time_column`, '%Y-%m-%d %H:%i:%S') IS NULL;
Verify the value not start with four number, which means year
Not recommended!
SELECT `my_date_column`
FROM `my_table`
WHERE `my_date_column` NOT REGEXP '^[0-9]{4}';
PHP appracoh
Using PHP strtotime function
Excel approach
- Validate the total length of cell value is equal to 10 e.g. =IF(LEN(B2)<>10, TRUE, FALSE)
- Validate the positions of the - symbols
- Combined the above condition, the verify formula is: (If the cell value is well-formatted, the formula returns true)
=NOT(
IF(OR(
ISERROR(FIND("-", B2, 1)),
ISERROR(FIND("-", B2, 6)),
IF(LEN(B2)<>10, TRUE, FALSE)
), TRUE, FALSE)
)