I have a date range:
SET @declared_start_datetime = '11/02/2015 07:00:00'
SET @declared_end_datetime = '11/09/2015 18:00:00'
This was working until I realized it was pulling all records from 7:00 am on 2nd of November until 6:00 pm on November 9th.
What I need it to do is pull all records from those dates but exclude Saturday/Sunday and only between the hours of 7:00 and 18:00 for Monday-Friday.
I am using SQL Server going through a linked MYSQL server so I am using OPENQUERY. Here is an example of what I have so far.
DEClare @session varchar(max) = 'SELECT * FROM database.session WHERE CREATIONTIMESTAMP between ''''' + convert(nvarchar(50),@converted_start_datetime, 120) + ''''' and ''''' + convert(nvarchar(50),@converted_end_datetime, 120) + ''''''
SET @session = 'SELECT * INTO ##session FROM OPENQUERY(mxie,''' + @session + ''')'
exec(@session)
How do I exclude Saturday's and Sundays and adjust it to only pull the time where the business is open?
Edit:
DEClare @session varchar(max) = 'SELECT * FROM database.session WHERE ((CREATIONTIMESTAMP between ''''' + convert(nvarchar(50),@converted_start_datetime, 120) + ''''' and ''''' + convert(nvarchar(50),@converted_end_datetime, 120) + ''''')
AND (TIME(CREATIONTIMESTAMP) between ''''' + convert(nvarchar(50),@business_opens_time, 108) + ''''' and ''''' + convert(nvarchar(50),@business_closes_time, 108) + '''''))'
SET @session = 'SELECT * INTO ##session FROM OPENQUERY(mxie,''' + @session + ''')'
exec(@session)
select * from ##session
drop table ##session
This doesn't exclude Saturdays or Sundays yet but it is currently returning 0 results. if I change the AND to OR it will return results but not what I am looking for.
I am looking for Monday - Friday from 7:00 am until 6:00 pm for each of the days.