最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-09 16:19:06
文档

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

SQL中的Agent日期和时间的处理问题:SQL中的Agent日期和时间的处理问题 Agent 日期 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 Se
推荐度:
导读SQL中的Agent日期和时间的处理问题:SQL中的Agent日期和时间的处理问题 Agent 日期 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 Se

SQL中的Agent日期和时间的处理问题 Agent 日期 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

SQL中的Agent日期和时间的处理问题 Agent 日期 $velocityCount-->
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,
 @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 @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

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

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

SQL中的Agent日期和时间的处理问题:SQL中的Agent日期和时间的处理问题 Agent 日期 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 Se
推荐度:
标签: 中的 时间 处理
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top