简体   繁体   中英

MySQL: SQL Query to return Date from Date/Timestamp Column

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.

Any ideas?

If the column type is char or varchar , then

SELECT LEFT(colname, 10)

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

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

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.

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, ...).

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

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

i think you can use substring !!!

SELECT SUBSTRING(TimeStamp,7,10);

如果它是字符串,则使用SUBSRT()LEFT()提取所需部分,但是将其存储为datatime类型是明智的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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