从Query中返回的DateTime值中删除时间

时间:2013-08-22 14:04:33

标签: sql sql-server-2008 tsql datetime

在这里你们的帮助下,我有以下SQL查询:

SELECT DataLog.TimestampUTC,MeterTags.Name,DataLog.Data
FROM DataLog
INNER JOIN MeterTags
ON DataLog.MeterTagId = MeterTags.MeterTagId
WHERE DataLog.TimeStampUTC between cast(getdate() - 1 as date) and cast(getdate() as date) and
      DataLog.MeterTagId Between 416 AND 462;

这将返回一列“TimestampUTC”,其中包含YYYY-MM-DD hh:mm:ss。我想把时间放在这个栏目中,只显示YYYY-MM-DD。

您可以给予的任何帮助都会非常感激。

提前致谢。

3 个答案:

答案 0 :(得分:2)

SELECT convert(char(10), DataLog.TimestampUTC, 120) as TimestampUTC, 
MeterTags.Name,DataLog.Data
FROM DataLog
INNER JOIN MeterTags
ON DataLog.MeterTagId = MeterTags.MeterTagId
WHERE DataLog.TimeStampUTC between cast(getdate() - 1 as date) and cast(getdate() as date) and
      DataLog.MeterTagId Between 416 AND 462;

答案 1 :(得分:1)

在SQL Server中,您可以转换为日期

select Convert(Date, Convert(datetime, '2013/01/01 12:53:45'))

结果:

2013-01-01

答案 2 :(得分:0)

你应该使用: CAST(DataLog.TimestampUTC as date)

代码如下:

SELECT CAST(DataLog.TimestampUTC as date),MeterTags.Name,DataLog.Data
FROM DataLog
INNER JOIN MeterTags
ON DataLog.MeterTagId = MeterTags.MeterTagId
WHERE DataLog.TimeStampUTC between cast(getdate() - 1 as date) and cast(getdate() as date) and
      DataLog.MeterTagId Between 416 AND 462;