Solved

query between separate date fields

Posted on 2013-01-30
6
254 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
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:Nalinkumarbalaji
Comment Utility
Use the below one for date filtering

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

Author Comment

by:fwstealer
Comment Utility
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
Comment Utility
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 3

Expert Comment

by:Nalinkumarbalaji
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
In this article I will describe the Detach & Attach 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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now