conversion of epoch_time gives '1970-01-01' in mysql - mysql

I've column name epoch_time with a varchar(255) which is having approx 40,000 epoch time in it and I've tried to convert all of them into a equivalent date with this query UPDATE table_name SET converted_into_date = FROM_UNIXTIME(epoch_time);
but it is giving 1970-01-01 05:30:00 in all rows and the datatype of converted_into_date column is varchar. I've tried all the datatype but then it is returning null . working in maria_db.

Please try
SELECT FROM_UNIXTIME(1526019841742682/1000000)

Related

Timestamp gets updated with zero

I need to update a column with datatype as timestamp. I run the following query:
update job_info set ProcessStartTime = UNIX_TIMESTAMP(CURDATE()) where JobID=4;
But it updates with a value : 0000-00-00 00:00:00 What could be the reason for this? Is the query incorrect?
Don't use UNIX_TIMESTAMP because MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds or your column type is datetime
update job_info set ProcessStartTime =CURDATE() where JobID=4;
or use NOW()
update job_info set ProcessStartTime =NOW() where JobID=4;

MySQL - Select where timestamp is midnight

I have a table with a timestamp column in it and I want to select any rows of any date where the time is 00:00:00
Is there a better way to do this other than filtering on the last 8 characters as a substring?
Use the TIME() function.
WHERE TIME(yourfield) = '00:00:00'

Find and replace data in a MySql database

I have two columns in a MySQL database, both has data/time
I have many records, that has actual date/time in the column sent_at like 2012-05-04 16:07:14
But the many records in the column date_recieved_by_proxy has null value like 00/00/0000 00:00
How can I give a sql command to replace date_recieved_by_proxy = 00/00/0000 00:00 records with the date of sent_at field?
update your_table
set date_recieved_by_proxy = sent_at
where date_recieved_by_proxy = '0000-00-00 00:00:00'
Alternatively you can try
where date_recieved_by_proxy is null
depending on the data type of your date_recieved_by_proxy column and if it can be null

Compare Dates using Between with Column Type VARCHAR

I have a database table called UPDATES that has some logs inserted to. In this table, there is a column called Time. This time column is of type VARCHAR. I had no choice but to make it VARCHAR because there are updates with corrupt time like, 2013-04-04 00xcdww and sometimes like 2013-4-2 with some non ASCII values. So if the Column type was DATETIME, the insertions of that datetime couldve triggered an error.
Now i am in the phase of doing a comparison between two dates to get range of results.
So my question is, will using a VARCHAR compare the dates correctly ?
doing :
Date(Time) BETWEEN 2013-03-02 AND 2013-03-02
Or will this return invalid results ? if it does can you tell me what this query will return that is not within the range specified ?
Yes you need to convert the varchar to date format using STR_TO_DATE
WHERE STR_TO_DATE(Time, '%Y-%m-%d')
BETWEEN '2013-03-02' AND '2013-03-02'
mysql> select STR_TO_DATE('2013-04-04 00xcdww', '%Y-%m-%d') ;
+-----------------------------------------------+
| STR_TO_DATE('2013-04-04 00xcdww', '%Y-%m-%d') |
+-----------------------------------------------+
| 2013-04-04 |
+-----------------------------------------------+
STR_TO_DATE is not enough you have to use SUBSTR function to get first 10 VARCHAR.
SELECT *
FROM `TABLE_NAME`
WHERE STR_TO_DATE(substr(`Time`,1,10), '%d/%m/%Y' )
BETWEEN '2013-03-02'
AND '2013-03-02'
This will avoid you from doing casting which affects the performance of the query.
Updated
As your database column may be 2013-04-04 00xcdww and sometimes like 2013-4-2 so you should use this query,
SELECT *
FROM `TABLE_NAME`
WHERE STR_TO_DATE(`Time`, '%d/%m/%Y')
BETWEEN '2013-03-02'
AND '2013-03-02'

How to get a total days count

Using MySQL & SQL Server
ID sDate
001 03/06/2010
002 07/08/2010
....
....
sDate Datatype is varchar
Format mm/dd/yyyy
I want to take the date count means How many days is still there, It should compare the system date...
How to convert my varchar to datetime datatype, then how to compare the mysdate to system for getting the totalday counts
sDate - SystemDate = Totalday
ExpectedOutput
ID sDate Totaldays
001 03/07/2010 3 days
002 07/07/2010 7 days
.....
How to make a query for this condition. Need query help.
Your question states MySQL & SQL Server so here is both:
SQL Server datediff function:
SELECT ID, DATEDIFF(DAY, GETDATE(), CONVERT(DATETIME, sDate)) FROM TABLE
MySQL datediff function:
SELECT ID, DATEDIFF(CURDATE(), STR_TO_DATE(sDate, '%c/%d/%Y')) FROM TABLE
This uses the STR_TO_DATE function to convert the varchar to a date and assumes that the format of your date strings is in the format month/day/year.
Gopal, in response to your "How to convert varchar to datetime in mysql...", it's easy:
ALTER TABLE sometable CHANGE sDate sDate datetime;
and MySQL will happily attempt to convert the values for you. However, if it can't properly parse the original date string, that record's sDate will get set to NULL or 0000-00-00. You'll have to massage the sDate field first to convert it to a more normal MySQL format for date strings, which is YYYY-MM-DD. A bruteforce hack would be:
UPDATE sometable SET sDate=CONCAT(
SUBSTR(sDate, 6, 4),
'/',
SUBSTR(sDate, 3, 2),
'/',
SUBSTR(sDate, 0, 2)
);
Of course, this is assuming that your dates are in DD/MM/YYYY format. If they're MM/DD/YYYY, then just swap the middle and last SUBSTR calls. Once this update's completed, then you can use the ALTER TABLE to change field types.
And of course, for anything that affects the entire table like this, make sure you have a backup of it first.