SQL query of date fields

Source: Internet
Author: User
SQL query of date fields

// 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

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.