SQL queries by a value of the date.

Source: Internet
Author: User

SQL queries by a value of the date.

-- Take 12:56:55 as an Example
-- Convert (nvarchar (10), CreateDate, 120) => 2013-12-10
-- DATEPART (month, CreateDate) => 12
-- DATEPART (year, CreateDate) = & gt; 2013

-- This is also possible
-- Year
Select datepart (YEAR, '2017-06-08 ')
Select datepart (yyyy, '2017-06-08 ')
Select datepart (yy, '2017-06-08 ')
-- Month
Select datepart (MONTH, '2017-06-08 ')
Select datepart (mm, '2017-06-08 ')
Select datepart (m, '2017-06-08 ')
-- Day
Select datepart (dd, '2017-06-08 ')
-- The day of the year
Select datepart (dy, '2017-06-08 ')
-- Quarter
Select datepart (qq, '2017-06-08 ')
-- The week of the year
Select datepart (wk, '2017-06-08 ')
-- Week
Select datepart (dw, '2017-06-08 ')

Select convert (VARCHAR (10), GETDATE (), 120) --
Select convert (VARCHAR (10), GETDATE (), 101) -- 07/13/2015

-- Group by day: 2013-01-01
Select convert (nvarchar (10), CreateDate, 120) as Times, ISNULL (sum (Unit), 0.0) as Drinking from pdt_Out
Group by convert (nvarchar (10), CreateDate, 120)
Go

-- Grouping by month: 2012-01
Select DATEPART (month, CreateDate) as Times, sum (Unit) as Totals from pdt_Out
Group by DATEPART (month, CreateDate)
Go

-- Group by year: 2013
Select DATEPART (year, CreateDate) as Times, sum (Unit) as Totals from pdt_Out
Group by DATEPART (year, CreateDate)
Go

Related Article

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.