关于sql中日期相关跨年处理
来源:互联网 发布:mac上可以剪辑音频 编辑:程序博客网 时间:2024/04/30 10:40
关于sql数据库里日期的跨年处理:
读取本周和上周纪录时,涉及跨年数据要特殊处理:
数据库里周数保存 1-52周。
按照sql函数取得周数时会出现53的问题。
写入数据库时要处理州周为53的情况,
week=53 则当作下1年处理
nian=nian+1
week=1
本周:
SET DATEFIRST 1 -- 设置周一为一周的第一天
declare @week_year int
set @week_year = year(getdate())
declare @week smallint -- 第几周
select @week = datepart(week,getdate())
if(@week=53)
begin
set @week_year=@week_year+1
set @week=1
end
上周:
SET DATEFIRST 1 -- 设置周一为一周的第一天
declare @last_week_year int
set @last_week_year = year(getdate())
declare @last_week smallint -- 第几周
select @last_week = datepart(week,getdate())
if(@last_week=1)
begin
set @last_week_year=@last_week_year-1
set @last_week=52
end
else
begin
set @last_week=@last_week-1
end
上上周:
SET DATEFIRST 1 -- 设置周一为一周的第一天
declare @last_last_week_year int
set @last_last_week_year = year(getdate())
declare @last_last_week smallint -- 第几周
select @last_last_week = datepart(week,getdate())
if(@last_last_week=1)
begin
set @last_last_week_year=@last_last_week_year-1
set @last_last_week=51
end
else if(@last_last_week=2)
begin
set @last_last_week_year=@last_last_week_year-1
set @last_last_week=52
end
else
begin
set @last_last_week=@last_last_week-2
end
本月:
declare @today_year int
declare @today_month int
set @today_year = year(getdate())
set @today_month = month(getdate())
上月:
declare @last_month_year int
declare @last_month int
set @last_month_year = year(getdate())
set @last_month = month(getdate())
if (@last_month = 1)
begin
set @last_month = 12
set @last_month_year =@last_month_year -1
end
else if (@last_month != 1)
begin
set @last_month = @last_month-1
end
- 关于sql中日期相关跨年处理
- SQL 中日期的处理
- 关于SQL语句中日期的处理to_date()的应用
- SQL中日期/时间函数的处理
- Java中日期处理
- jsp中日期处理
- Hive中日期处理
- Mysql中日期处理
- sql中日期格式
- sql中日期操作
- oracle中日期相关sql的写法及技巧
- SQL中日期格式的处理to_char函数
- 关于SQL SERVER中日期型数据的数
- jquery 中日期相关方法
- java中日期处理方法
- JAVA中日期的处理
- abap 中日期的处理
- Dojo中日期格式化处理
- asp.net获取URL和IP地址
- 内核开发相关的书
- 简历上的自我评价如何写
- 报道了
- 如何注册OCX控件
- 关于sql中日期相关跨年处理
- PHP环境配置中遇到的各种问题解决方法: Cannot load php5apache2_2.dll into server
- vs 相关快捷键
- 什么是shell
- 打开VMware出现Reason: Failed to lock the file.错误的解决办法
- 将android源码的签名包,转化为eclipse可以的签名包
- 【那些年,我们一起追的女孩】第十二章
- android 与 服务器之间的推送方式
- delphi身份证验证函数