英文:
DATE_FORMAT and STR_TO_DATE are not working
问题
我正在尝试获取以05/01/2023 04:33:59 AM
格式化的日期中的月份或日期,但似乎这两个函数不起作用。它给我返回了NULL
和0
的结果。
这是我尝试过的脚本。我已经将示例的查询组合在一起。
SELECT
format1.date1, format2.date2
FROM (SELECT DATE_FORMAT(datetime,'%d') AS date1 FROM may2023) format1,
(SELECT DAY(STR_TO_DATE(datetime, '%Y-%m-%d %h:%i:%s %p')) AS date2 FROM `may2023`) format2
有关这个问题的任何建议和帮助?提前感谢。
英文:
I am trying to get the month or day in a date formatted in 05/01/2023 04:33:59 AM
but seems like the two functions are not working. It gives me a NULL
and 0
results.
This is the scripts that I've tried. I have combined the queries for the sample.
SELECT
format1.date1, format2.date2
FROM (SELECT DATE_FORMAT(datetime,'%d') AS date1 FROM may2023) format1,
(SELECT DAY(STR_TO_DATE(datetime, "%Y")) AS date2 FROM `may2023`) format2
Any advice and help for this one? Thanks in advance.
答案1
得分: 0
CREATE TABLE may2023 (
datetime VARCHAR(50)
);
INSERT INTO may2023 (datetime)
VALUES ('05/01/2023 04:33:59 AM');
SELECT
DATE_FORMAT(STR_TO_DATE(datetime, '%m/%d/%Y %h:%i:%s %p'), '%m') AS 月份,
DATE_FORMAT(STR_TO_DATE(datetime, '%m/%d/%Y %h:%i:%s %p'), '%d') AS 日
FROM may2023;
英文:
Are you looking for something like that?
CREATE TABLE may2023 (
datetime VARCHAR(50)
);
INSERT INTO may2023 (datetime)
VALUES ('05/01/2023 04:33:59 AM');
SELECT
DATE_FORMAT(STR_TO_DATE(datetime, '%m/%d/%Y %h:%i:%s %p'), '%m') AS month,
DATE_FORMAT(STR_TO_DATE(datetime, '%m/%d/%Y %h:%i:%s %p'), '%d') AS day
FROM may2023;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论