1. You need to look at the mapping type that contains the _date field. If it is a date, you need to format it. If it is not a direct string, just store it in.
DATE_FORMAT(a.IN_OUT_DATE, '%Y-%m-%d') as IN_OUT_DATE,
sql also needs to verify that the _date field is included and the es is also of date type, whether they are all formatted
2. The same is true for fields containing time, provided that the mapping is also of date type
UNIX_TIMESTAMP(DATE_FORMAT(a.AUDIT_TIME, '%Y-%m-%d %H:%i:%s')) * 1000 as AUDIT_TIME,
**
The field containing 'date' is converted by DATE_FORMAT, only year, month, day is required, and the field containing time requires year, month, day, hour, minute, and second! !
**
Solution idea: Format the detected string
How to convert the 20180116120000 detected by MySQL into 2018-01-16
is a common requirement. We usually use the DATE or DATETIME function in MySQL to convert the string format into a date or datetime format. However, your numbers don't conform to the standard format for direct conversion by MySQL. You need to convert it to a standard date format string first.
Assuming this number exists my_column
in a column named in table , my_table
you can use the following SQL query to achieve this:
SELECT DATE_FORMAT(STR_TO_DATE(my_column, '%Y%m%d%H%i%s'), '%Y-%m-%d') as formatted_date
FROM my_table;
The way this query works is:
- Use
STR_TO_DATE
a function to convert your number to a datetime value. '%Y%m%d%H%i%s' is a format string that tells MySQL how to parse numbers. In this example, '%Y' is the four-digit year, '%m' is the month, '%d' is the day, '%H' is the hour, '%i' is the minute, and '%s' is the second. - Then, use
DATE_FORMAT
the function to convert the datetime value to the format you need, '%Y-%m-%d', where '%Y' represents the four-digit year, '%m' represents the month, and '%d' represents the date .
Note that this query assumes that your numbers always represent valid dates and times. If this is not the case, you may need more sophisticated handling.
However, it is best to keep the hours, minutes and seconds
If you need to keep the time part, you can simply adjust DATE_FORMAT
the function's format string to include hours, minutes, and seconds. For example, if you need the format '2018-01-16 12:00:00', you can use the following query:
SELECT DATE_FORMAT(STR_TO_DATE(CRT_TIME, '%Y%m%d%H%i%s'), '%Y-%m-%d %H:%i:%s') as formatted_date
FROM tbl_ps_device_info;
In this query, '%Y-%m-%d %H:%i:%s'
is a format string where %Y
represents the four-digit year, %m
represents the month, %d
represents the date, %H
represents the hour, %i
represents the minute, and %s
represents the second. Therefore, DATE_FORMAT
the function converts datetime values to this format.
For example:
check the CRT_TIME field from the tbl_ps_device_info table
If you want to tbl_ps_device_info
query a field from a table CRT_TIME
, and convert it to a date format (like '2018-01-16'), you can use a query like:
SELECT DATE_FORMAT(STR_TO_DATE(CRT_TIME, '%Y%m%d%H%i%s'), '%Y-%m-%d') as formatted_date
FROM tbl_ps_device_info;
This query will tbl_ps_device_info
fetch all CRT_TIME
the records from the table, then use STR_TO_DATE
the function to convert them to datetime, then use DATE_FORMAT
the function to convert the datetime to the date format you need.
Again, this query is assumed CRT_TIME
to always represent a valid date and time. If it is possible to have invalid datetimes, you may need more complex handling, such as using IF
a function to check STR_TO_DATE
if the result is NULL
.
Finally written as complete SQL:
SELECT
info.DEVICE_NO as _id,
info.DEVICE_NO,
info.DEVICE_NAME,
info.DEVICE_TYPE,
info.PARK_NO,
info.CHNO_NO,
info.IF_CONTROL,
info.CONTROLTIMES,
info.STATUS,
info.STATUS_CHANGE_TIME,
DATE_FORMAT(STR_TO_DATE(info.CRT_TIME, '%Y%m%d%H%i%s'), '%Y-%m-%d %H:%i:%s') as CRT_TIME,
info.UPD_TIME,
info.OPR_FLAG,
info.ITEMID,
info.DELETED,
info.INOUTTYPE,
info.IF_DOWN,
info.SUB_DEVICE_TYPE,
info.ORG_ID
FROM
tbl_ps_device_info info
--------
#注意最后的分号也不能写,不然会拼接where条件错误