Sql Server日期查詢-SQL查詢今天、昨天、7天內、30天

來源:互聯網
上載者:User

標籤:arc   varchar   目前時間   server   多少   pre   html   var   第一天   

記錄轉載自:http://www.cnblogs.com/suruozhong/p/5974595.html

今天的所有資料:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())=0昨天的所有資料:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())=17天內的所有資料:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())<=730天內的所有資料:select * from 表名 where DateDiff(dd,datetime類型欄位,getdate())<=30本月的所有資料:select * from 表名 where DateDiff(mm,datetime類型欄位,getdate())=0本年的所有資料:select * from 表名 where DateDiff(yy,datetime類型欄位,getdate())=0  查詢今天是今年的第幾天: select datepart(dayofyear,getDate())查詢今天是本月的第幾天:1. select datepart(dd, getDate())                         2.select day(getDate())查詢本周的星期一日期是多少 (注意:指定日期不能是周日,如果是周日會計算到下周一去。所以如果是周日要減一天) SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0) 查詢昨天日期:select convert(char,dateadd(DD,-1,getdate()),111)  //111是樣式號,(100-114) 查詢本月第一天日期:Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) as firstday查詢本月最後一天日期:Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) as lastday      //修改-3的值會有相應的變化 本月有多少天:select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast((cast(year(getdate()) as varchar)+‘-‘+cast(month(getdate()) as varchar)+‘-01‘ ) as datetime )))) 求兩個時間段相差幾天:select datediff(day,‘2012/8/1‘,‘2012/8/20‘) as daysum在指定的日期上±N天:select convert(char,dateadd(dd,1,‘2012/8/20‘),111) as riqi    //輸出2012/8/21在指定的日期上±N分鐘:select dateadd(mi,-15,getdate())  //查詢目前時間15分鐘之前的日期?

 

Sql Server日期查詢-SQL查詢今天、昨天、7天內、30天

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.