I need to select a timestamp and insert a date - mysql

I have a timestamp in a database table. Now everytime a record is amended the timestamp changes - this isn't really what I want as the record represents a sale so everytime the data is amended it looks like the sale time has changed! Thus I have added a new field to the database table called 'sale_date' and I want to select the timestamp field of that record, called 'sale_time' and convert the timestamp to the format dd/mm/yyyy hh:mm:ss. and insert it into the new 'sale_date' field (which is text not date)
Any ideas? I'm rubbish at mysql.
If I haven't explained myself well please say.

The definition of your timestamp column (one that changes on updates) constains ON UPDATE CURRENT_TIMESTAMP clause. Remove it (ALTER TABLE) and it will stop updating.
And please, pleasee, please, do not ever store dates as text.

Related

convert multiple epoch time to date in sql together

I am having`a whole mysql table of approx 40,000 rows with a column named 'epoch_time' and there is epoch time in it and I want to convert that whole table's 'epoch_time' to a equivalent 'date' together in a single sql query and I'm doing this is in php_my_admin . Thanks in advance.
I guess by epochtime you mean UNIX-style timestamps, that is, number of seconds since 1970-01-01T00:00Z. If my guess is wrong, so is the rest of my answer.
First you add a new column to the table.
ALTER TABLE mytable ADD COLUMN datestamp DATETIME AFTER epochtime;
This names the new column datestamp and puts it right after epochtime in the list of columns.
Then you update the whole table to populate the new column from the old using FROM_UNIXTIME(). Omitting the WHERE clause makes the update work on the whole table (careful!).
UPDATE mytable SET datestamp = FROM_UNIXTIME(epochtime);
Finally, if you wish you can drop the old column.
UPDATE TABLE mytable DROP COLUMN epochtime;
If I were you I'd try all this on a copy of your database to ensure it is correct before doing it on your production database.
If your epochtime values already have the TIMESTAMP data type, they are already stored internally as UTC (f/k/a GMT) times. The update operation I suggested will convert them to local time according to the settings on your server.
If your application has users in multiple time zones, you may wish to keep using the TIMESTAMP datatype: it honors time zone settings. If your epoch times are stored in an INT column, you can create your new column with the TIMESTAMP rather than DATETIME type by substituting this line for the first one in my instructions above.
ALTER TABLE mytable ADD COLUMN datestamp TIMESTAMP AFTER epochtime;

MYSQL query disallowing a timestamp column having two values from the same date

For example if a user inserts '2017-03-13 12:16:18.0' into the timestamp column,
the same user should not be allowed to enter another value in this column IF IT'S ON THE SAME DAY i.e 2017-03-13 (in this case). Or ultimately, update the timestamp column with the previously inserted value ('2017-03-13 12:16:18.0') each time the user tries to insert a timestamp date twice ON THE SAME DAY. I hope I've been explicit enough.
Below is a non-functioning query I came up with, but it shows what I would like the query to do ultimately. Thanks for your help and feedbacks.
INSERT INTO hr.entry(id,entry_time)
VALUES (45,
CASE WHEN '13-03-2017'= CAST(SYSDATE() AS date) THEN
(UPDATE hr.entry
SET entry_time =
(SELECT entry_time
FROM hr.entry
WHERE id=45
AND CAST(entry_time AS date)= CAST(SYSDATE() AS date) )
ELSE
SYSDATE());
You could add a DATE column to your table, and add a unique index to that column. Then, when you insert the timestamp into the timestamp column, you could also insert the date from that timestamp into the DATE column. Attempts to insert a timestamp whose date component already exists in that table would cause MySQL to throw an error.
I think you are going to need a trigger, unless you store the timestamp as a string using YYYY-MM-DD HH:MI:SS format. I don't really recommend that.
So, create a trigger that updates a column called timestamp_date. This simply extracts the date part of the timestamp.
With this column, you can define a unique index:
create unique index entry_userid_timestampdate on entry(userid, timestamp_date);
This will then enforce your condition.
If you decide that you want to store the timestamp as a string, you don't need the trigger (although will need to manually set the "timestamp"). Instead, you can use a prefix:
create unique index entry_userid_timestampstr on entry(userid, left(timestamp_date, 10));

How to emulate MySQL's timestamp data type in SQL Server

MySQL has a data type called timestamp. This is a date time field that is updated to now() when any data in the record is changed.
How can I achieve the same behaviour in SQL Server?
I know about the rowversion data type in SQL Server. I don't want this - I want an auto-updating date time value.
Create an INSTEAD OF INSERT trigger, in which you insert into your table, selecting all the columns from INSERTED except for the date time column you want to always reflect the current date time, and insert GETDATE() in for that column.
Then create an INSTEAD OF UPDATE trigger, and do something similar, running an UPDATE statement against the table from inserted, using GETDATE() for that specific column.
To emulate MySQL's TIMESTAMP column you'll need a DATETIME2(7) column and an INSTEAD OF UPDATE trigger. And use the SYSDATETIME function to populate the datetime2 column.

Automatically insert timestamp if there isn't any in the table

I have a table where I have a long list of timestamps and there could be chances of timestamps missing for an entire day or days and I want to insert a single timestamp for that particular day
For example if there is no timestamp for 2014-04-04 I want it to insert 2014-04-04 00:00:00 for that particular day.
Can this be done in mysql ?
Yes it can.
Add a trigger for after insert to the table. Make the trigger insert the current timestamp if none has been provided.
Or change the column to have a default value of CURRENT_TIMESTAMP.
Those don't resolve the problem of previous entries having no values, but it does fix the problem for the future. Filling in the rest should be a straightforward update: update mytable set its_timestamp = date(datefield) where its_timestamp is NULL.

Which is the correct datatype to store time in SQL?

Here's my table right now (using mysql):
SQL Table: koko_table
name varchar(140)
status varchar(140)
time TIMESTAMP
My issue basically is , I have a form (using php) which user uses to store data only in the status column, my time column captures the time as a permanent data when the user stores in the status column. I think I have not been using accurate DATATYPE for time column, because everytime I visit my database, the time column has different values.
What can be the correct datatype to store time of status input by the user as un-changeable data.
The best data type would be DATETIME.
You can use either TIMESTAMP or DATETIME in MySQL to store date and time.
There are differences though:
TIMESTAMP uses 4 bytes, DATETIME 8 bytes.
Timestamps can be between 1970 and 2038, while datetimes can be between 1000-01-01 00:00:00 and 9999-12-32 23:59:59.
TIMESTAMP values are converted from the current time zone to UTC for storage, and converted back from UTC to the (server's) current time zone for retrieval. This does not happen for DATETIME values where no timezone is implied.
Datetime fields have to be declared in INSERT operations while timestamp fields have the special feature that the first timestamp of a table is (by default) automatically inserted or updated at every INSERT or UPDATE operation with the current timestamp. (That's probably what you are seeing in your scenario.) You can change this behaviour, so only Inserts or only Updates set the timestamp to current timestamp. See MySQL docs: Timestamp properties
To have for example the timestamp automatically stored at Inserts but not changed during Updates, you could set:
ALTER TABLE TableName
CHANGE TimeStampName TimeStampName TIMESTAMP DEFAULT CURRENT_TIMESTAMP ;
I'd prefered DATETIME (the long number) or store it as a formated string like 2011/10/18 13:50. The first one is better in performance and the secound one is easyer to edit on phpMyAdmin or something like that.