Alternative for from_unixtime in GENERATED ALWAYS query - mysql

Unfortunately, MySQL/MariaDB no longer supports using from_unixtime in Stored Generated Always columns, I am trying to generate values in a column automatically from another column that contains timestamp values. I have this
ALTER TABLE messages
ADD COLUMN only_date DATE GENERATED ALWAYS AS from_unixtime(timestamp_column, '%Y-%M-%D') STORED;
Can someone please help with an alternative that works? Thanks.

Related

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

Sorting a Column of String Values Representing Numbers in Access (VBA)

The Scenario: I have a table In Access that has a column of type text but contains numerical values that needs to be sorted in a descending order.
The Issue: I tried doing a query and specifying the order in the design view, but the results are being sorted based on the first digit and not the whole number.
When I tried to change the type of the column from text to numeric, I received a warning some data will be lost and also the sorting yielded wrong results.
The Question: Any suggestions about how to solve this issue?
Note: I am importing the data from an excel sheet where the column is of type standard.
I find no datatype NUMERIC in the Access SQL documentation. If data will be lost, you use a datatype that is too small for the converted values. Probably use FLOAT, a double precision floating point value. You could add a FLOAT column to your table and then UPDATE that field of the table from the text field. This will preserve your old values and lets you check for any non-converted values.
UPDATE myTable SET float_field=text_field;

mySql Date/DateTime default value

I have a Mysql 5.5 and a table with a column as follow:
`VERSION_TS` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
but i'm having difficulties lately i'm getting this exception:
Incorrect datetime value: '1998-03-20' for column 'VERSION_TS' at row
1
when trying to enter values to the table, values of type Date, using mySql MakeDate().
Now, i can't change the function that return Date, but i can change the column to Date, but then i'll lose the default value. i've tried a couple of things, and then checked the web, and from what i understand in Mysql 5.5 there is no way to do it, but i could be wrong, so i came here to ask:
Is there a way that i can change the column to a Date and still have
a default value?
Also, is there a better way to do approach the problem?
I think the documentation is quite clear on this point:
TIMESTAMP and DATETIME columns can be automatically initialized and
updated to the current date and time (that is, the current timestamp).
So, this does not apply to DATE. However, you could create a view that does what you want:
create view v_table as
select t.*, date(version_ts) as version_date
from table t;
if the function which returns date is written in php then after receiving the date value you can do this:
$date = new DateTime('2006-12-12');
echo date_format($date,'Y-m-d H:i:s');// this you can store in mysql table.
if not same kind of approach you can apply in the respective language to do the job.
OR work with view as Gordon Linoff mentioned.

Get the date when the row was inserted MySQL

Is there any way to get a datetime when the row was created? Is it stored anywhere? I just forgot to add a column which tells when the row was inserted, and lots of rows have been inserted and I don't know when they were inserted... Thanks.
Unfortunately there's no way to achieve that. MySQL doesn't store a timestamp of row creation, that's why you always have to create a column to do that, along with ON UPDATE clause which will update the column every time the row is updated
Nope. You need a column to record that, it's not stored in the system otherwise.
There isn't a way to get timestamp when the row was created unless you add a field that is populated with system time or has a default value.

Dynamic column in MySql showing Timedifference

I need a column in a MySql datatable which shows the difference between now and a timestamp. I have the following column within a view but I need a corresponding column in a datatable (InnoDB)
time_format(timediff(`myTable`.`anyTimestamp`, now()), '%H:%i')) AS `timeDifference`
I may also use a procedure but this one should be executed at least every 3 minutes and I wonder how this procedure would influence overall perfomance of the datatable since there are > 1000000 datasets stored in it.
Any help is appreciated!
Do you really need a column, or do you in fact just need the time difference to be caluclated in your SQL statement.
See : SQL time difference between two dates result in hh:mm:ss