Home > OS >  Querying from SQL Server datetime conditions
Querying from SQL Server datetime conditions

Time:10-26

I have a datetime stored in SQL Server like '2021-10-12 18:46:31.047' and '2021-10-225 23:54:08.667' in one row..

How can I query from datetime by using date only not hours

WHERE str_day = '2021-10-23' AND end_day = '2021-10-24'

Help please

CodePudding user response:

Use date boundaries:

WHERE str_day >= '20211023'
  AND str_day < '20211024'
  AND end_day >= '20211024'
  AND end_day < '20211025'

CodePudding user response:

If your columns str_day and end_day are of time DATETIME (or DATETIME2(n)), then you can use something like this:

WHERE CAST(str_day AS DATE) = '2021-10-23' 
  AND CAST(end_day AS DATE) = '2021-10-24'

DATE is the date-only (no time portion) datatype - if your time portion is never needed, it might be useful to store those things as DATE from the beginning...

CodePudding user response:

Use this; this will work for many scenario and types

WHERE str_day = '23 OCT 2021' 
  AND end_day = '24 OCT 2021'
  • Related