// Query all the data generated at every day Select * from table where datepart (HH, Date Field) = 8 // Query all records whose dates are 2006-7-6 Select * From cstrecord Where (datepart ([Day], optime) = 6) and (datepart ([year], optime) = 2006) and (Datepart ([month], optime) = 7) 1) Remove the hour, minute, and second Declare @ datetime Set @ = getdate () -- '2017-7-1 10:00:00' Select @, dateadd (day, datediff (day, 0, @), 0) 2) display the day of the week Select datename (weekday, getdate ()) 3) how to obtain the number of days in a month Declare @ M int Set @ M = 2 -- month Select datediff (day, '2017-'+ Cast (@ M as varchar) +'-15', '2017-'+ Cast (@ m + 1 as varchar) + '-15 ') In addition, get the number of days this month Select datediff (day, cast (month (getdate () as varchar) + '-' + Cast (month (getdate () Varchar) + '-15', cast (month (getdate () as varchar) +'-'+ Cast (month (getdate () + 1 as varchar) + '-15 ') Or use the script to calculate the last day of the month, and then use the day function area for the last day. Select Day (dateadd (MS,-3, dateadd (mm, datediff (M, 0, getdate () + 1, 0 ))) 4) determine whether a leap year is used: Select case day (dateadd (mm, 2, dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate (), 0 )))) When 28 then 'Year' else 'Year' end Or Select case datediff (day, datename (year, getdate () + '-02-01', dateadd (mm, 1, datename (Year, getdate () + '-02-01 ')) When 28 then 'Year' else 'Year' end 5) How many days of a quarter Declare @ M tinyint, @ time smalldatetime Select @ M = month (getdate ()) Select @ M = case when @ m between 1 and 3 then 1 When @ m between 4 and 6 then 4 When @ m between 7 and 9 then 7 Else 10 end Select @ time = datename (year, getdate () + '-' + convert (varchar (10), @ M) + '-01' Select datediff (day, @ time, dateadd (mm, 3, @ time )) 5) How many days of a quarter Each quarter is three months. It is regular and can be calculated using an expression instead of using case when: Set @ M = convert (INT, (@ m-1)/3) * 3 + 1 |