1、獲取當前時間 select getdate()
2、截取需要的值
select datepart(year,getdate())
select datepart(month,getdate())
select datepart(day,getdate())
select datepart(hour,getdate())
select datepart(minute,getdate())
select datepart(second,getdate())
select datepart(week,getdate())
3、在日期中添加或減去指定的時間間隔
select dateadd(year,3,getdate()) --獲取當前時間,往后推遲三年
select dateadd(month,3,getdate()) --獲取當前時間,往后推遲三個月
select dateadd(day,3,getdate()) --獲取當前時間,往后推遲三天
select dateadd(hour,3,getdate()) --獲取當前時間,往后推遲三小時
select dateadd(minute,3,getdate()) --獲取當前時間,往后推遲三分鐘
select dateadd(second,3,getdate()) --獲取當前時間,往后推遲三秒鐘
4、返回兩個日期之間的時間
select datediff(year,'2001-08-19',getdate()) --2001-08-19和當前時間之間差多少年
select datediff(month,'2001-08-19',getdate()) --2001-08-19和當前時間之間差多少月
select datediff(day,'2001-08-19',getdate()) --2001-08-19和當前時間之間差多少天
5、用不同的格式顯示日期/時間
select convert(char,getdate(),8) --顯示當前時-分-秒
select convert(char,getdate(),10) --顯示當前月-日-年,顯示形式“08-19-11”
select convert(char,getdate(),11) --顯示當前年-月-日,顯示形式“11/08/19”
select convert(char,getdate(),14) --顯示當前時-分-秒-毫秒,顯示形式“14:54:57:090”
注:參數1-14隨意。
===========================================================================================
http://database.51cto.com/art/201009/226223.htm
使用sqlserver日期函數獲取當前日期
使用sqlserver日期函數獲取當前日期和時間,通過使用convert(varchar(10),getdate(),120)的方法可以得到當前的日期,不需要時間部分,或者不需要日期只要時間部分。
AD:
使用
sqlserver日期函數中的getdate()可以獲取當現的日期,下面就將為您介紹這種使用sqlserver日期函數獲取當前日期的方法,供您參考,希望對您學習sqlserver日期函數有所啟迪。
但是如果我們只需要得到當前的日期,不需要時間部分,或者不需要日期只要時間部分,再或者我要只要字段中的日期以某種形式顯示,應該怎么操作呢?
可以使用convert(varchar(10),getdate(),120)這樣的方法來實現,其中varchar(10)定義的是你要的字段的長度,當然長度的不同返回的也會不的,如果我們只要日期部分,設成10正好為日期長度,如果設成19則正好可以讀到時間部分。
現在介紹最后面代的參數,分別以代的參數及實例效果說明
參數 | 結果 |
100 | 05 8 2006 9:27PM |
101 | 05/08/2006 |
102 | 2006.05.08 |
103 | 08/05/2006 |
104 | 08.05.2006 |
105 | 08-05-2006 |
106 | 08 05 2006 |
107 | 05 08, 2006 |
108 | 21:30:51 |
109 | 05 8 2006 9:31:11 |
110 | 05-08-2006 |
111 | 2006/05/08 |
112 | 20060508 |
113 | 08 05 2006 21:31:59 |
114 | 21:33:06:503 |
120 | 2006-05-08 21:33:38 |
【編輯推薦】
初級sql select語法
sql server死鎖的檢測方法
SQL SERVER字段類型的說明
SQL Server安全解析
Sql Server日期時間函數