Before we know that SQL has the DateDiff () function, we need to calculate the date and time difference. I will directly put it in a program like php, asp, and asp.net for calculation, now I will share with you how to use the DateDiff function to add or subtract a date plan.
Before we know that SQL has the DateDiff () function, we need to calculate the date and time difference. I will directly put it in a program like php, asp, and asp.net for calculation, now I will share with you how to use the DateDiff function to add or subtract a date plan.
Syntax
DateDiff (interval, date1, date2 [, firstdayofweek [, firstweekofyear])
The DateDiff function syntax contains the following naming parameters:
This function calculates the number of date parts between two specified dates. The result is a signed integer equal to (date2-date1) in the date section.
When the result is not an even multiple of the date part, DATEDIFF is truncated instead of rounded.
When day is used as the date part, DATEDIFF returns the number of midnight between two specified times (including the second date but not the first date.
When month is used as the date part, DATEDIFF returns the number of the first day of the month between two dates (including the second date but not the first date.
When week is used as the date part, DATEDIFF returns the number of Sundays between two dates (including the second but not the first.
Use datediff to obtain the timestamp
The Code is as follows: |
|
1> SELECT datediff (ss, '2017-01-01 ', GETDATE () as time 2> go Time 1361988498 (1 row affected) 1> |
Datediff
Ss indicates in seconds
The timestamp is calculated from this date.
Getdate () Current Time
The number of seconds between the current read time and is the current timestamp.
Of course, if you can use getdate () to read the current date, you can also use other languages to convert it into a timestamp, such as js and php.
Of course, the datediff function is very powerful, for example, to calculate the number of months between two dates
The Code is as follows: |
|
1> SELECT datediff (mm, '2017-01-01 ', GETDATE () as time 2> go time13 (1 row affected) |
The following statement returns 1:
The Code is as follows: |
|
Select datediff (hour, ''4: 00am'', ''5: 50am '') |
The following statement returns 102:
The Code is as follows: |
|
Select datediff (month, ''1987/05/02 '', ''1995/11/15 '') |
The following statement returns 0:
The Code is as follows: |
|
Select datediff (day, ''00: 00 '', ''23: 59 '') |
The following statement returns 4:
The Code is as follows: |
|
Select datediff (day, '''2014/1/19 ''', ''2014/2/23 '') |
The following statement returns 0:
The Code is as follows: |
|
Select datediff (month, ''1999/07/19 '', ''1999/07/23 '') |
The following statement returns 1:
The Code is as follows: |
|
Select datediff (month, ''1999/07/19 '', ''1999/08/23 '') |