Solved

# query between separate date fields

Posted on 2013-01-30
267 Views
i need to query between two datetime columns. Start date is given and need to calculate 90 days out from that date.

declare @StartDate as date, @EndDate as date
set @StartDate = '1/14/2013'

query
where
cast(StartTimeLocal as Date) >= @StartDate
and cast(EndTimeLocal as Date) <=  @EndDate

problem is that i don't see all the rows. if i comment out: and cast(EndTimeLocal as Date) <=  @EndDate I see the rows.

example:
start date of 2013-01-14 06:00:00.000
end date of 2013-08-02 00:59:00.000

this row only shows when i comment out: and cast(EndTimeLocal as Date) <=  @EndDate otherwise it doesn't show.

how do i find the rows between those dates?
0
Question by:fwstealer
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 3

Expert Comment

ID: 38836014
Use the below one for date filtering

Convert(Varchar,StartTimeLocal,101) >= @StartDate
0

Author Comment

ID: 38836033
are you saying

declare @StartDate as date, @EndDate as date
set @StartDate = '1/14/2013'

select StartTimeLocal, EndTimeLocal from Schedules
where
Convert(Varchar,StartTimeLocal,101) >= @StartDate
and
Convert(Varchar,StartTimeLocal,101) <= @EndDate
0

Author Comment

ID: 38836045
this still didn't show the row I'm looking for

select StartTimeLocal, EndTimeLocal from Schedules
where
Convert(Varchar,StartTimeLocal,101) >= @StartDate
and
Convert(Varchar,EndTimeLocal,101) <= @EndDate
0

LVL 3

Expert Comment

ID: 38836048
Use this below one

declare @StartDate as datetime, @EndDate as datetime
set @StartDate = '1/14/2013'

select StartTimeLocal, EndTimeLocal from Schedules
where
Convert(Varchar,StartTimeLocal,101) >= @StartDate
and
Convert(Varchar,StartTimeLocal,101) <= @EndDate
0

Author Comment

ID: 38836102
that shows the row I need but also shows beyond 90 days

declare @StartDate as datetime, @EndDate as datetime
set @StartDate = '1/14/2013'

select @StartDate, @EndDate
--startdate = 2013-01-14
--enddate = 2013-04-14

this query goes way beyond the end date by showing data in the month of august:
select StartTimeLocal, EndTimeLocal from Schedules
where
Convert(Varchar,StartTimeLocal,101) >= @StartDate
and
Convert(Varchar,StartTimeLocal,101) <= @EndDate

i just want to show all the rows between startdate and enddate not beyond the enddate
0

LVL 3

Accepted Solution

ID: 38836134
Run the below one and check

declare @StartDate as datetime, @EndDate as datetime
set @StartDate = '1/14/2013'

select StartTimeLocal, EndTimeLocal from Schedules
where
Convert(Varchar,StartTimeLocal,101) >= @StartDate
and
Convert(Varchar,StartTimeLocal,101) <= @EndDate
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

get_systemdrive info from tsql? 1 26
MS SQL Server select from Sub Table 14 49
How to keep a record with the highest value 3 57
T-SQL: Need to trim a single leading space 7 60
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
###### Suggested Courses
Course of the Month7 days, 11 hours left to enroll