在我的
MySQL数据库表中,我需要使用列sHour(varchar)的值更新列sDate(Date).
我试过这个SQL查询:
UPDATE IGNORE `doTable`
SET `sDate` = STR_TO_DATE(`sHour`, '%d-%M-%Y')
WHERE
`sDate` IS NULL;
输出是:
+-----------+------------+
| sHour | sDate |
+-----------+------------+
| 01-ago-18 | NULL |
| 01-apr-18 | 2018-04-01 |
| 01-feb-18 | 2018-02-01 |
| 01-gen-18 | NULL |
| 01-giu-18 | NULL |
| 01-mar-18 | 2018-03-01 |
| 01-nov-18 | 2018-11-01 |
| 01-ott-18 | NULL |
| 01-set-18 | NULL |
| 02-ago-18 | NULL |
+-----------+------------+
10 rows in set
问题是当行sHour不包含英文的月份名称(en_US)时.
例如行:
+-----------+------------+
| sHour | sDate |
+-----------+------------+
| 01-ago-18 | NULL |
+-----------+------------+
不更新,因为月份名称(前三个字母)是前(it_IT)而不是八(en_US).
而是行:
+-----------+------------+
| sHour | sDate |
+-----------+------------+
| 01-apr-18 | 2018-04-01 |
+-----------+------------+
更新是因为月份名称-apr-(前三个字母)与意大利语和英语一致.
我尝试过这样的成功:
SET lc_time_names = 'it_IT';
怎么解决这个?
最佳答案 在MYSQL中尝试这个.
我希望我能提供帮助.
UPDATE `doTable`
SET `sDate` = NULL;
UPDATE IGNORE `doTable`
SET `sDate` = STR_TO_DATE(`sHour`, '%d-%M-%Y')
WHERE
`sDate` IS NULL;
UPDATE `doTable` jjj
JOIN (
SELECT
STR_TO_DATE(`sMonth`, '%d-%M-%Y') AS `xDate`
FROM
(
SELECT
CASE
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('ago') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'aug',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('dic') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'dec',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('gen') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'jan',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('giu') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'jun',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('ott') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'oct',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('set') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'sep',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('lug') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'jul',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
WHEN SUBSTRING_INDEX(
SUBSTRING_INDEX(`sHour`, '-' ,- 2),
'-',
1
) IN ('mag') THEN
CONCAT(
SUBSTRING_INDEX(`sHour`, '-', 1),
'-',
'may',
'-',
SUBSTRING_INDEX(`sHour`, '-' ,- 1)
)
END AS `sMonth`
FROM
`doTable`
WHERE
`sDate` IS NULL
) AS ttt
) AS kkk
SET jjj.`sDate` = kkk.`xDate`
WHERE
jjj.`sDate` IS NULL;