Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am Selecting the Data within two date start_date and End_date,
Start_date and End_date in between dates are not Existing in database.
how to get date within two dates.

What I have tried:

select STATUS,SELECT_DATE from PA_TIME_SCHEDULING_table where 
              SELECT_DATE between '2018/04/17' and '2018/04/19'
Posted
Updated 18-Apr-18 17:23pm

One method of achieving this is as follows;
NOTE: The following is just a demonstration of how it can be achieved and will need to be edited for your requirements
SQL
DECLARE @dteStart DATETIME
DECLARE @dteEnd DATETIME

SET @dteStart = '20180501'
SET @dteEnd = '20180514'

CREATE TABLE #TempDates
(
	[TheDate] DATETIME
)

WHILE @dteStart < DATEADD(day, 1, @dteEnd)
BEGIN
	INSERT INTO #TempDates([TheDate]) VALUES(@dteStart)
	SET @dteStart = DATEADD(day, 1, @dteStart)
END

SELECT * FROM #TempDates

DROP TABLE #TempDates


Kind Regards
 
Share this answer
 
If the SELECT_DATE is stored as a date in PA_TIME_SCHEDULING_table try explicitly converting the start and end dates to date. You should never trust on implicit conversion since it's affected by database settings,

For example
SQL
select STATUS,SELECT_DATE 
from PA_TIME_SCHEDULING_table where 
SELECT_DATE between convert(date, '2018/04/17', 111) and convert(date, '2018/04/19', 111)


If the SELECT_DATE is stored as a varchar, then you could achieve the correct result by converting it into a date in the where clause, but instead of doing that, I would recommend changing the data type into date.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900