Ayushman Bokde
Ayushman Bokde

Reputation: 27

Is there any way to shorten this SQL Query

SELECT * FROM userfeedback WHERE
    DATE LIKE '2020-07%' OR
    DATE LIKE '2020-08%' OR
    DATE LIKE '2020-09%' OR
    DATE LIKE '2020-10%' OR
    DATE LIKE '2020-11%' OR
    DATE LIKE '2020-12%' OR
    DATE LIKE '2021-01%' OR
    DATE LIKE '2021-02%' OR
    DATE LIKE '2021-03%' OR
    DATE LIKE '2021-04%' OR
    DATE LIKE '2021-05%' OR
    DATE LIKE '2021-06%';

Note : Here DATE is a column name which is of type Timestamp.

Upvotes: 0

Views: 64

Answers (2)

eshirvana
eshirvana

Reputation: 24603

here is one way :

SELECT * FROM userfeedback 
WHERE DATE between '2020-07-01' and '2021-06-30'

side note : date is a keyword , better not to be used as object names (clumn name , etc)

Upvotes: 2

Aleix CC
Aleix CC

Reputation: 2099

If I understood correctly, you want to retrieve all the data from July 2020 until June 2021. Since you stated that the DATE datatype is timestmap, what you can do is the following:

SELECT * 
FROM userfeedback 
WHERE 
  DATE >= '2020-07-01' AND
  DATE < '2021-07-01' 

PS: I'd avoid using BETWEEN, since it can give undesired results when using Timestamps.

Upvotes: 2

Related Questions