一个SQL题目,求围观

小金牛儿 2013-11-05 04:30:20
请教个查询语句
我想取得指定年 每个月月末倒数2条怎么写
例如结果
1月31日、1月30日、2日28日、2月29日...
...全文
366 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfktqti 2013-11-08
  • 打赏
  • 举报
回复
create table #tb(day2 datetime) go declare @year char(4) set @year=2013 insert into #tb select @year+'-01-03' union all select @year+'-02-03' union all select @year+'-03-03' union all select @year+'-04-03' union all select @year+'-05-03' union all select @year+'-06-03' union all select @year+'-07-03' union all select @year+'-08-03' union all select @year+'-09-03' union all select @year+'-10-03' union all select @year+'-11-03' union all select @year+'-12-03' union all select @year+'-01-02' union all select @year+'-02-02' union all select @year+'-03-02' union all select @year+'-04-02' union all select @year+'-05-02' union all select @year+'-06-02' union all select @year+'-07-02' union all select @year+'-08-02' union all select @year+'-09-02' union all select @year+'-10-02' union all select @year+'-11-02' union all select @year+'-12-02' union all select @year+'-01-01' union all select @year+'-02-01' union all select @year+'-03-01' union all select @year+'-04-01' union all select @year+'-05-01' union all select @year+'-06-01' union all select @year+'-07-01' union all select @year+'-08-01' union all select @year+'-09-01' union all select @year+'-10-01' union all select @year+'-11-01' union all select @year+'-12-01' select * from (SELECT ROW_NUMBER() OVER(PARTITION BY datepart(month,day2) ORDER BY day2 DESC) AS [RowId],*from #tb) t where [RowId]<3
f3228061 2013-11-07
  • 打赏
  • 举报
回复
DECLARE @Y VARCHAR(4)
SET @Y='2013'

SELECT 
RTRIM(DATEPART(MM,DATEADD(D,-1,A)))+'月'+RTRIM(DATEPART(DD,DATEADD(D,-1,A)))+'日' as '倒数第二天',
RTRIM(DATEPART(MM,DATEADD(D,-2,A)))+'月'+RTRIM(DATEPART(DD,DATEADD(D,-2,A)))+'日' as '倒数第二天'
FROM
(
SELECT dateadd(M,NUMBER,@Y+'-02-01') as a FROM dbo.spt_values 
WHERE TYPE='P' AND NUMBER<12
) AS A

f3228061 2013-11-07
  • 打赏
  • 举报
回复
引用 6 楼 ap0405140 的回复:

declare @y int

select @y=2013 --> 指定年

select rtrim(datepart(m,d1))+'月'+rtrim(datepart(d,d1))+'日' '倒数1',
       rtrim(datepart(m,d2))+'月'+rtrim(datepart(d,d2))+'日' '倒数2'
from
(select dateadd(d,-1,x) 'd1',dateadd(d,-2,x) 'd2' from
 (select dateadd(d,number,cast(rtrim(@y)+'-01-01' as date)) 'x'
  from master.dbo.spt_values
  where type='P' and number<=370) t
 where datepart(d,x)=1) y
where datepart(yyyy,d1)=@y and datepart(yyyy,d2)=@y 

/*
倒数1                          倒数2
---------------------------- ----------------------------
1月31日                        1月30日
2月28日                        2月27日
3月31日                        3月30日
4月30日                        4月29日
5月31日                        5月30日
6月30日                        6月29日
7月31日                        7月30日
8月31日                        8月30日
9月30日                        9月29日
10月31日                       10月30日
11月30日                       11月29日
12月31日                       12月30日

(12 row(s) affected)
*/
直接从2月1号,减一天和减两天都行的,而且不会担心365天和366天的问题
f3228061 2013-11-07
  • 打赏
  • 举报
回复
DECLARE @Y VARCHAR(4) SET @Y='2013' SELECT RTRIM(DATEPART(MM,DATEADD(D,-1,A)))+'月'+RTRIM(DATEPART(DD,DATEADD(D,-1,A)))+'日' as '倒数第二天', RTRIM(DATEPART(MM,DATEADD(D,-2,A)))+'月'+RTRIM(DATEPART(DD,DATEADD(D,-2,A)))+'日' as '倒数第二天' FROM ( SELECT dateadd(M,NUMBER,@Y+'-02-01') as a FROM dbo.spt_values WHERE TYPE='P' AND NUMBER<12 ) AS A
shuaidaile 2013-11-06
  • 打赏
  • 举报
回复
二楼比较准确, 月末要用下个月的月初减去一天和两天
LongRui888 2013-11-06
  • 打赏
  • 举报
回复
或者是这样:

declare @year varchar(10);
declare @str varchar(1000);

set @year = '2013';  --你要的年
set @str = '';

;with t
as
(
select 1 as number 
union all
select number + 1
from t
where t.number < 380
)

select @str = @str +'、'+cast(cast(substring(t,6,2) as int) as varchar)+
       '月'+right(t,2)+'日' 
from 
(
select convert(varchar(10),dateadd(day,number,@year+'-01-01'),120) as t,
       row_number() over(partition by convert(varchar(7),dateadd(day,number,@year+'-01-01'),120)
                         order by number desc) as rownum  
from t
where dateadd(day,number,@year+'-01'+'-01')<=@year +'-12-31'
)t
where t.rownum <=2
option(maxrecursion 1000)

select stuff(@str,1,1,'')
/*
1月31日、1月30日、2月28日、2月27日、3月31日、3月30日、4月30日、4月29日、5月31日、5月30日、6月30日、6月29日、7月31日、7月30日、8月31日、8月30日、9月30日、9月29日、10月31日、10月30日、11月30日、11月29日、12月31日、12月30日
*/
decamincow 2013-11-06
  • 打赏
  • 举报
回复
引用 11 楼 guoren12 的回复:
declare @d int
        ,@sql nvarchar(max)
set @d=2
set @sql=N'select dateadd(MONTH,1,dateadd(day,-datepart(dayofyear,getdate()),getdate())) ''倒1'',dateadd(month,1,dateadd(day,-1-datepart(dayofyear,getdate()),getdate())) ''倒2'''
while @d<=12
begin
set @sql=@sql+' union all 
select dateadd(MONTH,'+cast(@d as varchar(2))+',dateadd(day,-datepart(dayofyear,getdate()),getdate())) ''倒1'',dateadd(month,'+cast(@d as varchar(2))+',dateadd(day,-1-datepart(dayofyear,getdate()),getdate())) ''倒2'''
set @d=@d+1
end
print @sql
exec(@sql)
这个结果是当前年的数据,可以适当更改下
decamincow 2013-11-06
  • 打赏
  • 举报
回复
declare @d int
        ,@sql nvarchar(max)
set @d=2
set @sql=N'select dateadd(MONTH,1,dateadd(day,-datepart(dayofyear,getdate()),getdate())) ''倒1'',dateadd(month,1,dateadd(day,-1-datepart(dayofyear,getdate()),getdate())) ''倒2'''
while @d<=12
begin
set @sql=@sql+' union all 
select dateadd(MONTH,'+cast(@d as varchar(2))+',dateadd(day,-datepart(dayofyear,getdate()),getdate())) ''倒1'',dateadd(month,'+cast(@d as varchar(2))+',dateadd(day,-1-datepart(dayofyear,getdate()),getdate())) ''倒2'''
set @d=@d+1
end
print @sql
exec(@sql)
好帅的一条鱼 2013-11-05
  • 打赏
  • 举报
回复
学习,一群猛人
LongRui888 2013-11-05
  • 打赏
  • 举报
回复

declare @year varchar(10);

set @year = '2013';  --你要的年

;with t
as
(
select 1 as number 
union all
select number + 1
from t
where t.number < 380
)

select cast(cast(substring(t,6,2) as int) as varchar)+
       '月'+right(t,2)+'日' as '日期'
from 
(
select convert(varchar(10),dateadd(day,number,@year+'-01-01'),120) as t,
       row_number() over(partition by convert(varchar(7),dateadd(day,number,@year+'-01-01'),120)
                         order by number desc) as rownum  
from t
where dateadd(day,number,@year+'-01'+'-01')<=@year +'-12-31'
)t
where t.rownum <=2
option(maxrecursion 1000)
/*
日期
1月31日
1月30日
2月28日
2月27日
3月31日
3月30日
4月30日
4月29日
5月31日
5月30日
6月30日
6月29日
7月31日
7月30日
8月31日
8月30日
9月30日
9月29日
10月31日
10月30日
11月30日
11月29日
12月31日
12月30日
*/
唐诗三百首 2013-11-05
  • 打赏
  • 举报
回复

declare @y int

select @y=2013 --> 指定年

select rtrim(datepart(m,d1))+'月'+rtrim(datepart(d,d1))+'日' '倒数1',
       rtrim(datepart(m,d2))+'月'+rtrim(datepart(d,d2))+'日' '倒数2'
from
(select dateadd(d,-1,x) 'd1',dateadd(d,-2,x) 'd2' from
 (select dateadd(d,number,cast(rtrim(@y)+'-01-01' as date)) 'x'
  from master.dbo.spt_values
  where type='P' and number<=370) t
 where datepart(d,x)=1) y
where datepart(yyyy,d1)=@y and datepart(yyyy,d2)=@y 

/*
倒数1                          倒数2
---------------------------- ----------------------------
1月31日                        1月30日
2月28日                        2月27日
3月31日                        3月30日
4月30日                        4月29日
5月31日                        5月30日
6月30日                        6月29日
7月31日                        7月30日
8月31日                        8月30日
9月30日                        9月29日
10月31日                       10月30日
11月30日                       11月29日
12月31日                       12月30日

(12 row(s) affected)
*/
LongRui888 2013-11-05
  • 打赏
  • 举报
回复
引用 楼主 fox123871 的回复:
请教个查询语句 我想取得指定年 每个月月末倒数2条怎么写 例如结果 1月31日、1月30日、2日28日、2月29日...
你有具体的数据不,这样才能有的放矢
Andy__Huang 2013-11-05
  • 打赏
  • 举报
回复
create table #tb(day2 datetime)
go

declare @year char(4)
set @year=2013
insert into #tb
select @year+'-01-01'
union all select @year+'-02-01'
union all select @year+'-03-01'
union all select @year+'-04-01'
union all select @year+'-05-01'
union all select @year+'-06-01'
union all select @year+'-07-01'
union all select @year+'-08-01'
union all select @year+'-09-01'
union all select @year+'-10-01'
union all select @year+'-11-01'
union all select @year+'-12-01'

select dateadd(day,-1,dateadd(month,1,day2)) 倒数第一天,dateadd(day,-2,dateadd(month,1,day2)) 倒数第二天
from #tb
/*
倒数第一天          倒数第二天
----------------------------------------------------
2013-01-31 00:00:00.000	2013-01-30 00:00:00.000
2013-02-28 00:00:00.000	2013-02-27 00:00:00.000
2013-03-31 00:00:00.000	2013-03-30 00:00:00.000
2013-04-30 00:00:00.000	2013-04-29 00:00:00.000
2013-05-31 00:00:00.000	2013-05-30 00:00:00.000
2013-06-30 00:00:00.000	2013-06-29 00:00:00.000
2013-07-31 00:00:00.000	2013-07-30 00:00:00.000
2013-08-31 00:00:00.000	2013-08-30 00:00:00.000
2013-09-30 00:00:00.000	2013-09-29 00:00:00.000
2013-10-31 00:00:00.000	2013-10-30 00:00:00.000
2013-11-30 00:00:00.000	2013-11-29 00:00:00.000
2013-12-31 00:00:00.000	2013-12-30 00:00:00.000
*/


Landa_Peter 2013-11-05
  • 打赏
  • 举报
回复
不好意思发错了
Landa_Peter 2013-11-05
  • 打赏
  • 举报
回复
创建连接
/*********************************************************************************************
Function:创建连接服务器
Author:Bean
Date:2012-07-23
*********************************************************************************************/
--方法1:创建连接服务器
--判断是否已存在该连接服务器,如果存在删除
if exists(select 1 from sys.servers where name='LinkServer_BEAN')
Begin
exec master.dbo.sp_dropserver @server='LinkServer_BEAN',@droplogins='droplogins'
End
--创建连接服务器
exec sp_addlinkedserver
@server='SUNDB', --Linked Server Name
@srvproduct='', --Product Name
@provider='SQLOLEDB', --Provider
@datasrc='127.0.0.1' --Data Address
Go
exec sp_addlinkedsrvlogin
@useself='false',
@rmtsrvname='SUNDB', --Linked Server Name
@rmtuser='123', --User ID
@rmtpassword='123' --Password

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧