compare timestamps in mysql - 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!

Related

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'))

Is it possible to use DateDiff in an IIF statement?

I have searched looking for help but can't find exactly what I need.
I am trying to find the total number of days between two date fields, however the initiating date fields could be from two different fields, eg Start Date, or amended Start Date. The end date will always be the same field.
The majority of the time there will not be an amended start date but I need a way to cater for the scenario that an start date has been amended.
I tried the following
=IIF(Fields!AmendedStartDate.Value is nothing, DateDiff("d",Fields!StartDate.Value,Fields!EndDate.Value, DateDiff("d",Fields!AmendedStartDate.Value, Fields!EndDate.Value)))
I get an error run a run this.
I am fairly new to Report Builder/SSRS so I am unsure if what I am asking for is even possible.
Thanks for taking the time to look.
You can't do Fields!FieldName.Value is nothing, instead you need IsNothing(Fields!FieldName.Value):
=IIf(IsNothing(Fields!AmendedStartDate.Value),DateDiff("d",Fields!StartDate.Value,Fields!EndDate.Value),DateDiff("d",Fields!AmendedStartDate.Value,Fields!EndDate.Value))
Alternatively, you could create a Calculated Field (called say, StartDateToUse) with a similar expression:
=IIf(IsNothing(Fields!AmendedStartDate.Value),Fields!StartDate.Value,Fields!AmendedStartDate.Value)
And then refer to this field in your main expression to get the result you want:
=DateDiff("d",Fields!StartDateToUse.Value,Fields!EndDate.Value)

Selecting a period in field based on range

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 )

Select month value in a timestamp field as part of a MySQL query

I will like to query a MySQL database table that has a timestamp field. The query is a SELECT statement that retrieves, as part of the columns, the month value in the timestamp. At present, I have no idea what to write.
The main reason I'm doing this in MySQL is that I would want to sort the result by that month value.
Perhaps if it's not possible, a PHP solution will be highly welcome.
(It's been a long time I asked any question on SO, so please forgive the formatting. Hope to catch up on it soon.)