Common date functions in SQL

Source: Internet
Author: User
Date used, that is, full spelling and abbreviations can all be expressed in parameters.
Year YY yyyy 1753   ~   9999  
Quarter qq q quarter 1   ~   4  
Month Mm m Month 1 ~   12  
Day   Of   Year Dy Y: the number of days in a year. 1 - 366  
Day Dd d, 1 - 31  
Weekday dw w day of the week, the day of the week 1 - 7  
Week wk WW week, week of the year 0   ~   51  
Hour hh h 0 ~   23  
Minute MI n minutes 0 ~   59  
Second SS s seconds 0   ~   59  
Millisecond MS - Millisecond 0   ~   999  
========================================================== ======================
1 : Getdate () Function
Returns the current time.
Select   Getdate ()

-- ----------------------------------------------------
2009 - 02 - 05   11 : 18 : 39.770

(The number of affected rows is1Rows)

========================================================== ============================
2 : Dateadd Function
The first parameter is the type to be added, such as mm, the second parameter, and the third parameter is where you want to add
Select   Dateadd (M, 2 , ' 2004-1-1 ' ) [ Datelihan ]
Datelihan
-- ----------------------------------------------------
2004 - 03 - 01   00 : 00 : 00.000

(The number of affected rows is1Rows)

Select   Dateadd (Yy, 1 , ' 2004-1-1 ' ) [ Datelihan ]
Datelihan
-- ----------------------------------------------------
2005 - 01 - 01   00 : 00 : 00.000

(The number of affected rows is 1 Rows)
========================================================== ========================================================== ===
3 : Datediff Function
Returns the difference value between two dates, parameter 2 and parameter 3. - Parameter 2 return type: parameter 1
Select   Datediff (DD, ' 2009-12-27 ' , ' 9-12-25 ' ) [ Datelihan ]
Datelihan
-- ---------
- 2

(The number of affected rows is 1 Rows)
Select   Datediff (Yy, ' 2007-12-27 ' , ' 9-12-25 ' ) [ Datelihan ]
Datelihan
-- ---------
2

(The number of affected rows is 1 Rows)
========================================================== ========================================================== =
4 : Datepart Function
Returns the value of parameter 1 corresponding to parameter 2.
Select   Datepart (Mm, ' 2008-08-07 ' ) [ Month ]
Select   Datepart (Yy, ' 2008-08-07 ' ) [ Year ]
Month
-- ---------
8

(The number of affected rows is1Rows)

Year
-----------
2008

(The number of affected rows is1Rows)

========================================================== ========================================================== ==========
5 : Datename Function
Returns a string representing the specified date part of a specified date.
Select   Datename (Weekday, ' ' ) [ Week ]
Week
-- ----------------------------
Thursday

(The number of affected rows is1Rows)

========================================================== ========================================================== ========

6 : the day function
returns an integer indicating the date specified by the parameter, which is the day of the month
select day ( ' 2009-2-5 ' ) [ day ]
day
-- ---------
5

(the number of affected rows is 1 line)
=================================================== ========================================================== =============

7 : the month function
returns an integer indicating the date of the Year of the specified parameter
select month ( ' 2007-04-30 ' );
-- ---------
4

(the number of affected rows is 1 line)
=================================================== ========================================================== ===

8: Year Function
Returns an integer indicating the year of the date specified by the parameter.
Select Year('2007-04-30');
-----------
2007

(The number of affected rows is 1 Rows)
========================================================== ==========================================
For example, you want to query records less than 10 days ago.
Select   *   From Tablename Where   Datediff ( Day , Your Date field, Getdate ()) < 10
For example, you want to query records within a period of time.
Select   *   From Talbename Where Your Date Field Between Date 1 And Date 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.