Finding rows that Dates fall between specific dates in sql server table

I have a holidays table which records holidays taken by individuals.
The table has three columns
Name, start_date,end_date.


I need to  return all rows where  a holiday period would include any of the
 Following dates.
2018-01-01 00:00:00
2018-03-19 00:00:00
2018-03-30 00:00:00
2018-04-02 00:00:00
2018-05-07 00:00:00
2018-06-04 00:00:00
2018-08-06 00:00:00
2018-10-29 00:00:00
2018-12-25 00:00:00
2018-12-26 00:00:00

I am thinking something along the lines of
 select * from  Holidays_table where 
 (2018-01-01 00:00:00
)  between [ Start_date]  and [end_date}
UNION

Open in new window

Then repeat the select statement for each date needed using UNION

this is my starting point but any guidance or ideas appreciated....
Thanks

ps
 i have the dates stored in another table and i can get them with the following query
   select date from dbo.[holidays]
  where DATENAME(YEAR,holdate)='2018'

Open in new window

blossomparkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OMC2000Commented:
try this:


select name, Start_date,  end_date, dt from hollidays
inner join 
(select  cast ('2018-01-01 00:00:00' as datetime) as dt
union all
select  cast ('2018-03-19 00:00:00' as datetime) as dt
union all
select  cast ('2018-03-30 00:00:00' as datetime) as dt
union all
select  cast ('2018-04-02 00:00:00' as datetime) as dt
union all
select  cast ('2018-05-07 00:00:00' as datetime) as dt
union all
select  cast ('2018-06-04 00:00:00' as datetime) as dt
union all
select  cast ('2018-08-06 00:00:00' as datetime) as dt
union all
select  cast ('2018-10-29 00:00:00' as datetime) as dt
union all
select  cast ('2018-12-25 00:00:00' as datetime) as dt
union all
select  cast ('2018-12-26 00:00:00' as datetime) as dt
) d on dt between Start_date and end_date

Open in new window

0
blossomparkAuthor Commented:
Hi OMC2000,
thanks for your reply,
getting the following when i run that

Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

Open in new window

0
OMC2000Commented:
It means some of your dates are not dates in fact. I run subquery marked as "d" on my SQL server and it returned correct dates. Do you use other values?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

OMC2000Commented:
Also, you could try direct format specification for dateime conversion:

select name, Start_date,  end_date, dt from hollidays
inner join 
(select  convert (datetime, '2018-01-01 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-03-19 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-03-30 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-04-02 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-05-07 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-06-04 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-08-06 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-10-29 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-12-25 00:00:00', 20) as dt
union all
select  convert (datetime, '2018-12-26 00:00:00', 20) as dt
) d on dt between Start_date and end_date

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blossomparkAuthor Commented:
perfect....thank you
0
blossomparkAuthor Commented:
thanks :-)
0
PortletPaulfreelancerCommented:
FYI
In SQL Server dates can always safely be represented as YYYYMMDD

select  cast ('20180101' as datetime) as dt
union all
select  cast ('20180319' as datetime) as dt
union all
select  cast ('20180330' as datetime) as dt
union all
select  cast ('20180402' as datetime) as dt
union all
select  cast ('20180507' as datetime) as dt
union all
select  cast ('20180604' as datetime) as dt
union all
select  cast ('20180806' as datetime) as dt
union all
select  cast ('20181029' as datetime) as dt
union all
select  cast ('20181225' as datetime) as dt
union all
select  cast ('20181226' as datetime) as dt

Open in new window


ONLY this date literal style yyyymmdd is completely free of interference from server settings.
Even a literal style of yyyy-mm-dd can (under some language settings) be misunderstood.

yyyymmdd is style number 112 by the way
see https://www.experts-exchange.com/articles/12315/SQL-Server-Date-Styles-formats-using-CONVERT.html
0
blossomparkAuthor Commented:
Thanks for that PortletPaul, have updated code and works....Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.