Solved

query between separate date fields

Posted on 2013-01-30
6
262 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
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
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.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Managing Columnstore Indexes 2 27
SQL Server Question 5 36
Gettg error - Please help Msg 252, Level 16, State 1, Line 1 3 28
How to search for strings inside db views 4 24
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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 video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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

11 Experts available now in Live!

Get 1:1 Help Now