- 浏览: 225886 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
lizhiyu211:
哦,那看来性能确实不行,学习了。
记得还有一次是这么用的,是一 ...
ORACLE/MSSQL随机取一条记录方法 -
lizhiyu211:
string2020 写道直接执行 xhost + 就行了
恩 ...
linux下启动dbca或netmgr类的图形界面报错 -
Aaron5:
Aaron5 写道lizhiyu211 写道Aaron5 写道 ...
ORACLE/MSSQL随机取一条记录方法 -
Aaron5:
lizhiyu211 写道Aaron5 写道这个oracle的 ...
ORACLE/MSSQL随机取一条记录方法 -
string2020:
直接执行 xhost + 就行了
linux下启动dbca或netmgr类的图形界面报错
日期维度表:
SQL计算上月最后一天、上季度最后一天、上年最后一天、到季初天数、到年初天数、当前日期是季度中第几个月、当前日期是季度中第几天等
CREATE PROCEDURE sp_sz_tjrq(
@startdate char(8), -- 开始日期
@years integer -- 生成几年的数据
)
as
begin
-- 定义局部变量
declare @dt_begintime datetime -- 开始时间
declare @dt_endtime datetime -- 结束时间
declare @i_loopcount integer -- 循环次数
declare @c_sxrq char(8) -- 系统上线日期
declare @c_enddate char(8) -- 结束日期
declare @i_count integer -- 循环变量
declare @c_tjrq char(8) -- 统计日期
declare @c_year char(4) -- 日期年
declare @c_month char(2) -- 日期月
declare @c_day char(2) -- 日期日
declare @c_daysofmonth char(2) -- 统计日期
declare @c_daysofseason char(3) -- 月中天数
declare @c_daysofyear char(3) -- 年中天数
declare @c_monthsofseason char(2) -- 季中月数
declare @c_seasonsofyear char(2) -- 年中季数
declare @c_yesterday char(8) -- 上日日期
declare @c_pre_month_lastday char(8) -- 上月月末日期
declare @c_pre_season_lastday char(8) -- 上季季末日期
declare @c_pre_year_lastday char(8) -- 上年年末日期
declare @c_year_start_day char(8) -- 年度起始日期
declare @c_year_on_year char(8) -- 同比日期
-- 初始化变量
select @c_enddate = convert(char(8),dateadd(day,-1,dateadd(year,isnull(@years,5),@startdate)),112)
select @i_loopcount = datediff(day,@startdate,@c_enddate)
select @i_count = 0
if object_id('crmo_sz_rqdz') is null
begin
create table crmo_sz_rqdz(
tjrq char(8) not null primary key,
year char(4) null,
month char(2) null,
day char(2) null,
daysofmonth char(2) null,
daysofseason char(3) null,
daysofyear char(3) null,
monthsofseason char(2) null,
seasonsofyear char(2) null,
yesterday char(8) null,
pre_month_lastday char(8) null,
pre_season_lastday char(8) null,
pre_year_lastday char(8) null,
year_start_day char(8) null,
year_on_year char(8) null)
end
else
begin
truncate table crmo_sz_rqdz
end
while @i_count <= @i_loopcount
begin
-- 获取统计日期值
select @c_tjrq = convert(char(8),dateadd(day,@i_count,@startdate),112)
-- 截取统计日期年份
select @c_year = convert(char,year(@c_tjrq))
-- 截取统计日期月份
select @c_month = convert(char,month(@c_tjrq))
-- 截取统计日期天
select @c_day = convert(char,day(@c_tjrq))
-- 计算统计日期是本月第几天
select @c_daysofmonth = convert(char,day(@c_tjrq))
-- 计算统计日期是本季度第几天
if substring(@c_tjrq,5,2) >= '01' and substring(@c_tjrq,5,2) <= '03'
select @c_daysofseason = datediff(day,convert(datetime,left(@c_tjrq,4)+'0101'),convert(datetime,@c_tjrq))+1
if substring(@c_tjrq,5,2) >= '04' and substring(@c_tjrq,5,2) <= '06'
select @c_daysofseason = datediff(day,convert(datetime,left(@c_tjrq,4)+'0401'),convert(datetime,@c_tjrq))+1
if substring(@c_tjrq,5,2) >= '07' and substring(@c_tjrq,5,2) <= '09'
select @c_daysofseason = datediff(day,convert(datetime,left(@c_tjrq,4)+'0701'),convert(datetime,@c_tjrq))+1
if substring(@c_tjrq,5,2) >= '10' and substring(@c_tjrq,5,2) <= '12'
select @c_daysofseason = datediff(day,convert(datetime,left(@c_tjrq,4)+'1001'),convert(datetime,@c_tjrq))+1
-- 计算统计日期是本年第几天
select @c_daysofyear = convert(char(3),datepart(dy,@c_tjrq))
-- 计算统计日期所在月是本季度第几个月 = 月份除以3取余 如果=0 则 是第三个月 如果不是0余数即为季度中的月数
select @c_monthsofseason = case mod(month(@c_tjrq),3) when 0 then 3 else mod(month(@c_tjrq),3) end
-- 计算统计日期所在季度是本年第几个季度
select @c_seasonsofyear = convert(char,datepart(qq,@c_tjrq))
-- 计算昨天日期
select @c_yesterday = convert(char(8),dateadd(day,-1,@c_tjrq),112)
-- 计算上月最后一天日期
select @c_pre_month_lastday = convert(char(8),dateadd(day,-1,substring(@c_tjrq,1,6)+'01'),112)
-- 计算上季度最后一天 = substring(dateadd(qq,-1,统计日期季度),1,4)+‘季度最后一天’
select @c_pre_season_lastday = convert(char(8),dateadd(qq,-1,@c_tjrq),112)
if datepart(qq,@c_pre_season_lastday) = '1'
select @c_pre_season_lastday = substring(@c_tjrq,1,4)+'0331'
if datepart(qq,@c_pre_season_lastday) = '2'
select @c_pre_season_lastday = substring(@c_tjrq,1,4)+'0630'
if datepart(qq,@c_pre_season_lastday) = '3'
select @c_pre_season_lastday = substring(@c_tjrq,1,4)+'0930'
if datepart(qq,@c_pre_season_lastday) = '4'
select @c_pre_season_lastday = substring(@c_tjrq,1,4)+'1231'
-- 计算上一年最后一天
select @c_pre_year_lastday = convert(char(4),(year(@c_tjrq)-1)) + '1231'
-- 计算年度起始日期 统计日期向前推364天 如果小于系统上线日期那么 起始日期= 系统上线日期
select @c_year_start_day = convert(char(8),dateadd(day,-364,@c_tjrq),112)
-- 同比日期 = 上一年本月的最后一天的日期
select @c_year_on_year = convert(char(8),dateadd(day,-1,(substring(convert(char(8),dateadd(month,-11,@c_tjrq),112),1,6)+'01')),112)
-- 将计算结果插入日期对照表中
insert into crmo_sz_rqdz(tjrq,year,month,day,daysofmonth,daysofseason,daysofyear,monthsofseason,seasonsofyear,yesterday,pre_month_lastday,pre_season_lastday,pre_year_lastday,year_start_day,year_on_year)
values(@c_tjrq,@c_year,@c_month,@c_day ,@c_daysofmonth,@c_daysofseason ,@c_daysofyear,@c_monthsofseason,@c_seasonsofyear,@c_yesterday ,@c_pre_month_lastday,@c_pre_season_lastday ,@c_pre_year_lastday,@c_year_start_day,@c_year_on_year)
select @i_count = @i_count + 1
end
end
GO
发表评论
-
oracle存储过程编译卡死(资源dll锁)
2017-06-02 09:40 1677今天频繁编译一个存储过程时突然编译不 ... -
ORACLE 分析函数
2016-10-14 15:06 0引自 http://blog.csdn.net/haiross ... -
ORA-00376: 此时无法读取文件
2015-05-14 11:45 2190ORA-00376: 此时无法读取文件 4ORA-01 ... -
ORACLE解码/编码URL数据
2015-04-28 10:17 2670utl_url.escape这个方法必须放在一个函数中才能调 ... -
ORACLE数据泵 expdp/impdp使用详解(转)
2015-04-16 18:04 2622ORACLE使用EXPDP和IMPDP数据泵进行导出导入的方 ... -
ORACLE按周统计数据
2015-02-10 18:33 823oracle数据库默认的每周开始日期是周一,日常分析中往往需 ... -
sqlldr参数
2015-01-29 14:56 0有效的关键字: userid -- ORACLE use ... -
oracle 此处不允许序列
2014-11-13 14:04 1796一个插入语句,这样写报错:此处不允许序列 insert ... -
ORACLE检查字符是否为数字
2014-11-13 13:48 720select gid,gname from t_dw_fl ... -
ORACLE巡检后编译失效对象
2014-11-07 09:36 782检查失效对象: select owner,object_ ... -
为什么我的win7 ODBC数据源里没有Oracle的驱动程序
2013-05-30 17:29 3677直接在“控制面板---管理工具----数据源(ODBC)” ... -
亿赛通面试
2013-04-02 11:05 917iostate,netstate概念用途 ... -
SQL去掉重复数据只留一条
2013-02-27 15:13 1330select distinct khh,khjlh,jlrq ... -
linux下启动dbca或netmgr类的图形界面报错
2013-01-25 16:53 3464Xlib: connection to ":0 ... -
Sybase IQ 自定义表备份
2013-01-21 08:59 1877-- 初始化配置表if object_id('t_sz_ba ... -
LINUX top命令结果分析
2012-08-24 10:22 3783top命令是Linux下常用的性能分析工具,能够实时显示系统中 ... -
SYBASE IQ 查看字符集 系统状态
2012-07-17 11:12 3065查看数据库字符集: select db_property(' ... -
mysql存储过程 游标
2012-06-21 12:27 1625create table table1(id int,name ... -
red hat 5 Enterprise 入门
2012-06-04 19:35 0今天想在linux环境下搭建个oracle10G环境方 ... -
oracle OCP认证——准备篇
2012-05-30 11:22 1244本人一直从事数据库开发工作,说白了也就是编写存储过程, ...
相关推荐
sql语句查询周月年季的最后或第一天
time: 返回的时间点,如果时间点为8点,返回的结束时间为第二天的8点,例如获取8月最后一天的结束时间,以8点计,则返回值为 2022-09-01 07:59:59.997 select dbo.GetDateStartFirstAndLast('-q','s','2022-08-03',...
SQL Server中获取第一天、最后一天
所写程序需要生成一个每月第一天与最后一天日期。归档!
用于显示每月第一天及最后一天的日期值,有利于进行日期数据信息的统计
快速计算当前月份和上个月的第一天和最后一天的方法
此文档中详细记载了,SQL获取当前月的第一天和最后一天和当前月一共几天,希望可以帮到下载的朋友们!
sql去掉最后一个字符,sql去掉最后一个字符
SQLSERVER计算年龄(岁月天),可以精确到岁,月,几月几天。
通常,你需要获得当前日期和计算一些其他的日期,例如,你的程序可能需要判断一个月的第一天或者最后一天。你们大部分人大概都知道怎样把日期进行分割(年、月、日等),然后仅仅用分割出来的年、月、日等放在几个...
压缩包,包含:测试数据(excel),sql语句(txt)。如有不足,希望可以更好的交流。
本人编写,通过自己定义的函数计算两个日期之间周末的天数和工作日天数,经过测试,非常好用
sql语句得到开始日期和结束日期之间的工作日天数
计算2个日期之间相隔的天数。方便实用。 顺着算,倒着算,都可以。 还可以相隔天数到那个日期
拿去可以直接用的计算2个日期间相差天数,并过滤了节假日和周末。
sql获取当前日期 、当前日期+时间、一个月第一天、本周的星期一、一年的第一天、季度的第一天、当天的半夜、上个月的最后一天、去年的最后一天、本月的最后一天、本年的最后一天、本月的第一个星期一
利用mysql的函数实现统计查询一天24小时的数据,然后统计时间没有的数据默认补上了零。
常见所需要的一些日期之间的差值,本月最后一天,本年度最后一天
计算除周六日、法定节假日以外两个日期之间的间隔天数
SQL工作日计算,只排除周末SQL工作日计算