How to handle the Agent Date and Time in SQL

Source: Internet
Author: User
Troubleshooting of Agent Date and Time in SQL Agent date version: 1.0.0.1lastupdated: rule. InsomecasesitusestheSQLServerdatatimedatatype, butinmostoccasionsituse

Troubleshooting of Agent date and time in SQL Agent date 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 use

Troubleshooting of Agent Date and Time in SQL Agent date
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,            @year int,            @month int,            @day int,            @hour int,            @min int,            @sec int,            @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), @year) + N'-' +                       convert(nvarchar(2), @month) + N'-' +                       convert(nvarchar(4), @day) + N' ' +                      replace(convert(nchar(2), @hour) + N':' +                               convert(nchar(2), @min) + N':' +                               convert(nchar(2), @sec), ' ', '0')    select @date = convert(datetime, @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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.