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

SQL中的Agent日期和时间的处理问题,第1张

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

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

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 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 datetimeasbegin    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 @dateendgo-- exampleselect [dbo].[fn_AgentDate2DateTime](20020430)gocreate function [dbo].[fn_AgentTime2DateTime](@agenttime int)returns datetimeasbegin    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 @dateendgo-- exampleselect [dbo].[fn_AgentTime2DateTime] (110015)gocreate function [dbo].[fn_AgentDateTime2DateTime] (@agentdate int,@agenttime int)returns datetimeasbegin    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 @dateendgo-- exampleselect [dbo].[fn_AgentDateTime2DateTime] (20020222,110015)gocreate function [dbo].[fn_DateTime2AgentDate] (@date datetime)returns intasbegin    declare @dateint int    select @dateint = (datepart(year,@date) * 10000) +                      (datepart(month,@date) * 100) +                      (datepart(day,@date))    return @dateint endgo-- exampleselect [dbo].[fn_DateTime2AgentDate] (getdate())gocreate function [dbo].[fn_DateTime2AgentTime] (@date datetime)returns intasbegin    declare @timeint int    select @timeint = (datepart(hour,@date) * 10000) +                      (datepart(minute,@date) * 100) +                      (datepart(second,@date))    return @timeintendgo-- exampleselect [dbo].[fn_DateTime2AgentTime] (getdate())go

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的SQL中的Agent日期和时间的处理问题全部内容,希望文章能够帮你解决SQL中的Agent日期和时间的处理问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1165148.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存