real9555
asked on
Dates fall between two dates
I have a select query with appt_date that should fall between two dates,
start_date
End_date
Appt_date
If the appt_date is between estart_date and End_date, then yes, else, no. example:
appt_date is 01/01/2012
start_date 01/01/2012
end_date 02/03/2012
then this will be a yes.
the statement I have:
select Staff_name,Staff_ID, "I would like to place the if Statement here, appt_date from....
start_date
End_date
Appt_date
If the appt_date is between estart_date and End_date, then yes, else, no. example:
appt_date is 01/01/2012
start_date 01/01/2012
end_date 02/03/2012
then this will be a yes.
the statement I have:
select Staff_name,Staff_ID, "I would like to place the if Statement here, appt_date from....
CASE WHEN appt_date BETWEEN start_date AND end_date THEN 'yes' ELSE 'no' END
select
Staff_name,
Staff_ID,
CASE
WHEN appt_date BETWEEN start_date AND end_date THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
Staff_name,
Staff_ID,
CASE
WHEN appt_date BETWEEN start_date AND end_date THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
ASKER
dkrollCTN,
that worked best but what if the start_date and end date is balnk, can I get the code to give me the word "Null".
that worked best but what if the start_date and end date is balnk, can I get the code to give me the word "Null".
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Appt_date from date end_date
2013-02-01 10:00:00 2012-07-26 00:00:00 2013-02-01 00:00:00 No
it gave me no when it should have been yes. :(
2013-02-01 10:00:00 2012-07-26 00:00:00 2013-02-01 00:00:00 No
it gave me no when it should have been yes. :(
You have the end date as 00:00:00, but the appt_date is 10:00:00. That is after the end_date.
If you just want to use the date part and not worry about the time, do this:
select
Staff_name,
Staff_ID,
CASE
WHEN (start_date is NULL or start_date = '') and (end_date is NULL or end_date = '') THEN 'Null'
WHEN convert(varchar, appt_date, 101) BETWEEN convert(varchar, start_date, 101) AND convert(varchar, end_date, 101) THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
select
Staff_name,
Staff_ID,
CASE
WHEN (start_date is NULL or start_date = '') and (end_date is NULL or end_date = '') THEN 'Null'
WHEN convert(varchar, appt_date, 101) BETWEEN convert(varchar, start_date, 101) AND convert(varchar, end_date, 101) THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
ASKER
dkrollCTN,
it is still giving me No for yes cases.....
should I add another column for appt date and make it date only field?
it is still giving me No for yes cases.....
should I add another column for appt date and make it date only field?
can you give me the start, end and appt date you're using?
if you use this, you won't need another column
select
Staff_name,
Staff_ID,
CASE
WHEN (start_date is NULL or start_date = '') and (end_date is NULL or end_date = '') THEN 'Null'
WHEN convert(varchar, appt_date, 101) BETWEEN convert(varchar, start_date, 101) AND convert(varchar, end_date, 101) THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
select
Staff_name,
Staff_ID,
CASE
WHEN (start_date is NULL or start_date = '') and (end_date is NULL or end_date = '') THEN 'Null'
WHEN convert(varchar, appt_date, 101) BETWEEN convert(varchar, start_date, 101) AND convert(varchar, end_date, 101) THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
ASKER
appt_date start_date end_date
2013-03-25 09:00:00 2012-09-11 00:00:00 2013-03-25 00:00:00
appt_date smalldatetime Checked
start_date smalldatetime Checked
end_date smalldatetime Checked
2013-03-25 09:00:00 2012-09-11 00:00:00 2013-03-25 00:00:00
appt_date smalldatetime Checked
start_date smalldatetime Checked
end_date smalldatetime Checked
Sorry, I left one thing out, try this:
select
Staff_name,
Staff_ID,
CASE
WHEN (start_date is NULL or start_date = '') and (end_date is NULL or end_date = '') THEN 'Null'
WHEN convert(varchar(10), appt_date, 101) BETWEEN convert(varchar(10), start_date, 101) AND convert(varchar(10), end_date, 101) THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
select
Staff_name,
Staff_ID,
CASE
WHEN (start_date is NULL or start_date = '') and (end_date is NULL or end_date = '') THEN 'Null'
WHEN convert(varchar(10), appt_date, 101) BETWEEN convert(varchar(10), start_date, 101) AND convert(varchar(10), end_date, 101) THEN 'Yes'
ELSE 'No'
END as betweendates,
appt_date
FROM tablename
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT .... WHERE appt_date BETWEEN start_date AND end_date
or
SELECT .... WHERE appt_date < end_date AND appt_date>=start_date