MySQL: SQL查询从日期/时间戳列返回日期。

时间:2021-04-12 16:23:03

In a table I have a stored string column "MM/DD/YYYY HH:MM:SS" and I'm looking for a query to return just the "MM/DD/YYYY" part.

在一个表中,我有一个存储的字符串列“MM/DD/ yyyyyyyyyyyyyyyyyyyyyyyyyyhh:”,我正在寻找一个查询来返回“MM/DD/YYYY”部分。

Any ideas?

什么好主意吗?

4 个解决方案

#1


3  

If the column type is char or varchar, then

如果列类型是char或varchar,则。

SELECT LEFT(colname, 10)

选择离开(colname,10)

will suffice. If it's a datetime type, then try

就足够了。如果它是datetime类型,那么尝试。

SELECT DATE_FORMAT(colname , "%d/%m/%Y")

选择DATE_FORMAT(colname,“% d / Y % m / %)

#2


1  

You should be using a MySQL DATETIME field instead of a string field, really. That would allow you to apply date and time functions, which help tremendously when dealing with temporal data.

您应该使用一个MySQL DATETIME字段,而不是字符串字段。这将允许您应用日期和时间函数,这在处理时态数据时非常有用。

In your case, since your data is a string type, and not in MySQL Isodate format (YYYY-MM-DD), you can work only using string functions like SUBSTRING() and specialisations thereof (LEFT, ...).

在您的例子中,由于您的数据是字符串类型,而不是MySQL Isodate格式(YYYY-MM-DD),您只能使用字符串函数,比如SUBSTRING()和专门化(LEFT,…)。

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html Date and Time functions overview

html日期和时间功能概述。

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html String functions

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html字符串函数

#3


0  

i think you can use substring !!!

我想你可以用substring !!!

SELECT SUBSTRING(TimeStamp,7,10);

选择子串(时间戳,7,10);

#4


0  

if it is a string use SUBSRT() or LEFT() to extract required part, however it would be wise to have it stored as datatime type

如果是一个字符串使用SUBSRT()或LEFT()来提取所需的部分,那么将它存储为datatime类型是明智的。

#1


3  

If the column type is char or varchar, then

如果列类型是char或varchar,则。

SELECT LEFT(colname, 10)

选择离开(colname,10)

will suffice. If it's a datetime type, then try

就足够了。如果它是datetime类型,那么尝试。

SELECT DATE_FORMAT(colname , "%d/%m/%Y")

选择DATE_FORMAT(colname,“% d / Y % m / %)

#2


1  

You should be using a MySQL DATETIME field instead of a string field, really. That would allow you to apply date and time functions, which help tremendously when dealing with temporal data.

您应该使用一个MySQL DATETIME字段,而不是字符串字段。这将允许您应用日期和时间函数,这在处理时态数据时非常有用。

In your case, since your data is a string type, and not in MySQL Isodate format (YYYY-MM-DD), you can work only using string functions like SUBSTRING() and specialisations thereof (LEFT, ...).

在您的例子中,由于您的数据是字符串类型,而不是MySQL Isodate格式(YYYY-MM-DD),您只能使用字符串函数,比如SUBSTRING()和专门化(LEFT,…)。

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html Date and Time functions overview

html日期和时间功能概述。

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html String functions

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html字符串函数

#3


0  

i think you can use substring !!!

我想你可以用substring !!!

SELECT SUBSTRING(TimeStamp,7,10);

选择子串(时间戳,7,10);

#4


0  

if it is a string use SUBSRT() or LEFT() to extract required part, however it would be wise to have it stored as datatime type

如果是一个字符串使用SUBSRT()或LEFT()来提取所需的部分,那么将它存储为datatime类型是明智的。