Use SQL statements to query data for a certain time period

Source: Internet
Author: User
Tags date1

Use SQL statements to query data for a date in a week

SELECT * from Shoporder where DateDiff (Week,ordtime,getdate ()-1) =0//query date of the day on the first anniversary of the data

SELECT * from Shoporder where DateDiff (Day,ordtime,getdate ()-1) =0//query all data for the current day

SELECT * from A where DateDiff (D,datetime,getdate ()) <=30//First 30 days

SELECT * from A WHERE DATEDIFF (M, Shijian, GETDATE ()) <=1//Last month

--Day of enquiry:
SELECT * FROM info where DateDiff (dd,datetime,getdate ()) =0

--Query within 24 hours:
SELECT * FROM info where DateDiff (hh,datetime,getdate ()) <=24

--info is the table name, DateTime is the field value in the database

--Day of enquiry:
SELECT * FROM info where DateDiff (dd,datetime,getdate ()) =0

--Query within 24 hours:
SELECT * FROM info where DateDiff (hh,datetime,getdate ()) <=24

--info is the table name, DateTime is the field value in the database

SQL code
--the method of checking the same day record alternative
SELECT *
From J_gradeshop
WHERE (Gaddtime between CONVERT (DateTime, Left (GETDATE (), 10) + ' 00:00:00.000 ')
and CONVERT (datetime, Left (GETDATE (), 10) + ' 00:00:00.000 ') + 1)
ORDER by Gaddtime DESC

--the method of checking the same day record alternative
SELECT *
From J_gradeshop
WHERE (Gaddtime between CONVERT (DateTime, Left (GETDATE (), 10) + ' 00:00:00.000 ')
and CONVERT (datetime, Left (GETDATE (), 10) + ' 00:00:00.000 ') + 1)
ORDER by Gaddtime DESC

DATEDIFF function:

Grammar:


DATEDIFF (DatePart, StartDate, EndDate)

Note: EndDate minus startdate. If StartDate is later than EndDate, a negative value is returned.

If the result is outside the integer value range, DATEDIFF will produce an error. For milliseconds, the maximum number is 24 days and 20 hours 31分钟零23.647秒. For seconds, the maximum number is 68 years.

Methods that are computed across boundaries such as minutes, seconds, and milliseconds make the result specified by DATEDIFF consistent across all data types. The result is a signed integer value that is equal to the number of datepart boundaries across the first and second days. For example, the number of weeks between January 4 (Sunday) and January 11 (Sunday) is 1.


Can be tested in MSSQL again:

SQL code
--Two time difference is just 24.
--How to print
Print DateDiff (hh, ' 2009-1-1 0:0:0 ', ' 2009-1-2 0:0:0 ')

--How to query
Print DateDiff (hh, ' 2009-1-1 0:0:0 ', ' 2009-1-2 0:0:0 ')

--Two time difference is just 24.
--How to print
Print DateDiff (hh, ' 2009-1-1 0:0:0 ', ' 2009-1-2 0:0:0 ')

--How to query
Print DateDiff (hh, ' 2009-1-1 0:0:0 ', ' 2009-1-2 0:0:0 ')

SQL code
--Record of the month
SELECT * FROM table WHERE DateDiff (Month,[dateadd],getdate ()) =0

--This week's record
SELECT * FROM table WHERE DateDiff (Week,[dateadd],getdate ()) =0

--including this year, these query methods are the same

--Record of the month
SELECT * FROM table WHERE DateDiff (Month,[dateadd],getdate ()) =0

--This week's record
SELECT * FROM table WHERE DateDiff (Week,[dateadd],getdate ()) =0

--including this year, these query methods are the same

Time functions in SQL Server
1. Current system date, time
Select GETDATE ()


2. DateAdd returns a new datetime value based on adding a period of time to the specified date
Example: Add 2 days to the date
Select DATEADD (day,2, ' 2004-10-15 ')--return: 2004-10-17 00:00:00.000


3. DateDiff returns the number of date and time boundaries across two specified dates.
Select DateDiff (Day, ' 2004-09-01 ', ' 2004-09-18 ')--return: 17


4. DatePart returns an integer representing the specified date portion of the specified date.
SELECT DATEPART (month, ' 2004-10-15 ')--return 10


5. Datename returns a string representing the specified date part of the specified date
SELECT Datename (Weekday, ' 2004-10-15 ')--return: Friday


6. Day (), month (), year ()--can be compared with datepart

Select Current date =convert (varchar), GETDATE (), 120)
, Current time =convert (varchar (8), GETDATE (), 114)

Select Datename (DW, ' 2004-10-15 ')

Select how many weeks of the year =datename (week, ' 2004-10-15 ')
, today is a few weeks =datename (weekday, ' 2004-10-15 ')


function parameters/Functions
GetDate () returns the current date and time of the system
DateDiff (INTERVAL,DATE1,DATE2) returns the difference between Date2 and date1 two dates in the interval specified date2-date1
DATEADD (interval,number,date) with the date specified in interval, plus number
DatePart (interval,date) returns the integer value corresponding to the specified portion of the date, interval
Datename (interval,date) returns the name of the string that corresponds to the specified part of the date, interval

The setting values for the parameter interval are as follows:


Value abbreviations (SQL Server) Access and ASP descriptions
Year Yy yyyy 1753 ~ 9999
Quarter Qq Q Season 1 ~ 4
Month Mm M 1 ~ 12
Day of the year Dy y the number of days of the year, the day of the first 1-366
Day Dd D, 1-31
Weekday Dw W Day of the week, day of the week 1-7
Week Wk WW Week, week of the year 0 ~ 51
Hour Hh H 0 ~ 23
Minute Mi N min 0 ~ 59
Second Ss s seconds 0 ~ 59
Millisecond MS-MS 0 ~ 999

In Access and ASP, date () and now () are used to obtain system datetime, where Datediff,dateadd,datepart is also used in Access and ASP, and the usage of these functions is similar


Example:
1.GetDate () for SQL Server:select GetDate ()

2.DateDiff (' s ', ' 2005-07-20 ', ' 2005-7-25 22:56:32 ') returns a value of 514,592 seconds
DateDiff (' d ', ' 2005-07-20 ', ' 2005-7-25 22:56:32 ') returns a value of 5 days

3.DatePart (' W ', ' 2005-7-25 22:56:32 ') returns a value of 2 that is Monday (Sunday is 1, Saturday is 7)
DatePart (' d ', ' 2005-7-25 22:56:32 ') returns a value of 25, or 25th
DatePart (' y ', ' 2005-7-25 22:56:32 ') returns a value of 206 that is the No. 206 Day of the Year
DatePart (' yyyy ', ' 2005-7-25 22:56:32 ') returns a value of 2005 that is 2005

Description: Original address: Http://blog.csdn.net/hanjingjingpan/article/details/6022014#comments

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.