当前位置: 代码迷 >> Sql Server >> sql 时间加减,该如何解决
  详细解决方案

sql 时间加减,该如何解决

热度:96   发布时间:2016-04-24 10:07:23.0
sql 时间加减
数据库时间结构是
年 smallint
月 smallint
日 smallint
时 smallint
数值 int

怎样输出 与前一天的数据差

结果类似

年     月 日  时 数值   24小时差值
2014  1   1   1  99          
2014  1   2   1  92          7
2014  1   3   1  92          0
2014  1   3   1  96          4
2014  1   3   1  99          3

------解决方案--------------------
这样?
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-08-19 11:02:23
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
-- May 14 2014 18:34:29 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([年] int,[月] int,[日] int)
insert [huang]
select 2014,1,1 union all
select 2014,1,2 union all
select 2014,1,3 union all
select 2014,1,3 union all
select 2014,1,3
--------------开始查询--------------------------

select CAST(年 AS CHAR(4))+'-'+RIGHT('00'+CAST(月 AS VARCHAR(2)),2)+'-'+RIGHT('00'+CAST([日] AS VARCHAR(2)),2)[年月日]
from [huang]
----------------结果----------------------------
/* 
年月日
--------------
2014-01-01
2014-01-02
2014-01-03
2014-01-03
2014-01-03
*/
  相关解决方案