發(fā)文章
發(fā)文工具
撰寫
網(wǎng)文摘手
文檔
視頻
思維導(dǎo)圖
隨筆
相冊
原創(chuàng)同步助手
其他工具
圖片轉(zhuǎn)文字
文件清理
AI助手
留言交流
根據(jù)月份得出日歷,求一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 (所影響的行數(shù)為 6 行)*/----------------------------------------------------------------------用函數(shù)解決。(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條評論
發(fā)表
請遵守用戶 評論公約
(轉(zhuǎn))行變列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 ...
經(jīng)典SQL----行列轉(zhuǎn)換
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 查詢當(dāng)天,本月,本周的記錄
舉例:1.GetDate() 用于sql server :select GetDate()select dateadd(wk,datediff(wk,0,getdate()),6)–3.一年的第一天SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)–查詢本周注冊人數(shù)sel...
getDate()顯示格式 轉(zhuǎn) 與時間相關(guān)的SQL語句/sql獲取當(dāng)前時間
oracle行轉(zhuǎn)列
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轉(zhuǎn)換)
About Trim and Convert (去空格,text與varchar轉(zhuǎn)換)--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...
微信掃碼,在手機(jī)上查看選中內(nèi)容