CURDATE functionality from db2 query - mysql

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

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

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 )

MySQL Order By Date

I have an a MySQL database. My database contains documents with a datetime column called "created". I want to group by day in order to have the document count per day. However, some days have zero documents and as a result they are not part of the output. For example I need '2001-01-01' to have a zero count if documents do not exist.
I am thinking of creating an extra table with the date range I am interested on and the to Do an outer join with my table. Then I can group by date to have my results.
Is there any better way of doing such a thing?
My SQL code:
Select date(created_at),c.text from Dates d left outer join classifier c on d.n=DATE(c.created_at)
where c.classifier="2014streamlrall"
and date(c.created_at)>='2014-03-01' and date(c.created_at)<='2014-05-01'
order by d.n;
The left join still does not work.
There is no better way for that in MySQL.
It lacks both a method to generate an arbitrary length resultset (similar to PostgreSQL's generate_series) and recursive SQL required to emulate such a method (which is used in SQL Server and Oracle).
Even on SQL Server, populating and keeping a table with 100 years worth of dates (which takes but a little more than 73K records) gives much better performance on reports similar to yours than using a generated resultset.
Is your real issue with having a create a row with '2001-01-01: 0'? If so, just wrap the MySQL with PHP and control your output with PHP. Then you just echo (or create an XML entry, whatever formatting you need) with the date and the result, even if it's 0. Even if you're currently know nothing about PHP, there are plenty of tutorials on how to run MySQL queries in PHP and output results. Good luck.

Using MySQL to return row between to strings with date, but without year

I am currently working on a project in which I want to store commemorative days (like January 8th's World Leprosy Day) in a database. At this moment they're stored in a table which contains:
- an ID
- the date as varchar (stored European style, e.d. "8-01" for January 8th)
- length of the commemorative day (as some span multiple days)
- and the name
The reason I am storing the date as varchar is because the year is irrelevant, and I'm a bit reluctant to just store a year (e.g. 2013) in the database and truncate it.
But here's the problem: I can't seem to find a way to construct a query that will get the rows between dates. I think it's because the way the dates are stored in the database.
I already tried (given day = "8-01")
SELECT * FROM comdays WHERE date(day) BETWEEN date("1-01") AND date("20-01")
But to no avail.
Is there a way to get this thing going with strings? Or do I have to change the date column into a MySQL DATE format?
Thanks in advance!
If you really want to keep non standard date field in MYSQL you will need to use the following format 0108-> mmdd this format allows calculations.
It might also be worth reading the following answers to similar question Save day and month in database

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!