Solved

query between separate date fields

Posted on 2013-01-30
6
266 Views
Last Modified: 2013-01-30
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'
set @EndDate = dateadd(d,90,@StartDate)

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
Comment
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
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:Nalinkumarbalaji
ID: 38836014
Use the below one for date filtering

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

Author Comment

by:fwstealer
ID: 38836033
are you saying

declare @StartDate as date, @EndDate as date
set @StartDate = '1/14/2013'
set @EndDate = dateadd(d,90,@StartDate)

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

Author Comment

by:fwstealer
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 3

Expert Comment

by:Nalinkumarbalaji
ID: 38836048
Use this below one

declare @StartDate as datetime, @EndDate as datetime
set @StartDate = '1/14/2013'
set @EndDate = dateadd(d,90,@StartDate)

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

Author Comment

by:fwstealer
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'
set @EndDate = dateadd(d,90,@StartDate)

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

by:
Nalinkumarbalaji earned 500 total points
ID: 38836134
Run the below one and check

declare @StartDate as datetime, @EndDate as datetime
set @StartDate = '1/14/2013'
set @EndDate = Convert(Varchar,dateadd(d,90,@StartDate),101)

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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Varible Table 3 42
Text file into sql server 5 42
sql 2016 data tools breakdown.. 1 26
Checking for column changes SQL 2014 4 21
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question