发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
根据月份得出日历,求一sql 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 像这样的 ---------------------------------------declare @month as varchar(7)set @month = '2007-12'select 日,一,二,三,四,五,六 from(select week , max(case weekday when 1 then datename(day,dt) else '' end ) '日', max(case weekday when 2 then datename(day,dt) else '' end ) '一', max(case weekday when 3 then datename(day,dt) else '' end ) '二', max(case weekday when 4 then datename(day,dt) else '' end ) '三', max(case weekday when 5 then datename(day,dt) else '' end ) '四', max(case weekday when 6 then datename(day,dt) else '' end ) '五', max(case weekday when 7 then datename(day,dt) else '' end ) '六'from( select week = datepart(week , m.dt) , weekday = datepart(weekday , m.dt) , dt from ( select dt = @month + '-' + right('00'+cast(t.id as varchar),2) from ( select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 union select 13 union select 14 union select 15 union select 16 union select 17 union select 18 union select 19 union select 20 union select 21 union select 22 union select 23 union select 24 union select 25 union select 26 union select 27 union select 28 union select 29 union select 30 union select 31 ) t where isdate(@month + '-' + right('00'+cast(t.id as varchar),2)) = 1 and @month + '-' + right('00'+cast(t.id as varchar),2) <= dateadd(month , 1 , @month + '-01') ) m) ngroup by week) o/*日 一 二 三 四 五 六 -- -- -- -- -- -- -- 12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31 (所影响的行数为 6 行)*/----------------------------------------------------------------------用函数解决。(libin_ftsafe)create function f_calendar(@year int,@month int)returns @t table(日 varchar(4),一 varchar(4),二 varchar(4),三 varchar(4),四 varchar(4),五 varchar(4),六 varchar(4))asbegin declare @a table(id int identity(0,1),date datetime) insert into @a(date) select top 31 rtrim(@year)+'-'+rtrim(@month)+'-1' from sysobjects update @a set date=dateadd(dd,id,date) insert into @t select max(case datepart(dw,date) when 7 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 1 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 2 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 3 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 4 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 5 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 6 then rtrim(day(date)) else '' end) from @a where month(date)=@month group by (case datepart(dw,date) when 7 then datepart(week,date)+1 else datepart(week,date) end) returnendgoset datefirst 1select * from dbo.f_calendar(2007,12)/*日 一 二 三 四 五 六 ---- ---- ---- ---- ---- ---- ---- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31 */godrop function f_calendargo
源:http://topic.csdn.net/u/20090321/12/81433b7d-d02a-4a88-9e59-4463d1d56da9.html#replyachor
来自: 昵称10504424 > 《SqlServer》
0条评论
发表
请遵守用户 评论公约
(转)行变列SQL语句(MSSQL) - jack - 博客园
SELECT 字段1,[字段(01)]=MAX(CASE WHEN 字段2 =‘01‘ THEN 字段3 ELSE ‘‘ END),[字段(02)]=MAX(CASE WHEN 字段2 =‘02‘ THEN 字段3 ELSE ‘‘ END),[字段(03)]=MAX(CASE WHEN 字段2 =‘03‘ THEN ...
经典SQL----行列转换
declare @sql varchar(8000)set @sql = ''select 姓名 ''select @sql = @sql + '' , max(case 课程 when '''''' + 课程 + '''''...
Sql Server 整理收集
-- 记录为select a.title_id,a.title,b.au_id from titles a,titleauthor b where a.title_id=b.title_id.select a.title_id,a.title,b.au_id into NewTableName from titles a,titleauthor b where a....
SQL
select @wk_dt=convert(varchar,datepart(month,@now_dt))+''.''+convert.select @wk_dt=@wk_dt+convert(varchar,datepart(month,dateadd(day,-1,@now_dt)))+''.''+.i...
SQL 查询当天,本月,本周的记录
举例:1.GetDate() 用于sql server :select GetDate()select dateadd(wk,datediff(wk,0,getdate()),6)–3.一年的第一天SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)–查询本周注册人数sel...
getDate()显示格式 转 与时间相关的SQL语句/sql获取当前时间
oracle行转列
ROW_NUMBER + LEAD适用范围:8i,9i,10g及以后版本SELECT id, strFROM (SELECT id,row_number() over(PARTITION BY id ORDER BY col) AS rn,col || lead('','' || col, 1) over(PARTITI...
About Trim and Convert (去空格,text与varchar转换)
About Trim and Convert (去空格,text与varchar转换)--POT.P4_ORDERS text FREE_FORM_ORDERS varchar(MAX) (converttext to varchar)WHEN LTRIM(RTRIM(convert(varchar(max),POT.P4_...
精妙SQL语句
declare @sql varchar(8000)set @sql = ‘select name,‘select @sql = @sql + ‘sum(case subject when ‘‘‘+subject+‘‘‘ then source else 0 end) as ‘‘‘+subject+‘‘‘,‘from (select dis...
微信扫码,在手机上查看选中内容