简体   繁体   English

SQL 如何将 select 中的时间戳转换为日期时间?

[英]SQL How do I convert timestamp to datetime within the select?

I have table where there are columns username, visit and timestamp.我有一个表,其中包含用户名、访问和时间戳列。 I select user by name and visit.我按用户名select访问。 But the result of query must contain: username, visit, datetime.但查询结果必须包含:用户名、访问、日期时间。

How do I go from here:我如何从这里拨打 go:

select username, visit, timestamp
from Table
where username like "Jane" and visit like "cafe"

to the result with date?到日期的结果?

Thanks a lot非常感谢

You can convert the timestamp to a date using the DATE() function in SQL. The following query should give you the desired result:您可以使用 SQL 中的DATE() function 将时间戳转换为日期。以下查询应该会为您提供所需的结果:

select username, visit, DATE(timestamp) as datetime
from Table
where username like "Jane" and visit like "cafe"

This query selects the username, visit, and timestamp columns from the Table, and also includes an additional column, "datetime", which is the result of applying the DATE() function to the timestamp column.此查询从表中选择用户名、访问和时间戳列,还包括一个附加列“日期时间”,这是将DATE() function 应用于时间戳列的结果。 This function extracts the date part of the timestamp and formats it as a date.这个 function 提取时间戳的日期部分并将其格式化为日期。

If you want to format the datetime in a specific format you could use DATE_FORMAT() function like this:如果您想以特定格式格式化日期时间,您可以像这样使用DATE_FORMAT() function:

select username, visit, DATE_FORMAT(timestamp, '%Y-%m-%d') as datetime
from Table
where username like "Jane" and visit like "cafe"

This will format the datetime to YYYY-MM-DD format.这会将日期时间格式化为YYYY-MM-DD格式。

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

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