When using Sql Server to query database, we often need to query date format data. There are certain requirements for date format in sql language. By modifying the last parameter in convert, we can return the time when the format is not clear. The specific implementation is as follows:
1 Select CONVERT(varchar(45), GETDATE(), 0): 08 24 2018 08:22AM 2 Select CONVERT(varchar(45), GETDATE(), 1): 08/24/18 3 Select CONVERT(varchar(45), GETDATE(), 2): 18.08.24 4 Select CONVERT(varchar(45), GETDATE(), 3): 24/08/24 5 Select CONVERT(varchar(45), GETDATE(), 4): 24.08.18 6 Select CONVERT(varchar(45), GETDATE(), 5): 24-08-18 7 Select CONVERT(varchar(45), GETDATE(), 6): 24 08 18 8 Select CONVERT(varchar(45), GETDATE(), 7): 08 24, 18 9 Select CONVERT(varchar(45), GETDATE(), 8): 08:22:46 10 Select CONVERT(varchar(45), GETDATE(), 9): 08 24 2018 08:22:46:625AM 11 Select CONVERT(varchar(45), GETDATE(), 10): 08-24-18 12 Select CONVERT(varchar(45), GETDATE(), 11): 18/08/24 13 Select CONVERT(varchar(45), GETDATE(), 12): 180824 14 Select CONVERT(varchar(45), GETDATE(), 13): 24 08 2018 08:22:46:862 15 Select CONVERT(varchar(45), GETDATE(), 14): 08:22:46:967 16 Select CONVERT(varchar(45), GETDATE(), 20): 2018-08-24 08:22:47 17 Select CONVERT(varchar(45), GETDATE(), 21): 2018-08-24 08:22:47.157 18 Select CONVERT(varchar(45), GETDATE(), 22): 08/24/18 08:22:47 AM 19 Select CONVERT(varchar(45), GETDATE(), 23): 2018-08-24 20 Select CONVERT(varchar(45), GETDATE(), 24): 08:22:47 21 Select CONVERT(varchar(45), GETDATE(), 25): 2018-08-24 08:22:47.350 22 Select CONVERT(varchar(45), GETDATE(), 100): 08 24 2018 08:22AM 23 Select CONVERT(varchar(45), GETDATE(), 101): 08/24/2018 24 Select CONVERT(varchar(45), GETDATE(), 102): 2018.08.24 25 Select CONVERT(varchar(45), GETDATE(), 103): 24/08/2018 26 Select CONVERT(varchar(45), GETDATE(), 104): 24.08.2018 27 Select CONVERT(varchar(45), GETDATE(), 108): 24-08-2018 28 Select CONVERT(varchar(45), GETDATE(), 118): 24 08 2018 29 Select CONVERT(varchar(45), GETDATE(), 107): 08 24, 2018 30 Select CONVERT(varchar(45), GETDATE(), 108): 08:22:49 31 Select CONVERT(varchar(45), GETDATE(), 109): 08 24 2018 08:22:49:153AM 32 Select CONVERT(varchar(45), GETDATE(), 110): 08-24-2018 33 Select CONVERT(varchar(45), GETDATE(), 111): 2018/08/24 34 Select CONVERT(varchar(45), GETDATE(), 112): 20180824 35 Select CONVERT(varchar(45), GETDATE(), 113): 24 08 2018 08:22:49:426 36 Select CONVERT(varchar(45), GETDATE(), 114): 08:22:47:375 37 Select CONVERT(varchar(45), GETDATE(), 120): 2018-08-24 08:22:47 38 Select CONVERT(varchar(45), GETDATE(), 121): 2018-08-24 08:22:47.655 39 Select CONVERT(varchar(45), GETDATE(), 126): 2018-08-24T08:22:47.125
In addition, we sometimes need to get the current date a few days later, just need to replace the getdate() function in the above code, the specific implementation is as follows:
1 //Only use dateadd()The function replaces the original getdate()Function, where getdate()The current date of acquisition is 2018-08-24 2 //This is the current date plus seven days. 3 Select CONVERT(Varchar(45),dateadd(day,7,,getdate()),23):The result was 2018.-08-31 4 Select CONVERT(Varchar(45),dateadd(dd,7,,getdate()),23):The result was 2018.-08-31 5 6 //Current date plus 7 months 7 Select CONVERT(Varchar(45),dateadd(month,7,,getdate()),23):The result was 2019-03-31 8 Select CONVERT(Varchar(45),dateadd(mm,7,,getdate()),23):The result was 2019-03-31 9 //If the above date is added to six months, the result will be 2019.-02-28 10 11 //Current date plus seven years 12 Select CONVERT(Varchar(45),dateadd(year,7,,getdate()),23):The result was 2025.-08-31 13 Select CONVERT(Varchar(45),dateadd(yy,7,,getdate()),23):The result was 2025.-08-31