Search record time as a condition (Case)

A net friend asked:

 

 

Insus.NET achieve three questions are as follows:

Create a table, and prepare some data:

 

CREATE TABLE [dbo].[Q_Order] ([Order_Date] DATETIME )

INSERT INTO [dbo].[Q_Order] ([Order_Date]) 
VALUES 
    ('2019-04-16 09:45:00.097'),
    ('2019-04-19 12:05:23.092'),
    ('2019-04-25 05:05:04.000'),
    ('2019-05-03 20:25:00.492'),
    ('2019-05-07 21:09:45.106'),
    ('2019-05-09 17:55:00.083'),
    ('2019-05-10 03:27:00.000'),
    ('2019-05-11 14:20:44.113'),
    ('2019-05-16 09:33:16.106'),
    ('2019-05-18 23:15:39.997'),
    ('2019-05-20 12:06:46.765'),
    ('2019-05-20 15:31:26.881'),
    ('2019-05-20 22:40:22.094'),
    ('2019-05-26 09:43:47.392'),
    ('2019-06-01 01:34:41.654'),
    ('2019-06-02 08:07:06.003')
Source Code

 

Search record in May 2019, as long as the query is equal to the year 2019, five of the month on OK.

 

- query the record in May 2019: 
the SELECT *  the FROM  [ dbo ] . [ Q_Order ]  the WHERE  YEAR ( [ Order_Date ] ) =  2019  the AND   MONTH ( [ Order_Date ] ) =  5
Source Code

 

查询某段时间之间的记录,只要查询的字段大于等于开始日期,并且小于结束日期加一天。如下面结束日期为23号,加一天之后就是24号,只要小于24号的,均为23号结束的记录。

 

--查询某段时间之间的记录:
DECLARE @s_date DATE = '2019-05-08',@e_date DATE = '2019-05-23'
SELECT * FROM [dbo].[Q_Order] WHERE [Order_Date] >= @s_date AND [Order_Date] < DATEADD(D,1,@e_date)
Source Code

 

查询某一天的记录,这个方法法,跟上面查询时间段的方法一样:

 

--查询某一天的记录
DECLARE @q_date DATE = '2019-05-20'
SELECT * FROM [dbo].[Q_Order] WHERE [Order_Date] >= @q_date AND [Order_Date] < DATEADD(D,1,@q_date)
Source Code

 

Guess you like

Origin www.cnblogs.com/insus/p/10962090.html