Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir, i have a sql table in which 3 column and its data show like:-


EmpID -- FromDate -- ToDate
101 -- '01/AUg/2015' -- '03/Aug/2015'
101 -- '05/AUg/2015' -- '06/AUg/2015'


Now i need i sql query to show the data from that table like :-

EmpID -- Dated
101 -- '01/AUg/2015'
101 -- '02/AUg/2015'
101 -- '03/AUg/2015'
101 -- '05/AUg/2015'
101 -- '06/AUg/2015'



VB
i need to get date from '01/Aug/2015' to '03/Aug/2015' . if fromdate is '01/AUg/2015' and Todate is '05/AUg/2015' then i need to get date like

01/AUg/2015
02/AUg/2015
03/AUg/2015
04/AUg/2015
05/AUg/2015



how can i do it..
Posted
Updated 6-Aug-15 20:38pm
v2
Comments
Mehdi Gholam 7-Aug-15 2:15am    
Please explain what you mean (02/aug is not in you data)?
TCS54321 7-Aug-15 2:19am    
i need to get date from '01/Aug/2015' to '03/Aug/2015' . if fromdate is '01/AUg/2015' and Todate is '05/AUg/2015' then i need to get date like

01/AUg/2015
02/AUg/2015
03/AUg/2015
04/AUg/2015
05/AUg/2015

SQL
--you may use Common Tabl expression


DECLARE @STARTDATE VARCHAR(50)
SET @STARTDATE='2015-08-01'

DECLARE @ENDDATE VARCHAR(50)
SET @ENDDATE='2015-08-06'



;WITH DATERANGE AS
(
   SELECT DT =DATEADD(DD,0, @STARTDATE)
   WHERE DATEADD(DD, 1, @STARTDATE) <= @ENDDATE
   UNION ALL
   SELECT DATEADD(DD, 1, DT)
   FROM DATERANGE
   WHERE DATEADD(DD, 1, DT) <= @ENDDATE
)
SELECT  * FROM DATERANGE
 
Share this answer
 
Change your DB.

Never store date values as strings: it makes it easy for you to set up and insert, but it always creates problems like this whenever you try to use the data. Store it in a DATE or DATETIME column instead, and this becomes trivial:
SQL
SELECT * FROM MyTable ORDER BY Dated
With VARCHAR or NVARCHAR storage, that is horrible, because the default comparison is character-by-character and to order by "proper" date means converting the string to a DATE first (and dealing with the inevitable user errors) or splitting the string and ordering by a number of fragments.

Seriously, it may seem like a lot of work to fix the DB design errors, but it's a lot less work in the long term.
 
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