Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,
I have a doubt in SQL regarding querying this table
SQL
CREATE TABLE [dbo].[tbl_Trans_LeaveRequestApproval]
(
    [FromDate] [datetime] NOT NULL,
[ToDate] [datetime] NOT NULL,
    -- other columns to follow here
    -- ...
)

Suppose I have inserted a record with FromDate as '21-05-2012' and ToDate as '28-05-2012' and now I want the dates inbetween the two dates.
How do i make a query for this?

Thanks in advance!
Posted
Updated 21-May-12 1:53am
v3
Comments
Manfred Rudolf Bihy 21-May-12 7:56am    
You are being unclear: Do you wand the number of days between these two dates. You said you wanted "the dates between the two dates", what exactly do you mean by that? You probably mean records with dates between the two mentioned.
echosound 21-May-12 7:57am    
dates from the fromdate and todate

Try
SELECT * FROM myTable WHERE myDateField BETWEEN '21-05-2012' AND '28-05-2012'
 
Share this answer
 
Comments
Mohamed Mitwalli 21-May-12 8:07am    
5+
Abhinav S 21-May-12 9:14am    
Thanks.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 21-May-12 8:05am    
Right on! 5+
Mohamed Mitwalli 21-May-12 8:07am    
5+
See this link you may get some idea
http://msdn.microsoft.com/en-us/library/ms189794.aspx[^]
 
Share this answer
 
Comments
Manfred Rudolf Bihy 21-May-12 8:05am    
Until OP explains exactly what he wants, that too is an option! 5+
Try this:
SQL
SELECT * FROM tbl_Trans_LeaveRequestApproval WHERE FromDate >= '21-05-2012' AND ToDate <= '28-05-2012'
 
Share this answer
 
Comments
Manfred Rudolf Bihy 21-May-12 8:02am    
You beat me to it! 5+
echosound 21-May-12 8:07am    
no i want the date between those two dates
Prasad_Kulkarni 21-May-12 8:09am    
So therefore I answered it. Is there any problem you're facing with that query?
echosound 21-May-12 8:10am    
ya really want the dates based on the employee id!!!
Prasad_Kulkarni 21-May-12 8:16am    
SELECT * FROM tbl_Trans_LeaveRequestApproval WHERE FromDate >= '21-05-2012' AND ToDate <= '28-05-2012' WHERE id = '1'

like this?
Another twist on the previous solutions (since you didn't exaclty specify what you wanted):

SQL
SELECT *
FROM tbl_Trans_LeaveRequestApproval ra
WHERE
    ra.FromDate >= '2012-05-21' AND
    ra.ToDate   <= '2012-05-28


or maybe you wanted the difference of days:

SQL
SELECT DATEDIFF(day, '2012-05-28', '2012-05,21') AS NumberOfDays


Regards,

Manfred
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 21-May-12 8:06am    
5+
Dear Friend,

Try this, I hope this will fulfill your requirement.
But its taken from Net.

SQL
SET DATEFORMAT MDY
DROP TABLE #myBalances
CREATE TABLE #myBalances (
      [TranDate] DATETIME,
      [TransactionAmount] money,
      [Balance] money)
      
INSERT INTO #myBalances VALUES ('5/1/2012',100,100)
INSERT INTO #myBalances VALUES ('5/3/2012',-60,40)
INSERT INTO #myBalances VALUES ('5/4/2012',10,50)



DECLARE @StartDate DATETIME, @EndDate DATETIME
SET @StartDate = '5/21/2012'
SET @EndDate = '5/28/2012'

;with TallyCalendar as (
                       --5 years prior to today plus 5 years after
                       SELECT  dateadd( dd, -3650 + RW ,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)) As N
                       FROM (
                             SELECT TOP 7300 -- ~10 years of days
                               row_number() OVER (ORDER BY sc1.id) AS RW 
                             FROM Master.dbo.SysColumns sc1
                             CROSS JOIN Master.dbo.SysColumns sc2
                             ) X
                      )

SELECT DATEADD(DD, 0, Tally.n) AS TranDate
FROM TallyCalendar Tally 
LEFT JOIN #myBalances b ON b.TranDate = DATEADD(DD, 0, Tally.n)  
LEFT JOIN #myBalances x ON x.TranDate = (SELECT MAX(TranDate) AS TranDate FROM #myBalances WHERE TranDate <= DATEADD(DD, 0, Tally.n))
WHERE n BETWEEN DATEDIFF(DD, 0, @StartDate) AND DATEDIFF(DD, 0, @EndDate) 

Regards,
AP
 
Share this answer
 
v2

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