mySQL generating rows based on a recurring date - mysql

I have a mySQL table which contains events, details on the event, the event date and a recurring value - which is how often the event recurs in days.
e.g. Date: 2012-12-03, Recurrance: 7 - it will recurr every 7 days.
I am now looking to search on this by date and would like the row to be returned if the search was for 2012-12-03, 2012-12-10, 2012-12-17, 2012-12-24...+7 days.
Currently, the search is for a specific day:
SELECT
id,
title,
venue,
date_format(datetime, '%I:%i%p') AS time
from
bt_db_eventcal
WHERE
DATE(datetime) = '" . $date . "'
Can anyone help expand this to return rows based on the recurrance?
Thanks

its a simple math really. you subtract the saved date from the search date and mod the value by the recurrence. if the value is 0 then it is good.

Related

SSRS - common expressions DATE&TIME?

I am creating an SSRS report based of a table that have [timestamp] and [status] field .
i am trying to count hourly TIMESTAMP based of sample below .
ON MY TABLE I WONT TO COUNT HOURLY TIMESTAMP EXCLUDING DATE
ONE FIELD [6A-7A] 2ND -[8A-9A] ?
[enter image description here]
which Common function expression should i manipulate in SSRS to get results .
timestamp STATUS
1/19/2010 6:04:41 AM PAY
04/19/2010 7:04:41 AM DENY
1/19/2010 8:04:41 AM PEND
02/19/2010 1:04:41 PM ADJU
1/19/2010 4:04:41 PM PAY
10/22/2010 7:04:41 PM PAY
You could Group By the HOUR to get the hour part.
=HOUR(Fields!timestamp.Value)
For the displayed value you could use the FORMAT function to get the hour and AM/PM period (figuring out that term was a can of worms).
=FORMAT(Fields!timestamp.Value, "ht") & " - " & FORMAT(DateAdd("h", 1, NOW), "ht")
The h is for hour and the t is for the first part of the AM or PM period. tt would be used to show AM or PM.
If you only want to show hours 8 and 9, you would add a filter expression to the Column Grouping.
=HOUR(Fields!timestamp.Value)
Datatype: Integer
Operator: BETWEEN
Values: 8 9
Thank you everybody for your suggestion was helpful .
in case someone needed an answer this worked for me .
below expression will get time hourly report [7a-8a],[8a-9a]...ETC to be specific
=SUM(IIF(HOUR(Fields!timestamp.Value)= 7,1,0)) +SUM(IIF(HOUR(Fields!timestamp.Value)= 8,1,0))

How to search data for a date value after than the specified one?

I have a basic SQL knowledge and I am trying to retrieve data from my database based on the date a user chooses. So, if the user selects 2018-03-01 I want to display the data for the date value 2018-03-02.
I can search data for a specified date in the following way:
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01';
Now I want to search the data for the date next to the specified so for '2018-03-02' in this case. I cant directly search for 2nd March because I don't know what date the user will choose
So is there any way to query data for the date next to the one specified?
I have already tried looking up everywhere but couldn't find anything that makes sense to me.
Thx
I think this works well. By the documentation for date/time functions, you should be able to do something like,
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01' + INTERVAL 1 DAY
What sort of element are you using for the user to be able to get their date and what is your codebase? We have mysql for the DB but we need to know if your codebase is c#, PHP etc.
Most languages generally have a function to allow you to add days to a datetime, if for instance you used C# and had a datetime selector posting its value you can do:
DateTime dateField = postedDateTime.AddDays(1);
from there you can easily escape the value and push it through your db call.
or PHP:
$dateField = date('Y-m-d', strtotime($postedDateTime. ' + 1 days'));
If you would like a date range (date selected and the next day) then you would adjust your php code to have:
$dateFrom = $postedDateTime;
$dateTo = date('Y-m-d', strtotime($postedDateTime. ' + 2 days'));
$dateTo = date('Y-m-d', strtotime($dateTo. ' - 1 seconds'));
//DB Query
$query = "select * from FLIGHT where DEPARTURE_DATE >= :dateFrom AND DEPARTURE_DATE <= :dateTo";
The above assuming you're using pdo, but you can see how to adjust for your situation. I am also assuming the datetime is based on midnight for each day so you may need to adjust your dateTo to take from 1 second before midnight the day after just so you can get the entire days data
You can make use of interval as day as mentioned in the answer. Along with day you can also have day_minute, day_hour,day_second but be sure when you are adding decimals on these.

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

Filtering the dates range based on the days

I have a table with columns as
Id (1)
Date holding values as (2013-12-12 00:00:00)
Start time (00:00:00)
end time (01:00:00)
value
The user will give the date range and will specify day like sunday ,monday etc .
How can I use a sql query to filter the dates and find the proper days between that for the specified days as well.
One way would be to use BETWEEN ... AND ... and DAYOFWEEK():
SELECT *
FROM my_table
WHERE Date BETWEEN ? AND ?
AND DAYOFWEEK(Date) = ? -- 1=Sunday ... 7=Saturday

Adding an automatic date range to a query in VBA access

This is my first question, please be kind. I am writing a macro in access and I want a series of reports to run one after another. the problem is all report have a date range that needs to be entered. Some are for the previous week some are for the previous month.
Is there a way in VBA or the macro creator to automatically calculate a date range for the previous month or week and populate the field to fully automate the process without manually entering the date range each time.
I am a new to VBA. Any help would be great, just point me in the right direction. Have a good day.
This query is created using the query design window in MS Access and then cut from SQL view. It will show records for last week, where ww is week number, in table1
SELECT Table1.AKey, Table1.atext, Table1.ADate,
Format([ADate],"ww") AS Week, Month([ADate]) AS [Month],
Year([ADate]) AS [Year]
FROM Table1
WHERE (((Format([ADate],"ww"))=Format(Date(),"ww")-1)
AND ((Year([ADate]))=Year(Date())));
You will notice that one column is called Month. You can use this to set a previous month in a similar way to setting the previous week. For example, both last week and last month:
SELECT Table1.AKey, Table1.atext, Table1.ADate,
Format([ADate],"ww") AS Week, Month([ADate]) AS [Month],
Year([ADate]) AS [Year]
FROM Table1
WHERE (((Format([ADate],"ww"))=Format(Date(),"ww")-1)
AND ((Year([ADate]))=Year(Date())))
OR (((Month([ADate]))=Month(Date())-1)
AND ((Year([ADate]))=Year(Date())));
The SQL could be written much more neatly, but you may as well start with the query design window.
i guess the date has a own field in the database you open
then you can do something like this
strSQL = "SELECT * FROM reports WHERE Date >= " & now() -7
rs.open(strSQL)
' for the last week
strSQL = "Select * FROM reports WHERE Date >= " & now() - 30
rs.open(strSQL)
' for the last month
but you will need to format now() to the same format as it is in your Table
and that is just kinda the rawest code. i had to handle something similar and this worked out quite well