1

SQL Server query to get values between (from date) to (to date) with from date and to date.

I had tried

select * 
from abc 
where 1 = 1 
  and entrydate between '"txt1.Text"' and '"txt2.Text"'

I get result between startdate and enddate but in need values of start date and enddate also

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
balram
  • 15
  • 4
  • hi i need in MS SQL, am using expressions (txt1 and txt2) not date – balram Jan 28 '17 at 05:19
  • "txt1.Text" and "txt2.Text" are not dates. I assume `entrydate` is a date or datetime column. So... yeah, if you get any data at all it will be purely coincidental. – Sam Axe Jan 28 '17 at 05:30
  • Not sure what you meant. Try to share a sample output and table structure as well. – AT-2017 Jan 28 '17 at 05:44

1 Answers1

0

If I had a @StartDate parameter and @EndDate parameter both as Datetimes, I would cast them to dates so you can get:

Select *
From ABC 
Where entrydate >= CAST(@StartDate as Date) 
  and entrydate < DATEADD(d, 1, CAST(@EndDate as Date))

Since the end date would be essentially midnight on the morning of the end date, to include it you have to advance it to the next day, then you can do a less than and the previous day is fully included.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mark Fitzpatrick
  • 1,624
  • 1
  • 11
  • 8