Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将时间从{yyyy-mm-dd hh:mm:ss}转换为{yyyy-mm-dd hh}_Sql_Sql Server - Fatal编程技术网

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

Sql 将时间从{yyyy-mm-dd hh:mm:ss}转换为{yyyy-mm-dd hh},sql,sql-server,Sql,Sql Server,我试图每小时从大型SQL数据库中提取数据。下面的查询以分钟为单位给我一个分组。我希望按每小时的数据分组 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

我试图每小时从大型SQL数据库中提取数据。下面的查询以分钟为单位给我一个分组。我希望按每小时的数据分组

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)
选择CONVERT(VARCHAR(20),t.counterTime,100)作为[Time],
t、 实例名
从表中选择t
其中t.counterId=@counterId
和t.计数器时间>=@startTime
和t.计数器时间<@endTime
按t.instanceName分组,转换(VARCHAR(20),t.counterTime,100)
政府也没有提供任何这样的选择。有什么想法吗

像这样试试

--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.
——SQL Server 2012
选择格式(t.计数器时间,'yyyy-MM-dd-HH')作为[时间]
,t.instanceName
从表中选择t
其中t.counterId=@counterId
和t.计数器时间>=@startTime
和t.计数器时间<@endTime
按t.instanceName分组
,格式(t.计数器时间,“yyyy-MM-dd-HH”)
--对于早期版本
选择CONVERT(字符(13),t.计数器时间,120)作为[时间]
,t.instanceName
从表中选择t
其中t.counterId=@counterId
和t.计数器时间>=@startTime
和t.计数器时间<@endTime
按t.instanceName分组
,转换(字符(13),t.计数器时间,120)
注意:格式可能会比较慢。
像这样试试

--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.
——SQL Server 2012
选择格式(t.计数器时间,'yyyy-MM-dd-HH')作为[时间]
,t.instanceName
从表中选择t
其中t.counterId=@counterId
和t.计数器时间>=@startTime
和t.计数器时间<@endTime
按t.instanceName分组
,格式(t.计数器时间,“yyyy-MM-dd-HH”)
--对于早期版本
选择CONVERT(字符(13),t.计数器时间,120)作为[时间]
,t.instanceName
从表中选择t
其中t.counterId=@counterId
和t.计数器时间>=@startTime
和t.计数器时间<@endTime
按t.instanceName分组
,转换(字符(13),t.计数器时间,120)
注意:格式可能会比较慢。

您可以像这样使用子字符串:

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

可以像这样使用子字符串:

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

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) 
选择datepart(小时,t.计数器时间)作为[时间],
t、 实例名
从表中选择t
其中t.counterId=@counterId
和t.计数器时间>=@startTime
和t.计数器时间<@endTime
按t.instanceName、datepart分组(小时、t.counterTime)
试试这个

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) 
选择datepart(小时,t.计数器时间)作为[时间],
t、 实例名
从表中选择t
其中t.counterId=@counterId
和t.计数器时间>=@startTime
和t.计数器时间<@endTime
按t.instanceName、datepart分组(小时、t.counterTime)

是,不要使用
varhcar(20)
,而是使用
char(13)
。在您的选择和分组依据中使用表达式
CAST(t.counterTime AS DATE)
。你是怎么想出这个表达式的?@Nick.McDermaid casting to date会丢失小时数据…哦,是的,我错过了那个比特是的,不要使用
varhcar(20)
,而是使用
char(13)
。在你的选择和分组依据中使用表达式
CAST(t.counterTime AS date)
。你是怎么想出这个表达式的?@Nick.McDermaid到目前为止的播放会丢失小时数据…哦,是的,我错过了那一位请注意,
格式
只适用于2012版或更高版本。这些内容是不必要的
CONVERT(Char(13),t.counterTime,120)
将返回与
格式(t.counterTime,'yyyy-MM-dd-HH')相同的字符串。
返回的字符串。不过,我很喜欢你自己测试了一下,在dba.stackexhage上找到了Aaron的答案。请注意,
格式
只适用于2012版或更高版本。这些东西是不必要的
CONVERT(Char(13),t.counterTime,120)
将返回与
格式(t.counterTime,'yyyy-MM-dd-HH')相同的字符串。
返回的字符串。不过,我很喜欢你测试了自己,在dba.stackexchage上找到了Aaron的答案。