Selecting a period in field based on range - ms-access

Thank you all in advance for any help. I'm Still very new to access and have no idea where to start to find a solution.
What I am trying to do is to auto populate a field in my table called "Period". I would like to have it use the "Activity_Date" to look into a different table that has date ranges that reference to the correct period. Based on which "Period" the "Activity_Date" falls under will return the correct "Period". I've tried using calculated data type and queries and I feel no closer to an answer than when I started.
Thanks again for your time.

I would question why you NEED to populate the field period in your table.
In short, I wouldn't bother.
The period it is in can be derrived from the activity date field that is in the same record.
So you can write select statements that calc the period for the record in your MyTable as required.
SELECT TableWithPeriods.period, MyTable.activity_date
FROM MyTable
LEFT JOIN TableWithPeriods
ON MyTable.activity_date
BETWEEN TableWithPeriods.StartDate
AND TableWithPeriods.EndDate
If you need to access the period a lot then there is an argument for keeping a period value in the MyTable in step with the TableWithPeriods.
Keeping in step could be akward though as what if someone changes one of the period 's dates?
Keeping in step might mean writing a bit of SQL to update ALL MyTable rows that wither do not have the period set or when the period is now different.
A VBA update statement will look a bit like the SELECT above.
Or
you could use database the onchange macros that respond to data being added or updated in the MyTable (and the TableWithPeriods, if users can change dates).
Anyway, there's my opinion. I would NOT copy the value over.
PS I'm not 100% sure about the SQl I gave above, this might work though
SELECT TableWithPeriods.period, MyTable.activity_date
FROM MyTable
LEFT JOIN TableWithPeriods
ON ( MyTable.activity_date >= TableWithPeriods.StartDate
AND MyTable.activity_date <= TableWithPeriods.EndDate )

Related

MS Access; Remove Time stamp from Date\time field in all records

Is there another way besides running an Update QUery to take 1 particular Date\Time field in my main table to "mm/dd/yyyy hh:mm:ssPM" to just "mm/dd/yyyy"?
ive seen other answer's that involve querys or VBA but I didnt know if there was a secret access command for this type of change. If not I will run a query but im always eager to know short cuts!
You can use a select query to rip the time part:
Select *, Fix([YourDateTimeField]) As DateOnly From YourTable

Is there a way to know the number of records added into the SQL database after a particular date and time

The table doesn't have any date time column. I want to if there is any inbuilt keyword which can does that.
I want to know all commits done after a particular date.
If flashback is enabled on the database you can get records on the table in an around a particular date range in Oracle.(It purely depends on if its enabled and for how long the flashback needs to be kept)
You can query to see the data in the table as of 3 days back as follows
select *
from table as of timestamp sysdate-3

CURDATE functionality from db2 query

I'm trying to apply 'curdate()' functionality to a select statement from DB2. I'm used to MySQL but I'm still trying to get the hang of a lot of the DB2 functionality and how to essentially marry the two.
My query is complete except for one line. I'm trying to select based on a ship date, which is the column EXTD1H and I need to check it against today or curdate(). The problem is that column in DB2 is an integer format, not a date format, and I don't have the option of changing it. In prior inserts to mysql, I've been able to put it into Y-m-d format and I know I can trim the year using LEFT(EXTD1H, 4) but I have no idea how to modify my select so that I can say WHERE EXTD1H is today so that I'm only selecting records for this date.
Here's the query:
select
invnoz as ORDER,
fstatz as STATUS
from gportafl
/*where EXTD1H is curdate, hypothetically*/
AND FSTATZ <> 'S'
limit 20;
As you can see, I have a commented line where my issue is. I'm sure it's simple I just can't seem to find in the documentation exactly what I'm looking for, which is to be able to use that INT column to verify that selected records are from today.
UPDATE:
All values from the column are in YYYYMMDD format i.e.
20180202
but it should be 2018-02-02
It's best not to do operations on the columns, so the indexes are used.
You can typecast the current date to fit your data as follows:
WHERE extd1h = INTEGER(VARCHAR_FORMAT(CURRENT DATE,'YYYYMMDD'))

How to Autofire up a Sql Statement when a condition is fulfilled?

I have a table Training having column T_StartDate and T_EndDate and so on. Now i want if saved T_EndDate matches with Today's Date , I want record of that column should be deleted by itself. for this i have write some code in procedure but i think this would not work as expectation as procedure can not fire up automatically.
So for this issue i read from internet that i need a trigger to do such functioning. But do not have much of idea about trigger.
So please tell me how could i do this?
Also i am attaching a picture for my table so that you guys understand the structure.
Thanks in advance
Just add a computed column:
alter table Training add IsValid as (case when t_enddate < cast(getdate() as date) then 1 else 0 end)
You can then access the table using this column or using a view:
create view v_Training as
select t.*
from Training
where IsValid = 1;
You can then delete the rows at your leisure -- once per month, once per week.
Otherwise, you need to set up a job to run at exactly midnight. If not, there will be a period of time when the data will not be valid. And, if you miss the update for some reason, you could have problems.

compare timestamps in mysql

I save timestamps in my database in this format 2012-04-16 08:58:55. I read a timestamp of my database and then i want to use this timestamp in another query and ask from the database to return records where the timestamp is greater equal than this timestamp. I am using the ">=" but it is not working.
I am trying this one:
$query="SELECT DISTINCT timestamp,text FROM array WHERE id='$theID' AND timestamp>='$thisTimestamp'";
What exactly does "not working" mean? It's not clear whether you want to compare datetime stamps or simply the time porition.
For the former, check this thread Mysql Compare two datetime fields, for the latter, simply use the TIME() function in your query e.g. SELECT * FROM table WHERE TIME(datetime) >= '08:58:55';
Clarify your question if you are in search of something else.
EDIT: Have you not read my first link? That is exactly what you need given the problem you have provided so far. What results are you looking for? Give an example and then give an example of how your query is performing incorrectly. Without this information, no one will be able to give you complete help!