SQL date operations and methods for obtaining only dates

Source: Internet
Author: User

 

Use of datepart () Functions
* The datepart () function allows you to conveniently retrieve all parts of the period.
* Date: 18:15:36. 513
* YY: 2006 of the year
* Mm: 7 months
* DD: 2 days in the month
* DY: 183 days in the year
* WK: week 27 in the year
* DW: The day in the week.
* QQ: Quarter 3 of the year
* Hh: 18 hours
* Mi: 15 minutes
* Ss: 36 seconds
* The following simple statements demonstrate the obtained results:
*/


Select
Getdate ()
Select
Datepart (mm, getdate ())
Select
Datepart (YY, getdate ())
Select
Datepart (DD, getdate ())
Select
Datepart (dy, getdate ())
Select
Datepart (wk, getdate ())
Select
Datepart (DW, getdate ())
Select
Datepart (QQ, getdate ())
Select
Datepart (HH, getdate ())
Select
Datepart (MI, getdate ())
Select
Datepart (SS, getdate ())

Select
Datediff (DD, getdate (), '2014/1/123') -- calculates the number of days from today
Select
Datediff (mm, getdate (), '2014/1/123') -- calculates the number of months from today till.
Select
Datename (mm, getdate () -- get the month of today in the form of a string instead of a number (07)


Select
Dateadd (DD, 30, getdate () -- add 30 days to the current date
Select
Dateadd (mm, 3, getdate () -- add 3 months to the current date
Select
Dateadd (YY, 1, getdate () -- add one year to the current date

Select
Dateadd (QQ, datediff (QQ, 0, getdate ()),
0) -- get the first day of the quarter
Select
Dateadd (mm,-3, dateadd (QQ, datediff (QQ, 0, getdate (), 0 ))
-- Get the first day of the master and quarter

Use SQL to retrieve only the date

You can use the getdate () method to obtain the date plus time. However, if we only need to get the current date, do not need the time part, or do not need the date as long as the time part, or I want to display the date in the field in some form, what should I do? You can use convert (varchar (10), getdate (), 120) to implement this. varchar (10) defines the length of the field you want, of course, different returns of different lengths will not be returned. If we set the date part to 10, it is the date length. If we set it to 19, we can read the time part. Now we will introduce the last generation of parameters, namely the generation parameters and instance effect descriptions.
Parameters Result
100 05 8 2006 pm
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,200 6
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 21:33:38

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.