Would like to convert timestamp stored in SQL to a specific timezone.
SQL Query to offset created_at time by -33000
select cast(created_at - 33000 as date) from table_name
The query is working fine but it's giving NULL for timestamp 2019-11-14 02:52:31.
Any hints is appreciated.
Try this:
SQL SERVER:
Your query will cause some explicit conversion error. So you have to convert it into proper Datetime, and then try you query.
select cast(CAST('2019-11-14 02:52:31' AS DATETIME) - 33000 as date)
MySQL
You have to use DATEADD function.
SELECT DATE_ADD('2019-11-14 02:52:31', INTERVAL -330 MINUTE);
The output will be 2019-11-13 21:22:31
Hope this will fix your issue.
Related
I have date in database like 2019-05-02 12:14:20 and field name is created_date Now i want to get record with created date like 2019-05-02 12:14. Don't want to check seconds.
I've tried with separating time but it won't work could you please suggest some solution which we can achieve in single mysql query.
Thanks in advance
You can use DATE_FORMAT to check a specific part of datetime to a string:
SELECT *
FROM table_name
WHERE DATE_FORMAT(created_date, '%Y-%m-%d %H:%i') = '2019-05-02 12:14'
demo on dbfiddle.uk
I am using this query to find some records from my user table in my own timezone. Can someone help out please? This is the query I am using on my MySql DB:
select *
from user
where date(convert_tz(now(), '+00:00', '+05:30')) > date(convert_tz(start_date, '+00:00', '+05:30')) AND
now() < end_date;
Try using datestamp or timestamp as the datatype while you are declaring date. Using this might help your code fetch the date and time format that you are using in your computer.
we have a store procedure, the IN parameter is DATE today. in this procedure, a aql is to compare this today value with a table which has a timestamp column.
for example:
column A
2012-12-01 00:00:00
SQL:
select * from t where A = today.
We run this procedure in phpmyadmin, it run OK. but it's not work in command line.
Why?
Guess you may need to format both dates into a common format.. To be safe you may even add Date() or str_to_Date if required...if you are not sure column A contains a proper date...
Try this please:
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(TODAY, '%d/%m/%Y');
if you meant CURDATE() by today then try this as well,
SELECT * FROM tablename
WHERE DATE_FORMAT(A, '%d/%m/%Y') = DATE_FORMAT(CURDATE(), '%d/%m/%Y');
It's possible that the dates are in different formats and that's causing them to be not equal. You can use datediff(date1, date2) = 0 to fix this.
http://www.w3schools.com/sql/func_datediff_mysql.asp
I'm trying to insert the date in a a query, using the NOW() statement.
However only the Y-m-d are being inserted correctly, while the hours, minutes and seconds are all appearing zeros ( 00:00:00 )
Any reason for that?
Did you check the type of the column you are inserting into? Make sure it's datetime, not just date.
Refer to the docs for more info.
Try this :
SELECT CURRENT_TIMESTAMP
or
SELECT GETDATE()
or
Select {fn NOW()}
Note the accolades in the function.
I want to compare a date from a database that is between 2 given dates.
The column from the database is DATETIME, and I want to compare it only to the date format, not the datetime format.
SELECT * FROM `players` WHERE CONVERT(CHAR(10),us_reg_date,120) >= '2000-07-05' AND CONVERT(CHAR(10),us_reg_date,120) <= '2011-11-10'
I get this error when I execute the SQL above:
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near
'us_reg_date,120) >= '2000-07-05' AND
CONVERT(CHAR(10),us_reg_date,120) <=
'2011-' at line 1
How can this problem be fixed?
You can try below query,
select * from players
where
us_reg_date between '2000-07-05'
and
DATE_ADD('2011-11-10',INTERVAL 1 DAY)
That is SQL Server syntax for converting a date to a string. In MySQL you can use the DATE function to extract the date from a datetime:
SELECT *
FROM players
WHERE DATE(us_reg_date) BETWEEN '2000-07-05' AND '2011-11-10'
But if you want to take advantage of an index on the column us_reg_date you might want to try this instead:
SELECT *
FROM players
WHERE us_reg_date >= '2000-07-05'
AND us_reg_date < '2011-11-10' + interval 1 day
This works:
select date_format(date(starttime),'%Y-%m-%d') from data
where date(starttime) >= date '2012-11-02';
Note the format string %Y-%m-%d and the format of the input date. For example 2012-11-02 instead of 12-11-2.
I got the answer.
Here is the code:
SELECT * FROM table
WHERE STR_TO_DATE(column, '%d/%m/%Y')
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')
this is what it worked for me:
select * from table
where column
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')
Please, note that I had to change STR_TO_DATE(column, '%d/%m/%Y') from previous solutions, as it was taking ages to load