Can someone please explain to me why the first code doesn't work? in the first one I am try to extract time only and input in the Time column.
UPDATE SampleData_0816
SELECT Time1 = DATE_FORMAT(opened_first_time, '%h:%i')
SELECT DATE_FORMAT(opened_first_time, '%h:%i')
FROM SampleData_0816
If you use the update function you must use SET instead of SELECT
UPDATE SampleData_0816
SET Time1 = DATE_FORMAT(opened_first_time'%H:%i:%s')
SELECT DATE_FORMAT(colName, '%Y-%m-%d') DATEONLY,
DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY
You can achieve that using DATE_FORMAT().
Related
I want to convert a given date time into another timezone. The code I use is:
SELECT `time` INTO #t1 FROM `table` ORDER BY `table`.`id` DESC LIMIT 1;
SELECT CONVERT_TZ(#t1,'+03.00','+00.00') INTO #time1;
SELECT #time1;
The problem is that I get a NULL result.
I tried this:
SELECT CONVERT_TZ('2020-10-18 11:27:23','+03.00','+00.00') INTO #time1;
SELECT #time1;
and it works without any problem.
What am I doing wrong?
Thank you in advance.
You have an error in timezone offsets. it should contain : (colons sign) instead . (dot):
SELECT CONVERT_TZ('2020-10-18 11:27:23','+03:00','+00:00');
Here example: SQLize.online
I have a table like this:
I want to to do something like this:
select * from stat_tps Where min_date ='2013-06-12'
but the problem as you see the min_date is datetime format, so I have to provide the hh:mm:s part. I was wondering if there is any way that I can apply my query without specifying the hours-minutes and seconds?
thanks..
select * from stat_tps
Where date(min_date) = '2013-06-12'
But that won't take use of indexes if you have one on the min_date column. Better use
select * from stat_tps
Where min_date >= '2013-06-12'
and min_date < '2013-06-13'
Use the DATE() function:
SELECT * FROM stat_tps WHERE DATE(min_date) ='2013-06-12'
You need to specify a range, this will effecitvely use an index.
select * from stat_tps
Where min_date BETWEEN '2013-06-12' AND DATE_ADD('2013-06-12',INTERVAL 1 DAY)
Use the SUBSTR function to chop off the time.
I want to select rows that falls between some date range, I tried the following query but it didn't work.
SELECT * FROM tbl WHERE DATE_FORMAT(date_col, '%2011-%c-%e') BETWEEN '2011-11-28' AND '2011-12-5'
It doesn't seem like the BETWEEN keyword works on date. Please how do I get the results? Thanks
You don't need to use DATE_FORMAT if you want to compare dates.
SELECT *
FROM tbl
WHERE DATE(date_col) BETWEEN '2011-11-28' AND '2011-12-05'
Your code compares strings, assuming you use DATE_FORMAT(date_col, '%Y-%c-%e')
SELECT * FROM tbl WHERE DATE_FORMAT(date_col, '%Y-%m-%d') BETWEEN '2011-11-28' AND '2011-12-5'
I am guessing this is relatively simple to do, but I am unsure of the syntax. I have date and time columns that I want to combine to a timestamp column. how would I query this using a select?
Or you could use the built-in TIMESTAMP(date,time) function.
So then you would do something like this say from an Orders table...
SELECT OrderNumber, TIMESTAMP(date,time) as OrderTS, SalesPersonID
FROM Orders
Mysql does not seem to have a constructor for datetime such as datetime('2017-10-26', '09:28:00'). So you will have to treat the component part as string and use string concatenation function (Note mysql does not have the || operator for string concatenation). If you want the datetime type, you will have to cast it.
concat(datefield,' ',timefield) as date
select cast(concat('2017-10-26', ' ', '09:28:00') as datetime) as dt;
If it possible to use built-in function, just use it.
Any way here is an example to find records between given timestamps.
SELECT `id` FROM `ar_time` WHERE TIMESTAMP(`cdate`,`ctime`) BETWEEN fromTimeStamp AND nowTimeStamp;
For 24hr time
TIMESTAMP(Date, STR_TO_DATE(Time, '%h:%i %p'))
SELECT * FROM tablename WHERE TIMESTAMP(datecol, timecol) > '2015-01-01 12:00:00';
O.P. did say SELECT but in case anyone wants to add a timestamp column:
ALTER TABLE `t` ADD COLUMN `stamp` TIMESTAMP;
UPDATE `t` SET `stamp` = STR_TO_DATE(CONCAT(`Date`, ' ', `Time`), '%m/%d/%Y %H:%i:%s');
Adjust format strings to taste.
concat('2021-12-31', ' ', '07:00:00')
it worked in an INSERT procedure.
I have a problem regarding the datediff MYSQL function, I can use it and it is simple. But I don't understand how to use it to collect differences within the table field. E.g.
I have a column dob and I want to write a query that will do something like
select dateDiff(current_timeStamp,dob)
from sometable 'here dob is the table column
I mean I want the difference from the current date time to the table field dob, each query result is the difference, the age of the user.
You mean like this?
SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), dob)), "%Y")+0 AS age from sometable
(Source)
You could do this
SELECT TIMESTAMPDIFF(YEAR, date_of_birth, NOW()) ASageFROM your_table
Works everytime.
If you want, for each user, display the age in years, do
select name,extract(year from (from_days(dateDiff(current_timestamp,dob))))
from sometable;
If I understand your comments on the previous answers, the date-of-birth column is not actually a DATE value but a string in the format m/d/y. I strongly recommend you change this; it slows down any date computations you want to do and you risk invalid date values getting entered into the column.
I think this is what you need. It uses the STR_TO_DATE() function and an algorithm for computing the age from the MySQL documentation:
SELECT YEAR(CURDATE()) - YEAR(STR_TO_DATE(dob, '%m/%d/%Y'))
- (RIGHT(CURDATE(), 5) < RIGHT(STR_TO_DATE(dob, '%m/%d/%Y'), 5)) AS age
FROM sometable;
I think this should help
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(#dateofbirth)), '%Y') + 0;
Note: Give the D.O.B in the correct format, E.g. YYYY-MM-DD'=> '1991-11-11
Try this
SELECT DATEDIFF(CURDATE(), '2014-02-14');
select truncate(datediff(curdate(),dob)/365.25,0) from table;