SQL query for data within one year

select * from ShopOrder wheredatediff(week,ordTime,getdate()-1)=0 //Query the data of the first anniversary of the current date
select * from ShopOrder wheredatediff(day,ordTime,getdate()-1)=0 //Query the current day All data of
SELECT * FROM A where datediff(d,datetime,getdate()) <=30//first 30 days
SELECT * FROM A WHERE DATEDIFF(m, shijian, GETDATE())<=1 //previous
month- - Query the day:
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
--query the day:
select * from info where DateDiff(dd,datetime,getdate())=0 --query
within 24 hours:
select * from info whereDateDiff( hh,datetime,getDate())<=24
--info is the table name, datetime is the field value in the database
Sql code --an
alternative method to query the records of the day
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
-- query the record of the current day Method
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:
syntax:

DATEDIFF ( datepart , startdate , enddate )

Note: enddate minus startdate. A negative value is returned if startdate is later than enddate.
DATEDIFF will generate an error if the result is outside the range of integer values. For milliseconds, the maximum number is 24 days, 20 hours, 31 minutes, and 23.647 seconds. For seconds, the maximum number is 68 years.
The method of computing across boundaries such as minutes, seconds, and milliseconds makes the result specified by DATEDIFF consistent across all data types. The result is a signed integer value equal to the number of datepart boundaries across the first and second date periods. For example, the week number between January 4 (Sunday) and January 11 (Sunday) is 1.

You can test it in MSSQL:
Sql code
-- the difference between the two times is exactly 24
--
print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0 ')

--The way of query
print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0') --the
difference between the two times is exactly 24 --the
way of printing
print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0') --The
query method
print dateDiff(hh,'2009-1-1 0:0: 0','2009-1-2 0:0:0')
Sql code
--this month record
SELECT * FROM table WHERE datediff(month,[dateadd],getdate())=0

--This week record
SELECT * FROM table WHERE datediff(week,[dateadd],getdate())=0

--Including this year, the query methods are the same --this
month records
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, the query methods are the same
Time function in sql server
1. Current system date and time
select getdate ()

2. dateadd returns a new datetime value based on adding a period of time to the specified date.
For 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 part of the specified date.
SELECT DATEPART(month, '2004-10-15') -- returns 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() -- you can compare it with datepart
select current date=convert(varchar(10),getdate(),120)
, current time=convert(varchar(8),getdate( ),114)
select datename(dw,'2004-10-15')
select how many weeks in this year=datename(week,'2004-10-15')
, today is the day of the week=datename(weekday,'2004-10 -15')

Function parameter/function
GetDate( ) Returns the current date and time of the system
DateDiff (interval,date1,date2) Returns the difference between date2 and date1 in the way specified by interval date2-date1
DateAdd (interval,number,date ) In the way specified by interval, add the date after the number
DatePart (interval,date) Returns the integer value corresponding to the specified part of
interval in the date date, DateName (interval,date) Returns the character corresponding to the specified part of interval in the date and date The setting value of the string name
parameter interval is as follows:

Value Abbreviation (Sql Server) Access and ASP Description
Year Yy yyyy Year 1753 ~ 9999
Quarter Qq q Quarter 1 ~ 4
Month Mm m Month 1 ~ 12
Day of year Dy y Day of the year, the day of the year 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 Min Min Minutes 0 ~ 59
Second Ss s Seconds 0 ~ 59
Millisecond Ms - Milliseconds 0 ~ 999
Use date() and now() to obtain system date and time in access and asp; DateDiff, DateAdd, DatePart can also be used in Access and asp , the usage of these functions is also similar

For example:
1.GetDate() for sql server :select GetDate()
2.DateDiff('s','2005-07-20','2005-7-25 22:56:32') The return value is 514592 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') The return value is 2, that is, Monday (Sunday is 1, Saturday is 7)
DatePart('d','2005-7-25 22:56:32') The return value is 25, which is the 25th
DatePart(' y','2005-7-25 22:56:32') The return value is 206, which is the 206th day of the year
DatePart('yyyy','2005-7-25 22:56:32') The return value is 2005 is 2005

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324890613&siteId=291194637