SQL 时间函数

[sql]  view plain  copy
  1.           datediff(day,createdate,GetDate())=0      -- 判断是否当天,createdate为日期字段  
  2.       
  3.     --                                                                                    ╔════════════════════╗  
  4.     -- =================================================================================  ║    第一天、第几月  ║  
  5.     --                                                                                    ╚════════════════════╝   
  6.   
  7.             -- 1.一个月第一天的  
  8.             Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)  
  9.   
  10.             -- 2.本周的星期一  
  11.             Select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)  
  12.   
  13.             -- 3.一年的第一天  
  14.             Select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)  
  15.   
  16.             -- 4.季度的第一天  
  17.             Select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)  
  18.   
  19.             -- 5.当天的半夜  
  20.             Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)  
  21.   
  22.             -- 6.上个月的最后一天  
  23.             Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))  
  24.   
  25.             -- 7.去年的最后一天  
  26.             Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))  
  27.   
  28.             -- 8.本月的最后一天  
  29.             Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))  
  30.   
  31.             -- 9.本年的最后一天  
  32.             Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))  
  33.   
  34.             -- 10.本月的第一个星期一  
  35.             select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0)   
  36.   
  37.             select 本年第多少周=datename(week,getdate())  
  38.                   ,今天是周几=datename(weekday,getdate())  
  39.   
  40.             --  dateadd 在向指定日期加上一段时间的基础上,返回新的datetime值  
  41.             -- 向日期加上2天 或 增加1个月  
  42.                 select dateadd(day,2,'2004-10-15'--返回:2004-10-17 00:00:00.000  
  43.                 select dateadd(month,2,'2004-10-15'--返回:2004-12-17 00:00:00.000  
  44.   
  45.             --3. datediff 返回跨两个指定日期的日期和时间边界数。  
  46.                  select datediff(day,'2004-09-01','2004-09-18'--返回天数:17  
  47.                  select DateDiff(s,'2005-07-20','2005-7-25 22:56:32'--返回值为 514592 秒  
  48.                  select DateDiff(ms,'2005-07-20','2005-7-25 22:56:32'--返回值为 微秒  
  49.                  select DateDiff(d,'2005-07-20','2005-7-25 22:56:32'-- 返回值为 5 天  
  50.                  select DatePart(w,'2005-7-25 22:56:32')--返回值为 2 即星期一(周日为1,周六为7)  
  51.                  select DatePart('d','2005-7-25 22:56:32')--返回值为 25即25号  
  52.                  select DatePart('y','2005-7-25 22:56:32')--返回值为 206即这一年中第206天  
  53.                  select DatePart('yyyy','2005-7-25 22:56:32')--返回值为 2005即2005年  
  54.   
  55.             --DateDiff (interval,date1,date2) 以interval 指定的方式,  
  56.             --返回date2 与date1两个日期之间的差值 date2-date1  
  57.             --DateAdd (interval,number,date) 以interval指定的方式,加上number之后的日期  
  58.             --DatePart (interval,date) 返回日期date中,interval指定部分所对应的整数值  
  59.             --DateName (interval,date) 返回日期date中,interval指定部分所对应的字符串名称  
  60.   
  61.   
  62.     --                                                                                    ╔════════════════════╗  
  63.     -- =================================================================================  ║  当前时间函数      ║  
  64.     --                                                                                    ╚════════════════════╝   
  65.             -- 返回当前日期和时间  
  66.                select GETDATE()  
  67.   
  68.             --  返回代表指定日期的指定日期部分的整数。  
  69.                 select datepart(month'2004-10-15'--返回 月  
  70.                 select datepart(day'2004-10-15'--返回 日  
  71.                 select datepart(year, getdate()) --返回 年  
  72.                 select convert(varchar(8),getdate(),114)  -- 当前时间  
  73.                 select datename(weekday, getdate()) --返回:星期五  
  74.                 select datepart(weekday, getdate()) --返回:小写星期2-1  
  75.                 select convert(varchar(10),getdate(),120)  -- 当前日期  
  76.                 select datepart(S, '2004-10-15'--返回 月  
  77.             --  返回时间到豪秒  
  78.                 Select CONVERT(VARCHAR(30),GETDATE(),9)  
  79.   
  80.             --  获取当前日期,年、月、日、周、时、分、秒  
  81.                 select GETDATE() as '当前日期',  
  82.                 DateName(year,GetDate()) as '年',  
  83.                 DateName(month,GetDate()) as '月',  
  84.                 DateName(day,GetDate()) as '日',  
  85.                 DateName(dw,GetDate()) as '星期',  
  86.                 DateName(week,GetDate()) as '周数',  
  87.                 DateName(hour,GetDate()) as '时',  
  88.                 DateName(minute,GetDate()) as '分',  
  89.                 DateName(second,GetDate()) as '秒'  
  90.   
  91. print DateName(second,GetDate())+'1'  
  92.   
  93.   
  94.   
  95.   
  96.   
  97.   
  98.   
  99.             --  格式  
  100.   
  101.                 select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')  
  102.                 20040912110608  
  103.   
  104.                 select CONVERT(varchar(12) , getdate(), 111 )  
  105.                 2004/09/12  
  106.   
  107.                 select CONVERT(varchar(12) , getdate(), 112 )  
  108.                 20040912  
  109.   
  110.                 select CONVERT(varchar(12) , getdate(), 102 )  
  111.                 2004.09.12  
  112.   
  113.                 select CONVERT(varchar(12) , getdate(), 101 )  
  114.                 09/12/2004  
  115.   
  116.                 select CONVERT(varchar(12) , getdate(), 103 )  
  117.                 12/09/2004  
  118.   
  119.                 select CONVERT(varchar(12) , getdate(), 104 )  
  120.                 12.09.2004  
  121.   
  122.                 select CONVERT(varchar(12) , getdate(), 105 )  
  123.                 12-09-2004  
  124.   
  125.                 select CONVERT(varchar(12) , getdate(), 106 )  
  126.                 12 09 2004  
  127.   
  128.                 select CONVERT(varchar(12) , getdate(), 107 )  
  129.                 09 12, 2004  
  130.   
  131.                 select CONVERT(varchar(12) , getdate(), 108 )  
  132.                 11:06:08  
  133.   
  134.                 select CONVERT(varchar(12) , getdate(), 109 )  
  135.                 09 12 2004 1  
  136.   
  137.                 select CONVERT(varchar(12) , getdate(), 110 )  
  138.                 09-12-2004  
  139.   
  140.                 select CONVERT(varchar(12) , getdate(), 113 )  
  141.                 12 09 2004 1  
  142.   
  143.                 select CONVERT(varchar(12) , getdate(), 114 )  
  144.                 11:06:08.177  
  145.   
  146.     --                                                                                    ╔════════════════════╗  
  147.     -- =================================================================================  ║  数据库时间函数    ║  
  148.     --                                                                                    ╚════════════════════╝   
  149.   
  150.                 -- 查询最近一个月内的点击率大于100的记录数据:  
  151.                 select * from t_business_product where hit_count>100 and datediff(Dd,last_date,getdate())<=30 order by id desc  
  152.   
  153.                 -- 查询最近一周内的点击率大于100的记录数据:  
  154.                 select * from t_business_product where hit_count>100 and datediff(Dw,last_date,getdate())<=7 order by id desc  
  155.   
  156.                 -- 你可以使用LIKE来返回正确的记录。通过在日期表达式中包含通配符“%”,  
  157.                 -- 你可以匹配一个特定日期的所有时间。这里有一个例子:  
  158.                 --这个语句可以匹配正确的记录。因为通配符“%”代表了任何时间。  
  159.   
  160.                 Select * FROM weblog Where entrydate LIKE ‘Dec 25 2000%’  
  161.   
  162.     --                                                                                    ╔════════════════════╗  
  163.     -- =================================================================================  ║ CAST和CONVERT函数  ║  
  164.     --                                                                                    ╚════════════════════╝   
  165. select @@version  


转自:http://blog.csdn.net/dxnn520/article/details/8456653


猜你喜欢

转载自blog.csdn.net/u010673842/article/details/79113257