加入收藏 | 设为首页 | 会员中心 | 我要投稿 好传媒门户网 (https://www.haochuanmei.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

SQL中的Agent日期和时间的处理问题

发布时间:2021-01-17 13:25:40 所属栏目:编程 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 version: 1.0.0.1 last updated: 22 November 2002SQL Agent uses two ways to represent date and time information. In some cases it uses the SQL

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

version: 1.0.0.1 

last updated: 22 November 2002

SQL Agent uses two ways to represent date and time information. In some cases it uses the SQL Server datatime data type,but in most occasions it uses two integers to represent the date and time separately. The format of the date and time integers are very straight forward,the date is formatted as YYYYMMDD and the time is formatted as HHMMSS. Everywhere where schedule information is represented the date and time are stored using integers. 

The problem is that you need to convert the integer representation to a datetime,before you can leverage the existing datetime manipulation functions in SQL Server. This article provides helper functions to convert between the two representations. In total there are five user defined functions.

Name Description 
fn_AgentDate2DateTime Converts an SQL Agent integer date representation in to a SQL Server datetime datatype,since the time is not specified,the time is always 00:00:00.000 
fn_AgentTime2DateTime Converts an SQL Agent integer time representation in to a SQL Server datetime datatype,since the date is not specified,the date is always 1900-01-01 
fn_AgentDateTime2DateTime Converts an SQL Agent integer date and time representation in to a SQL Server datetime datatype 
fn_DateTime2AgentDate Converts a SQL Server datetime into an SQL Agent integer date representation 
fn_DateTime2AgentTime Converts a SQL Server datetime into an SQL Agent integer time representation 

Below follows the source code for the five user defined functions:

create function [dbo].[fn_AgentDate2DateTime] (@agentdate int)
returns datetime
as
begin
    declare @date datetime,@year int,@month int,@day int,@datestr nvarchar(40)

    select @year = (@agentdate / 10000)
    select @month = (@agentdate - (@year * 10000)) / 100
    select @day = (@agentdate - (@year * 10000) - (@month * 100))

    select @datestr = convert(nvarchar(4),@year) + N'-' + 
                      convert(nvarchar(2),@month) + N'-' + 
                      convert(nvarchar(4),@day)

    select @date = convert(datetime,@datestr)
    
    return @date
end
go

-- example
select [dbo].[fn_AgentDate2DateTime](20020430)
go

create function [dbo].[fn_AgentTime2DateTime](@agenttime int)
returns datetime
as
begin
    declare @date datetime,@hour int,@min int,@sec int,@datestr nvarchar(40)

    select @hour = (@agenttime / 10000)
    select @min = (@agenttime - (@hour * 10000)) / 100
    select @sec = (@agenttime - (@hour * 10000) - (@min * 100))

    select @datestr = replace(convert(nvarchar(2),@hour) + N':' + 
                              convert(nvarchar(2),@min) + N':' + 
                              convert(nvarchar(2),@sec),' ','0')

    select @date = convert(datetime,@datestr)

    return @date
end
go

-- example
select [dbo].[fn_AgentTime2DateTime] (110015)
go

create function [dbo].[fn_AgentDateTime2DateTime] (@agentdate int,@agenttime int)
returns datetime
as
begin
    declare @date datetime,@datestr nvarchar(40)

    select @year = (@agentdate / 10000)
    select @month = (@agentdate - (@year * 10000)) / 100
    select @day = (@agentdate - (@year * 10000) - (@month * 100))

    select @hour = (@agenttime / 10000)
    select @min = (@agenttime - (@hour * 10000)) / 100
    select @sec = (@agenttime - (@hour * 10000) - (@min * 100))

    select @datestr = convert(nvarchar(4),@day) + N' ' +
                      replace(convert(nchar(2),@hour) + N':' + 
                              convert(nchar(2),@min) + N':' + 
                              convert(nchar(2),@datestr)

    return @date
end
go

-- example
select [dbo].[fn_AgentDateTime2DateTime] (20020222,110015)
go

create function [dbo].[fn_DateTime2AgentDate] (@date datetime)
returns int
as
begin
    declare @dateint int

    select @dateint = (datepart(year,@date) * 10000) +
                      (datepart(month,@date) * 100) +
                      (datepart(day,@date))

    return @dateint 
end
go

-- example
select [dbo].[fn_DateTime2AgentDate] (getdate())
go

create function [dbo].[fn_DateTime2AgentTime] (@date datetime)
returns int
as
begin
    declare @timeint int

    select @timeint = (datepart(hour,@date) * 10000) +
                      (datepart(minute,@date) * 100) +
                      (datepart(second,@date))

    return @timeint
end
go

-- example
select [dbo].[fn_DateTime2AgentTime] (getdate())
go

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:好传媒门户网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读