使用getdate 獲取不同格式的日期

2021-08-25 16:35:50 字數 3434 閱讀 8906

在使用sql server查詢資料庫時,我們經常會需要查詢日期格式的資料,對於日期在sql語言中的格式有一定的要求,通過修改convert中的最後一位引數,可以返回不通格式的時間,具體實現如下:

select convert(varchar(45), getdate(), 0): 08 24 2018 08:22am 

select convert(varchar(45), getdate(), 1): 08/24/18

select convert(varchar(45), getdate(), 2): 18.08.24

select convert(varchar(45), getdate(), 3): 24/08/24

select convert(varchar(45), getdate(), 4): 24.08.18

select convert(varchar(45), getdate(), 5): 24-08-18

select convert(varchar(45), getdate(), 6): 24 08 18

select convert(varchar(45), getdate(), 7): 08 24, 18

select convert(varchar(45), getdate(), 8): 08:22:46

select convert(varchar(45), getdate(), 9): 08 24 2018 08:22:46:625am

select convert(varchar(45), getdate(), 10): 08-24-18

select convert(varchar(45), getdate(), 11): 18/08/24

select convert(varchar(45), getdate(), 12): 180824

select convert(varchar(45), getdate(), 13): 24 08 2018 08:22:46:862

select convert(varchar(45), getdate(), 14): 08:22:46:967

select convert(varchar(45), getdate(), 20): 2018-08-24 08:22:47

select convert(varchar(45), getdate(), 21): 2018-08-24 08:22:47.157

select convert(varchar(45), getdate(), 22): 08/24/18 08:22:47 am

select convert(varchar(45), getdate(), 23): 2018-08-24

select convert(varchar(45), getdate(), 24): 08:22:47

select convert(varchar(45), getdate(), 25): 2018-08-24 08:22:47.350

select convert(varchar(45), getdate(), 100): 08 24 2018 08:22am

select convert(varchar(45), getdate(), 101): 08/24/2018

select convert(varchar(45), getdate(), 102): 2018.08.24

select convert(varchar(45), getdate(), 103): 24/08/2018

select convert(varchar(45), getdate(), 104): 24.08.2018

select convert(varchar(45), getdate(), 108): 24-08-2018

select convert(varchar(45), getdate(), 118): 24 08 2018

select convert(varchar(45), getdate(), 107): 08 24, 2018

select convert(varchar(45), getdate(), 108): 08:22:49

select convert(varchar(45), getdate(), 109): 08 24 2018 08:22:49:153am

select convert(varchar(45), getdate(), 110): 08-24-2018

select convert(varchar(45), getdate(), 111): 2018/08/24

select convert(varchar(45), getdate(), 112): 20180824

select convert(varchar(45), getdate(), 113): 24 08 2018 08:22:49:426

select convert(varchar(45), getdate(), 114): 08:22:47:375

select convert(varchar(45), getdate(), 120): 2018-08-24 08:22:47

select convert(varchar(45), getdate(), 121): 2018-08-24 08:22:47.655

select convert(varchar(45), getdate(), 126): 2018-08-24t08:22:47.125

另外,我們有時候需要獲取當前日期往後推幾天的時間,只需要把上面**中的getdate()函式做替換就行了,具體實現如下:

//僅用dateadd()函式替換了原來的getdate()函式,其中getdate()獲取的當前日期為2018-08-24

//這個為當前日期加上7天

select convert(varchar(45),dateadd(day,7,,getdate()),23):結果為2018-08-31

select convert(varchar(45),dateadd(dd,7,,getdate()),23):結果為2018-08-31

//當前日期加上7個月

select convert(varchar(45),dateadd(month,7,,getdate()),23):結果為2019-03-31

select convert(varchar(45),dateadd(mm,7,,getdate()),23):結果為2019-03-31

//若如上日期加上六個月,則結果為2019-02-28

//當前日期加上7年

select convert(varchar(45),dateadd(year,7,,getdate()),23):結果為2025-08-31

select convert(varchar(45),dateadd(yy,7,,getdate()),23):結果為2025-08-31

Python 獲取不同格式的時間

導包 import time 普通的時間 import time 這裡可以根據需要選擇寫入對應時間的字元如 y表示年 print time.strftime y m d a h m s 24小時格式 print time.strftime i m s 12小時格式結果 2018 06 08 frid...

php 獲取HTTP POST中不同格式的資料

http協議中的post 方法有多中格式的資料協議,在http的head中用不同的content type標識.常用的有 multipart form data,這個是用來上傳檔案的,在http的head中是content type multipart form data boundary webk...

flash讀取不同格式xml

xml格式資料兩種寫法如下 第一種 宣告省略,不影響flash讀取 1第乙個 絕對第乙個屬性 2第二個 絕對第二個屬性 第二個例1 課本.xml 課本 書名 語文這是個很簡單的xml文件。如果想增加其他資訊,可以繼續修改這個xml文件 課本 具體 書名 語文 具體 書名 數學 具體 書名 歷史 具體...