简体   繁体   English

如何在SQL中将字符串日期转换为日期时间类型

[英]How to convert string date to datetime type in SQL

I am having old database and in those tables every entry maintains create_at abd updated_at entries, 我有旧的数据库,在那些表中,每个条目都维护create_at abd updated_at条目,

But this entries are in varchar format eg: 24/04/2018 14:25:29 pm 但是此条目采用varchar格式,例如: 24/04/2018 14:25:29 pm

How to I convert this to datetime so I can fire query and get count of records of each month. 如何将其转换为日期时间,以便可以进行查询并获取每个月的记录数。

Query will be something like this: 查询将是这样的:

SELECT *
FROM table
WHERE MONTH(columnName) = MONTH(CURRENT_DATE())
AND YEAR(columnName) = YEAR(CURRENT_DATE())   

MySQL: MySQL:

Use STR_TO_DATE function to convert into datetime 使用STR_TO_DATE函数转换为datetime

Like this: 像这样:

SELECT *
FROM table
WHERE MONTH(STR_TO_DATE(columnName,'%d%m%Y')) = MONTH(STR_TO_DATE(columnName,'%d%m%Y'))
AND YEAR(STR_TO_DATE(columnName,'%d%m%Y')) = YEAR(STR_TO_DATE(columnName,'%d%m%Y')) 

Demo: 演示:

http://sqlfiddle.com/#!9/94c4b/26 http://sqlfiddle.com/#!9/94c4b/26

Explanation: 说明:

STR_TO_DATE

Syntax: STR_TO_DATE(str,format) 语法: STR_TO_DATE(str,format)

It takes a string str and a format string format. 它采用字符串str和格式字符串格式。
STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. 如果格式字符串同时包含日期和时间部分,则STR_TO_DATE()返回DATETIME值;如果字符串仅包含日期或时间部分,则STR_TO_DATE()返回DATE或TIME值。
If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning. 如果从str提取的日期,时间或日期时间值不合法,则STR_TO_DATE()返回NULL并产生警告。

Specifiers in the format: 格式说明符:

%d - Day of the month, numeric (00..31) %d-每月的某天,数字(00..31)
%m - Month, numeric (00..12) %m-月,数字(00..12)
%Y - Year, numeric, four digits %Y-年,数字,四位数
%T - Time, 24-hour (hh:mm:ss) %T-时间,24小时(hh:mm:ss)

Links To Source: 链接到源:

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date https://dev.mysql.com/doc/refman/8.0/zh-CN/date-and-time-functions.html#function_str-to-date

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format https://dev.mysql.com/doc/refman/8.0/zh-CN/date-and-time-functions.html#function_date-format

Just use substr() or substring() (depending on your database): 只需使用substr()substring() (取决于您的数据库):

SELECT *
FROM table
WHERE substring(columnName, 4, 2) = MONTH(CURRENT_DATE()) AND
      substring(columnName, 7, 4) = YEAR(columnName);

I would then strongly advise you to change the format of the date/time. 然后,我强烈建议您更改日期/时间的格式。 The exact logic depends on the database, but in general, you can do: 确切的逻辑取决于数据库,但是通常可以执行以下操作:

update table
    set columnName = <convert columnname to date/time>;  -- converts to a date and then back to a string using "default" format

alter table <table> alter columnName datetime;  -- "datetime" is probably the appropriate type, but that depends on the database

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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