datediff(day,createdate,GetDate())=0 -- 判斷是否當天,createdate為日期字段 -- ╔════════════════════╗ -- ================================================================================= ║ 第一天、第幾月 ║ -- ╚════════════════════╝ -- 1.一個月第一天的 Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) -- 2.本周的星期一 Select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) -- 3.一年的第一天 Select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) -- 4.季度的第一天 Select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) -- 5.當天的半夜 Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) -- 6.上個月的最后一天 Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)) -- 7.去年的最后一天 Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) -- 8.本月的最后一天 Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) -- 9.本年的最后一天 Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0)) -- 10.本月的第一個星期一 select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0) select 本年第多少周=datename(week,getdate()) ,今天是周幾=datename(weekday,getdate()) -- dateadd 在向指定日期加上一段時間的基礎上,返回新的datetime值 -- 向日期加上2天 或 增加1個月 select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000 select dateadd(month,2,'2004-10-15') --返回:2004-12-17 00:00:00.000 --3. datediff 返回跨兩個指定日期的日期和時間邊界數。 select datediff(day,'2004-09-01','2004-09-18') --返回天數:17 select DateDiff(s,'2005-07-20','2005-7-25 22:56:32') --返回值為 514592 秒 select DateDiff(ms,'2005-07-20','2005-7-25 22:56:32') --返回值為 微秒 select DateDiff(d,'2005-07-20','2005-7-25 22:56:32') -- 返回值為 5 天 select DatePart(w,'2005-7-25 22:56:32')--返回值為 2 即星期一(周日為1,周六為7) select DatePart('d','2005-7-25 22:56:32')--返回值為 25即25號 select DatePart('y','2005-7-25 22:56:32')--返回值為 206即這一年中第206天 select DatePart('yyyy','2005-7-25 22:56:32')--返回值為 2005即2005年 --DateDiff (interval,date1,date2) 以interval 指定的方式, --返回date2 與date1兩個日期之間的差值 date2-date1 --DateAdd (interval,number,date) 以interval指定的方式,加上number之后的日期 --DatePart (interval,date) 返回日期date中,interval指定部分所對應的整數值 --DateName (interval,date) 返回日期date中,interval指定部分所對應的字符串名稱 -- ╔════════════════════╗ -- ================================================================================= ║ 當前時間函數 ║ -- ╚════════════════════╝ -- 返回當前日期和時間 select GETDATE() -- 返回代表指定日期的指定日期部分的整數。 select datepart(month, '2004-10-15') --返回 月 select datepart(day, '2004-10-15') --返回 日 select datepart(year, getdate()) --返回 年 select convert(varchar(8),getdate(),114) -- 當前時間 select datename(weekday, getdate()) --返回:星期五 select datepart(weekday, getdate()) --返回:小寫星期2-1 select convert(varchar(10),getdate(),120) -- 當前日期 select datepart(S, '2004-10-15') --返回 月 -- 返回時間到豪秒 Select CONVERT(VARCHAR(30),GETDATE(),9) -- 獲取當前日期,年、月、日、周、時、分、秒 select GETDATE() as '當前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) as '月', DateName(day,GetDate()) as '日', DateName(dw,GetDate()) as '星期', DateName(week,GetDate()) as '周數', DateName(hour,GetDate()) as '時', DateName(minute,GetDate()) as '分', DateName(second,GetDate()) as '秒' print DateName(second,GetDate())+'1' -- 格式 select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','') 20040912110608 select CONVERT(varchar(12) , getdate(), 111 ) 2004/09/12 select CONVERT(varchar(12) , getdate(), 112 ) 20040912 select CONVERT(varchar(12) , getdate(), 102 ) 2004.09.12 select CONVERT(varchar(12) , getdate(), 101 ) 09/12/2004 select CONVERT(varchar(12) , getdate(), 103 ) 12/09/2004 select CONVERT(varchar(12) , getdate(), 104 ) 12.09.2004 select CONVERT(varchar(12) , getdate(), 105 ) 12-09-2004 select CONVERT(varchar(12) , getdate(), 106 ) 12 09 2004 select CONVERT(varchar(12) , getdate(), 107 ) 09 12, 2004 select CONVERT(varchar(12) , getdate(), 108 ) 11:06:08 select CONVERT(varchar(12) , getdate(), 109 ) 09 12 2004 1 select CONVERT(varchar(12) , getdate(), 110 ) 09-12-2004 select CONVERT(varchar(12) , getdate(), 113 ) 12 09 2004 1 select CONVERT(varchar(12) , getdate(), 114 ) 11:06:08.177 -- ╔════════════════════╗ -- ================================================================================= ║ 數據庫時間函數 ║ -- ╚════════════════════╝ -- 查詢最近一個月內的點擊率大於100的記錄數據: select * from t_business_product where hit_count>100 and datediff(Dd,last_date,getdate())<=30 order by id desc -- 查詢最近一周內的點擊率大於100的記錄數據: select * from t_business_product where hit_count>100 and datediff(Dw,last_date,getdate())<=7 order by id desc -- 你可以使用LIKE來返回正確的記錄。通過在日期表達式中包含通配符“%”, -- 你可以匹配一個特定日期的所有時間。這里有一個例子: --這個語句可以匹配正確的記錄。因為通配符“%”代表了任何時間。 Select * FROM weblog Where entrydate LIKE ‘Dec 25 2000%’ -- ╔════════════════════╗ -- ================================================================================= ║ CAST和CONVERT函數 ║ -- ╚════════════════════╝ select @@version
