How to convert/cast varchar to date? - mysql

I have a date column with data type varchar(mm-dd-yyyy) in mySQL 5.1. How do I convert it to DATE?
Here is what I have so far -
SELECT id, date
FROM tableName
WHERE (CAST((SUBSTRING (date FROM 7 FOR 4 )||'/'||SUBSTRING (date FROM 4 FOR 2 )||'/'||SUBSTRING (date FROM 1 FOR 2 )) AS DATE) >= '01/01/2012' )
ORDER BY date DESC;
Getting this
error - #1064 - 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 'FROM 7 FOR 4 )
Please help.

You can use MySQL's STR_TO_DATE() function
SELECT id, date
FROM tableName
WHERE STR_TO_DATE(date,'%Y-%m-%d') >= '01/01/2012'
ORDER BY date DESC;
Although I suspect you will have an easier time using Unix Timestamps
SELECT id, date 
FROM tableName 
WHERE UNIX_TIMESTAMP(STR_TO_DATE(date,'%d/%m/%Y')) >= UNIX_TIMESTAMP('01/01/2012') 
ORDER BY date DESC;

Related

Temporary Table and Set Variables mysql --- UPDATES

I want to both create a temporary table and utilize a variable. I have consulted the documentation and I believe the syntax is correct but putting them together is giving me an error.
My goal is to create a date variable that is always 1 year before now() and will populate my temporary table with one month increment rows until the present moment. Any help would be greatly appreciated.
Error:
"Error: 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 'SET #start_date = DATEADD(year, -1 ,GETDATE())\n\n WHILE #start_date < GETD' at line 4"
Here is my SQL statement.
CREATE TEMPORARY TABLE helper(
date datetime not null
);
SET #start_date = DATEADD(year, -1 ,GETDATE())
WHILE #start_date < GETDATE()
BEGIN
INSERT INTO helper VALUES (#start_date)
SELECT #start_date = DATEADD(MONTH, 1, #start_date)
END
update 1
with this code I am still receiving a syntax error
CREATE TEMPORARY TABLE helper(
date datetime not null
);
insert into helper (date)
with recursive cte as (
select curdate() - interval 12 month as date
union all
select date + interval 1 month
from cte
where date < curdate()
)
select date from cte;
select * from helper;
error
"Error: 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 'insert into helper (date)\n with recursive cte as (\n select c' at line 4",
UPDATE 2
I have changed the syntax to this. I am still getting errors but it seems to be closer to mySql 5.7
`
CREATE TEMPORARY TABLE helper(
date datetime not null
);
CREATE PROCEDURE dowhile()
BEGIN
DECLARE date datetime DEFAULT SUBDATE(curdate(), interval 12 month);
WHILE date < curDate() DO
INSERT INTO helper VALUES(date);
SET date = date + interval 1 month;
END WHILE;
END;
DELIMITER;
CALL dowhile()
SELECT date_format(log.entry_stamp, '%Y-%M') AS 'date', COUNT(log.element_id) as count FROM dm_log log
INNER JOIN dm_element el
ON el.element_id = log.element_id
WHERE ? = el.dm_id
LEFT OUTER JOIN helper h
ON h.date = log.date
GROUP BY date
ORDER BY date;
DROP TEMPORARY TABLE helper;
`
Error
"Error: 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 'CREATE PROCEDURE dowhile()\n BEGIN\n DECLARE date datetime DEFAU' at line 4",
You can use a recursive CTE instead of a WHILE loop:
insert into helper (date)
with recursive cte as (
select curdate() - interval 12 month as date
union all
select date + interval 1 month
from cte
where date < curdate()
)
select date from cte;
Here is a db<>fiddle.
Note that MySQL does not use getdate(). And doesn't support while loops outside of programming blocks. And doesn't have a three-argument form of dateadd(). It looks like you are confusing MySQL with SQL Server.
In earlier versions, you'll probably need to list the values explictly:
insert into helper (date)
select curdate() union all
select curdate() - interval 1 month union all
. . .;

MySQL (Aurora) update timestamp using adddate or date-add or date-sub says syntax error

Can any of you figure out what is the syntax error on mySQL update statement here ?
update table_name set start_time = DATE_ADD (start_time , INTERVAL 2 DAY) where start_time = '2020-12-08 10:47:00';
The above is simple mySQL query to update start_time (time stamp) by 2 days.
AFAIK, the above should work and its nothing complicated. But i am getting syntax error and i am not able to comprehend why there is a syntax error. Here is the error i got...
Database error code: 1064. Message: 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 ') where start_time = '2020-12-08 10:47:00'' at line 10
I have also tried other variants such as...
update table_name set start_time = adddate (start_time , INTERVAL 2 DAY) where start_time = '2020-12-08 10:47:00';
update table_name set start_time = date_sub (start_time , INTERVAL -2 DAY) where start_time = '2020-12-08 10:47:00';
Database error code: 1064. Message: 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 ') where start_time = '2020-12-08 10:47:00'' at line 3
If i use the date_sub in select statement,
select * from table_name where start_time > DATE_SUB(now() , INTERVAL 2 DAY);
it does work like a charm. But i am not able to use it in update statement for set =....
I am just not able to comprehend what is going wrong and curious to know what is wrong with the syntax. Can you suggest ?
It seems like the problem is the space between the function name and the opening parenthese.
This:
DATE_ADD (start_time , INTERVAL 2 DAY)
Should be written:
DATE_ADD(start_time , INTERVAL 2 DAY)
This is not happening in the example using DATE_SUB(), because the leading space is not there.
Not all MySQL functions have such restriction. This is related to the way the MySQL parser handles function names, which is described in the documentation:
The requirement that function calls be written with no whitespace between the name and the parenthesis applies only to the built-in functions that have special considerations.
DATE_ADD() and DATE_SUB() are listed as affected functions. You can play around with SQL mode IGNORE_SPACE to change the default behavior.
Overall, I would recommend using date arithmetic like so, so you don't need to worry about such caveat (and it makes for more readable code as well):
start_time + INTERVAL 2 day

How to update the current time with more than 2 condition in mysql

I want to update my data with the current time but why its still error when simulated in Mysql like this:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'GetDate())) AND (jam_pulang <> (Select GetDate()))' at line 1
and below is mine
UPDATE Presensi
SET jam_pulang=(Select GetDate())
WHERE NIP IN(SELECT nip from presensi where faceid=101)and Tanggal in(Select Convert(date, GetDate()));
getdate is not a function in mysql.
please edit getdate() to now() or curdate()
GETDATE() is the date function for SQL Server. In MySQL you want to use NOW()
In MYSQL GETDATE() Function doesn't work, instead of this you can use current_date or current_timestamp or now()::timestamp function
UPDATE Presensi
SET jam_pulang=current_date
WHERE NIP IN(SELECT nip from presensi where faceid=101)and Tanggal = current_Date;

Select date between current date and one week InnoDB

I have table with one column (date of expiration). I want to select all rows where expiration is between today and one next week. I am using MySQL and InnoDB type.
I try something like this:
SELECT name, expiration
FROM exp
WHERE (expiration BETWEEN(CURRENT_DATE, INTERVAL 1 WEEK));
But i have bad syntax. There is error:
#1064 - 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 ') LIMIT 0, 30' at line 3
Try this:
SELECT *
FROM exp
WHERE expiration BETWEEN
current_date
AND
current_date + interval 7 day
demo: --> http://www.sqlfiddle.com/#!2/8598a/2
This should be the syntax you want:
SELECT name, expiration FROM table WHERE expiration
BETWEEN
CURRENT_DATE
AND
ADDDATE(CURRENT_DATE, INTERVAL 1 WEEK)
(You need to add the one week to the current date, you cannot just specify the interval standing on its own/as a parameter to BETWEEN)

Compare dates in MySQL

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