简体   繁体   English

将时间从{yyyy-mm-dd hh:mm:ss}转换为{yyyy-mm-dd hh}

[英]CONVERT Time from {yyyy-mm-dd hh:mm:ss} to {yyyy-mm-dd hh}

I am trying to extract data from a large SQL DB on hourly basis. 我试图每小时从大型SQL DB中提取数据。 The following query is giving me a grouping on minute basis. 以下查询为我提供了按分钟分组的信息。 I wish to group by the data on hourly basis. 我希望按小时对数据进行分组。

select CONVERT(VARCHAR(20), t.counterTime, 100) as [Time],
    t.instanceName
from table as t
WHERE t.counterId= @counterId
and t.counterTime >=  @startTime
and t.counterTime <  @endTime
group by t.instanceName, CONVERT(VARCHAR(20), t.counterTime, 100)

The documentation also does not provide any such option. 文档也未提供任何此类选项。 Any ideas? 有任何想法吗?

Try like this, 这样尝试

--SQL Server 2012
SELECT FORMAT(t.counterTime, 'yyyy-MM-dd HH') AS [Time]
    ,t.instanceName
FROM TABLE AS t
WHERE t.counterId = @counterId
    AND t.counterTime >= @startTime
    AND t.counterTime < @endTime
GROUP BY t.instanceName
    ,FORMAT(t.counterTime, 'yyyy-MM-dd HH')

--for earlier versions
SELECT CONVERT(CHAR(13), t.counterTime, 120) AS [Time]
    ,t.instanceName
FROM TABLE AS t
WHERE t.counterId = @counterId
    AND t.counterTime >= @startTime
    AND t.counterTime < @endTime
GROUP BY t.instanceName
    ,CONVERT(CHAR(13), t.counterTime, 120)

Note: Format would be perfoming slower.

您可以使用以下子字符串:

select substring(field,1,4) + '-'+ substring(field,6,2) +'-'+ substring(field,7,2)+' '+ substring(field,9,2)  from dbo.youtable 

Try this 尝试这个

select datepart(hour,t.counterTime) as [Time],
    t.instanceName
from table as t
WHERE t.counterId= @counterId
and t.counterTime >=  @startTime
and t.counterTime <  @endTime
group by t.instanceName, datepart(hour,t.counterTime) 

暂无
暂无

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

相关问题 将日期时间“ YYYY-MM-DD HH:mm:ss”转换为“ DD / MM / YYYY HH:mm:ss” - convert Datetime 'YYYY-MM-DD HH:mm:ss' to 'DD/MM/YYYY HH:mm:ss' pyspark sql 将日期格式从 mm/dd/yy hh:mm 或 yyyy-mm-dd hh:mm:ss 转换为 yyyy-mm-dd hh:mm 格式 - pyspark sql convert date format from mm/dd/yy hh:mm or yyyy-mm-dd hh:mm:ss into yyyy-mm-dd hh:mm format 在yql中将yyyy-mm-dd hh:mm:ss转换为yyyy-mm-dd - convert yyyy-mm-dd hh:mm:ss to yyyy-mm-dd in sql 如何在雪花中将 YYYY-MM-DD HH:MM:SS:MS 转换为 MM/DD/YYYY - How to convert YYYY-MM-DD HH:MM:SS:MS to MM/DD/YYYY in snowflake 尝试以(yyyy-mm-dd hh:mm:ss)日期格式转换字符串格式(dd-mm-yyyy hh:mm:ss) - Trying to convert string format (dd-mm-yyyy hh:mm:ss) in (yyyy-mm-dd hh:mm:ss) date format 如何将mm / dd / yyyy格式的日期转换为yyyy-mm-dd hh:mi:ss.mmm - How to convert date in mm/dd/yyyy format to yyyy-mm-dd hh:mi:ss.mmm 在SQL中以YYYY-MM-DD HH:MM:SS格式转换日期和时间字符串 - Convert a date and time string in the format YYYY-MM-DD HH:MM:SS in SQL 在雅典娜中将 yyyymm 字符串转换为 yyyy-mm-dd hh:mm:ss 日期时间 - Convert yyyymm String to yyyy-mm-dd hh:mm:ss date time in athena 以yyyy-MM-dd HH:mm:ss“格式转换日期时间 - Convert the date time in yyyy-MM-dd HH:mm:ss" format 如何将 yyyy-mm-dd hh:MM:SS.mil 形式的时间戳转换为 Athena 中的纪元时间 - How to convert timestamp in the form of yyyy-mm-dd hh:MM:SS.mil to epoch time in Athena
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM