Hi i am relatively new to SQL so this is probably a basic question. i am trying to pass a variable and use that variables content to determine what it runs. The variable is a char(2) and it can have the following possible values:
Accepted
= AP
All
= AL
Declined
= DE
Line Manager Accepted
= LA
Admin Accepted
= AA
here is what it should do. I should input a start and an end date and then the two digit code to decide which holiday bookings i select. in the example below i am looking for a date range between the 2 holidays with any status (accepted or declined):
EXEC spSearchHoliday '2013-04-01','2015-05-24','PE'
This returns the correct result! But if i try to run a date search between two dates with any other status it continues to return every date. in the example below i want only to return accepted holidays:
EXEC spSearchHoliday '2013-04-01','2015-05-24','AP'
But instead it still returns every status holiday between the range.
Below is the stored procedure itself the long holidays.startdate and holidays.EndDate sections work fine its jsut the status related parts that are not working correctly.
AS
BEGIN
IF #Status = 'AL'
BEGIN
SELECT StartDate,
EndDate,
Duration,
[Status]
FROM Holidays
WHERE Holidays.Startdate <= #Startdate AND Holidays.Enddate >= #EndDate
OR Holidays.Startdate >= #Startdate AND Holidays.Enddate <= #EndDate
OR Holidays.Startdate <= #Enddate AND Holidays.Enddate >= #EndDate
OR Holidays.Startdate <= #Startdate AND Holidays.Enddate >= #Startdate
END
IF #Status <> 'AL'
BEGIN
SELECT StartDate,
EndDate,
Duration,
[Status]
FROM Holidays
WHERE Holidays.Status = #Status
AND Holidays.Startdate <= #Startdate AND Holidays.Enddate >= #EndDate
OR Holidays.Startdate >= #Startdate AND Holidays.Enddate <= #EndDate
OR Holidays.Startdate <= #Enddate AND Holidays.Enddate >= #EndDate
OR Holidays.Startdate <= #Startdate AND Holidays.Enddate >= #Startdate
END
END
Perhaps the problem is incorrect priority in the second query. Please try using the following WHERE-clause in your second query:
WHERE Holidays.Status = #Status
AND (Holidays.Startdate <= #Startdate AND Holidays.Enddate >= #EndDate
OR Holidays.Startdate >= #Startdate AND Holidays.Enddate <= #EndDate
OR Holidays.Startdate <= #Enddate AND Holidays.Enddate >= #EndDate
OR Holidays.Startdate <= #Startdate AND Holidays.Enddate >= #Startdate)
Related
I need to calculate how many days one set period contains in another set period. I have table with
create table #test
(
,project nvarchar(10),
startProjectDate date,
endProjectDate date)
insert into #test values
('EE43213','2021-12-31','2022-01-06') ,
('EE0211213','2022-01-09','2022-03-14'),
('EE53134','2022-02-18','2022-02-22')
I have parameters with dates (user input in the future)
DECLARE #startDate DATE = N'2021-12-16'
DECLARE #endDate DATE = N'2022-03-02'
For every project I need to calculate, how many days of their running time will be set on user chosen period and then * this count on some koeff.
I have case when in mind, if the whole project was in parameter-set period, I just find datediff between two project dates and * it.
case when (startProjectDate BETWEEN #startDate and #endDate)
and (endProjectDate BETWEEN #startDate and #endDate)
then DATEDIFF(day, startProjectDate , endProjectDate) + 1 * coeff else ...
But how to find an amount of days if they only partially set on this period?
Seems you just need a DATEDIFF and some CASE expressions here:
DECLARE #StartDate DATE = N'20211216',
#EndDate DATE = N'20220302';
SELECT project,
DATEDIFF(DAY, CASE WHEN startProjectDate < #StartDate THEN #StartDate ELSE startProjectDate END, CASE WHEN endProjectDate > #EndDate THEN #EndDate ELSE endProjectDate END) AS DaysInRange,
DATEDIFF(DAY,startProjectDate, endProjectDate) AS DaysInProject
FROM #test
WHERE startProjectDate <= #EndDate
AND endProjectDate >= #StartDate;
Currently working on a t-sql query which is supposed to get a list of all records that fall between the current fiscal year (we use 4-4-5 calendar).
The record's start and end date are 06/02/2018, 31/07/2020.
The date I am filtering for is:
DECLARE #StartDate DATE = '12-29-2018';
DECLARE #EndDate DATE = '12-31-2019';
The condition I have in my where clause is:
AND
(
(o.Revenue_Start_Date__c >= #StartDate AND o.Revenue_End_Date__c <= #EndDate) OR (o.Revenue_End_Date__c >= #StartDate AND o.Revenue_End_Date__c <= #EndDate)
)
I have tried variations of BETWEEN as well. Any idea what I may be doing wrong and how do I get a list of all records to be included if it falls within the coresponding dates.
ANSWER:
AND ( -- Starts Within range
( o.Revenue_Start_Date__c
BETWEEN #StartDate
AND #EndDate
)
OR -- Ends within range
(
o.Revenue_End_Date__c
BETWEEN #StartDate
AND #EndDate
)
OR -- SPANS Range
(
o.Revenue_Start_Date__c < #StartDate
AND
o.Revenue_End_Date__c > #EndDate
)
)
This seems to be working for me just now.
For fall through cases, this should work.
o.Revenue_Start_Date__c <= #EndDate AND o.Revenue_End_Date__c >= #StartDate
Is this what you want?
(o.Revenue_Start_Date__c < #EndDate AND
o.Revenue_End_Date__c >= #StartDate
)
This is the logic for overlapping intervals. Note that the inequalities might be exact or inexact depending on your actual definitions of overlapping.
I have the following two dates :
startDate = '2017-04-04' and endDate = '2017-04-12'
I would like to find the total count of days between these two dates excluding 'Fridays' using SQL Server.
Any Help from the SQL Server Experts will be highly appreciated!! Thanks in Advance!!
You can use DATENAME to check for 'Friday' do this, something like this :
DECLARE #startDate DATETIME
DECLARE #endDate DATETIME
SET #startDate = '2017-04-04'
SET #endDate = '2017-04-12'
Declare #count int
set #count=0
while #startDate < #endDate
Begin
IF DATENAME(dw, #startDate) <> 'Friday'
SET #count = #count + 1
SET #startDate = DateAdd(d, 1, #startDate)
END
select #count
This will result in :
7
just add below clause to your query
select count(*) from table
where startDate >= '2017-01-12' and endDate <= '2017-04-27'
and datename(WEEKDAY,startdate))<>'Friday'
select
id,
starttime,
endtime,
name
from
table1
where
id != 0
and starttime >= #startdate and endtime <= #enddate
In the above query, how do I perform if the #startdate and #enddate parameter is null by without using if else?
starttime >= #startdate and endtime <= #enddate this condition should check if #startdate and #enddate is not null. else it won't to check.
Any suggestion for this?
you simply check that the variable is null thus returning true as as appropriate
doing this individually for each parameter
where (startdate >= #startdate or #startdate is null)
and (enddate <= #enddate or #enddate is null)
you could also 'ignore' the criteria if either are null
where ((startdate >= #startdate and enddate <= #enddate)
or #startdate is null
or #enddate is null)
All you need is ISNULL or COALESCE function in the where condition.
select id,
starttime,
endtime,
name
from table1
where
id != 0 and
starttime >= ISNULL(#startdate,starttime) and endtime <= ISNULL(#enddate,endtime)
Add extra condition:
and #startdate is not null and #enddate is not null
try this:
for this
should check if #startdate and #enddate is not null. else it won't to check use first answer
if #startdate is null then it will return true and gives all result
select id,
starttime,
endtime,
name
from table1
where
id != 0 and
(starttime >= #startdate or #startdate is null )
and
(endtime <= #enddate or #enddate is null)
or
select id,
starttime,
endtime,
name
from table1
where
id != 0 and
(starttime >= #startdate and #startdate is NOT null )
and
(endtime <= #enddate and #enddate is NOT null)
Try to use OR to check this:
where
(id != 0)
and
(
(#startdate IS NULL) OR (#enddate IS NULL)
OR
(starttime >= #startdate and endtime <= #enddate)
)
This is my stored procedure which I am connecting with crystal report and I want to return start date and end date which is I am getting from parameters like if date is null then nothing shows on reports but if date has some value then that value prints.
CREATE PROCEDURE [dbo].PatientClaimInfo
#StartDate Date = NULL,
#EndDate Date = NULL
AS
BEGIN
select p.VLNAME + ' ' + p.VFNAME AS Patients_Name, p.IPATID AS Patient_ID, p.DDOB AS dob,
d.NCOPAY, d.NVTOTPLAN, d.NVWOPLAN, d.NVWOPAT, d.NVADJPLAN, d.NVADJPAT, d.NVPAIDPLAN,
d.NVPAIDPAT, d.NVBALPLAN, d.NVBALPAT, d.NAPPTBAL, d.VPAYSTAT AS Status
From pmvixtr d
INNER JOIN pmptxft p ON p.IPATID = d.IPATID
Where #StartDate <= d.DSDATE AND #EndDate >= d.DSDATE
END
In your select statement you can include #StartDate, #EndDate
e.g.
select #StartDate, #EndDate, .... <rest of your select statement>...
I would also suggest in your where clause use Where d.DSDATE BETWEEN(#StartDate, #EndDate)
If you don't want to select anything if #StartDate and #EndDate is NULL, having that in WHERE Clause can be very expensive... I suggest have an if condition
IF #StartDate IS NOT NULL AND #EndDate IS NOT NULL
select.....
END IF
Try
CREATE PROCEDURE [dbo].PatientClaimInfo
#StartDate Date = NULL,
#EndDate Date = NULL
AS
BEGIN
IF (#StartDate Date IS NOT NULL AND #EndDate Date IS NOT NULL)
THEN
select p.VLNAME + ' ' + p.VFNAME AS Patients_Name, p.IPATID AS Patient_ID, p.DDOB AS dob,
d.NCOPAY, d.NVTOTPLAN, d.NVWOPLAN, d.NVWOPAT, d.NVADJPLAN, d.NVADJPAT, d.NVPAIDPLAN,
d.NVPAIDPAT, d.NVBALPLAN, d.NVBALPAT, d.NAPPTBAL, d.VPAYSTAT AS Status
From pmvixtr d
INNER JOIN pmptxft p ON p.IPATID = d.IPATID
Where #StartDate <= d.DSDATE AND #EndDate >= d.DSDATE
END
END