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