mySql Date/DateTime default value - mysql

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.

Related

Alternative for from_unixtime in GENERATED ALWAYS query

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.

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

Change datetime format within CREATE TABLE

I hope you might be able to give some advice. I'm trying to create a table within SQL Server 2008 and I need to update my DateTime column to a different format.
CREATE TABLE #History (Tag nvarchar(512), User nvarchar(40), DateTime datetime, Quality int)
However I need to make the DateTime column return a DateTime value in the following format:
yyyy-MM-dd THH:mm:ss
Thanks in advance for any advice you can give.
There's no dateformat there's only the binary data!
Your date data is persisted as bunch of 0 and 1 and got no format at all.
when you retrieve that data, let's say in a select the query analyzer ill show you a representation of that data, by default a ISO formated datetime.
If anything consuming that result set needs a specif date format you can use cast/convert or set dateformat (and language) to achieve that.
See Damien comment for a good example.

Bulk Update Datetime column in SQL

I have a few datetime columns to bulk update. Originally because this data is being used as a demo I had used this to update my date columns to more recent dates.
UPDATE tblVehicleReservation
SET [PredictedJobEndDate] = GETDate()
WHERE [PredictedJobEndDate] IS NOT NULL
It has now come to light that these are needed with the time as 00:00:00.000 as opposed to 13:00:32.957 while keeping the date part the same.
After researching the only resolutions I found to this converted my date to a string.
Ideal Resolutions
Something I can use to set the time on each date to 0's.
A hint how to go through the update process again entirely and set a date without 0's
I figured this out while writing it...
I decided to just run the updates again and set the date manually instead of using GetDate().
Duh.

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!