CONVERT_TZ() cannot accept variable as input - mysql

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

Related

Mysql: Using str_to_date on a select query returns nothing

I've following Mysql query:
select str_to_date((select distinct cast(substr(tb2.sub1,1,4) AS CHAR) as year from (
SELECT SUBSTRING_INDEX(file_name,'_',-1) as sub1 from table2) as tb2) , '%Y')
And it is correct because mysqlworkbench returns green flag but no output.
Could you help me?
The expression
select str_to_date('2007', '%Y')
returns 2007-00-00. Some MySQL servers are set to disallow invalid dates. Try using
select makedate('2007', 1)
instead. That will give you the valid date of 2007-01-01.
I'm leaving it to you to edit your query to make the change.

Mysql str_to_date shows null instead of formatted date

I am facing a silly problem while converting datetime column into y-m-d format.
I am running following query :
SELECT STR_TO_DATE(dateadded, '%y-%m-%d') FROM my_table
Note : The dateadded column is a type of datetime.
Whenever I am running above query it always shows (NULL).
Can somebody solve this issue as this is irritating me ?
Since your column is of datetime type rather than string type you should use DATE_FORMAT function as below:
SELECT DATE_FORMAT(dateadded, '%y-%m-%d')
FROM my_table
This query will work for a four digit year in table
SELECT STR_TO_DATE(dateadded, '%Y-%m-%d')
FROM my_table
while this will work for a two digit year
SELECT STR_TO_DATE(dateadded, '%y-%m-%d')
FROM my_table
you can try this by following query
SELECT
STR_TO_DATE('99-12-12', '%y-%m-%dd'),
STR_TO_DATE('1999-12-12', '%Y-%m-%dd')
both output will be
1999-12-12
for further details

mysql query with 'like' doesn't work with varchar and space

I have a Db with one table with 3 fields like the following:
user_id TimeStamp Azioni
where the 'timestamp' field is a varchar(25) like this: 2012/09/19 16:34:01.95
It is a varchar and not a timestamp value because i need it to be in the shown format.
And i cannot change its type even if i wanted to.
Now, I'm trying to get all db entries with the same date. For example, when Timestamp contains 2012/09/19
I tied several queries:
Query 0:
SELECT Azioni.Action
FROM Azioni
WHERE TimeStamp LIKE '2012/09/19%'
Query 1:
SELECT `Azioni`.*
FROM Azioni
Where `TimeStamp` LIKE '{2012/09/19}%'
Query 2:
SELECT `Azioni` . *
FROM Azioni
WHERE LOCATE( '2008/09/19', `TimeStamp` ) >0
Query 3:
SELECT `Azioni` . *
FROM Azioni
WHERE INSTR( `TimeStamp` , '2012/09/19' ) >0
Query 4:
SELECT * FROM `Azioni`
WHERE `TimeStamp` like '2012|/09|/19%' escape '|'
and I always get: MySQL returned an empty result set (i.e. zero rows).
But I am sure there are rows containing the said timestamp. What am i doing wrong? Does the 'space' between date and time create a problem? If so how can i solve it? Do you have any suggestion?
EDIT:
Aa suggested, from
SELECT TIMESTAMP, HEX( TIMESTAMP )
FROM Azioni
i get the following
2009-06-06 09:28:00.0000 323030392D30362D30362030393A32383A30302E30303030
2009-06-06 09:29:00.0000 323030392D30362D30362030393A32393A30302E30303030
2009-06-06 09:30:51.0000 323030392D30362D30362030393A33303A35312E30303030
2009-06-06 14:25:00.0000 323030392D30362D30362031343A32353A30302E30303030
2009-06-06 14:26:00.0000 323030392D30362D30362031343A32363A30302E30303030
EDIT 2:
ehm yeah, i was typing the date wrong in the query. Sigh, i'm stupid. Sorry for wasting your time guys.
How about this:
where timestamp like '2012/09/19%'
And, if you are going to call the field timestamp you should store it as a date/datetime/timestamp. Call it something else if it is going to be stored as a string. Timestamp is actually the name of a type in MySQL, so having that in a column name with a different type is quite misleading.
EDIT:
Have you tried:
where left(timestamp, 10) = '2012/09/19'
It sounds like there are string characters in the field, which are preventing reasonable code from working.
SELECT * FROM Azioni
WHERE `TimeStamp' LIKE '2012/09/19%'

How to select rows within some date range

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'

Retrieve records based on Date

I have a date column in my table.
Is there any way in mysql to retrieve record based on date excluding the time part?
I have tried appending * but does not seems to work.
SELECT * FROM myTable where filterDate="2010-08-01 *"
Is there any way to do it like this rather then filtering it by using between?
Thanks.
SELECT * FROM myTable where filterDate LIKE "%2010-08-01 %"
You can use DATE_FORMAT function of mysql
SELECT * FROM myTable where DATE_FORMAT(filterDate,'%Y-%m-%d') ="2010-08-01"
Try this
SELECT DATE_FORMAT(filterDate , '%d-%m-%Y') AS new_date FROM table_name WHERE
DATE_FORMAT(filterDate , '%d-%m-%Y') = '2010-08-01'