Temporary Table and Set Variables mysql --- UPDATES - mysql

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
. . .;

Related

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;

How to convert/cast varchar to date?

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;

How to calculate date using interval from table

I have a table with a start_date, end_date and an interval. I would like to update end_date with the value of start_date and the interval.
create table date_test (
start_date date,
end_date date,
date_interval varchar(45)
);
The values I am using for date_interval are like - INTERVAL 1 WEEK, + INTERVAL 1 MONTH.
I would like to do something like:
UPDATE date_test SET end_date = date( concat( start_date, " ", date_interval));
but I get this warning:
1292 Truncated incorrect date value: '2012-01-01 - INTERVAL 1 week'
How can I force this date to get evaluated before updating?
Jonathan Leffler said :
Nearly; there's a crucial difference between the manual page and the question, though. The manual discusses DATE_ADD(date_value, INTERVAL '1' DAY) etc, whereas the question would be having a 'string' value as the second parameter. I fear the question would need a function to convert the string into an INTERVAL type. There doesn't appear to be a 'TO_INTERVAL' function in MySQL.
Here is a function that will take the date as first parameter and the string interval as second parameter.
Simply add the following stored procedure to your database :
CREATE PROCEDURE my_date_add(d DATE, i VARCHAR(50))
BEGIN
DECLARE sign CHAR(1);
DECLARE x INT;
SET sign = SUBSTRING_INDEX(i, ' ', 1);
SET x = SUBSTRING_INDEX(SUBSTRING_INDEX(i, ' ', -2), ' ', 1);
IF sign = '-' THEN
SET x = -x;
END IF;
CASE SUBSTRING_INDEX(i, ' ', -1)
WHEN 'DAY' THEN SELECT DATE_ADD(d, INTERVAL x DAY);
WHEN 'WEEK' THEN SELECT DATE_ADD(d, INTERVAL x WEEK);
WHEN 'MONTH' THEN SELECT DATE_ADD(d, INTERVAL x MONTH);
END CASE;
END
Then you should be able to update your table like this :
UPDATE date_test SET end_date = my_date_add(start_date, date_interval);
What you want to do is :
UPDATE date_test SET end_date = DATE_ADD(start_date, date_interval);
But I'm not sure that using date_interval as the second parameter will work, tell us if it does.
You will find a lot of useful examples in the MySQL documentation, see DATE_ADD() function description.
MySQL does not support values evaluating. So, you cannot use an UPDATE statement directly.
In this case I'd suggest you these ways:
Write a SELECT...INTO OUTFILE statement that would generate a list of UPDATE statemants and output all this statements into the file, then just run this sript.
Or write a stored procedure that would open a cursor on date_test table, in a loop generate and execute UPDATE statements for each record, one by one, using prepared statements.
Ask, if you have a questions about the solutions.

SQL - Select entries more than 1 day old. error #1305 Function doesn't exist

for a basic SQL command via PHPMyAdmin in MySQL I want to select items more than 1 day old. I'm getting a basic error saying that there's a SQL syntax error, but can't tell what I'm doing wrong here:
COMMAND:
select *
from table_name
where column_name < Date_Add(day, -1, GetDate())
and user_id = 1
and column_name <> '0000-00-00 00:00:00'
ERROR:
#1064 - You have an error in your SQL syntax; check the manual for the right syntax to use near '-1, GetDate()) and user_id = 1 and column_name <> '0000-00-00 00:00:00' LIMI' at line 3
Any clues? Thx!!
I was thinking the syntax here is DATE_ADD.
Ok here is from the 5.0 reference guide:
The DATE_ADD() function and its
synonym ADDDATE() allow you to add or
subtract an interval to the selected
date, date function or date constant.
DATE_SUB() and SUBDATE() work in the
same way but the interval specified is
subtracted. (If the interval was
negatvie DATE_SUB() makes it
positive).
mysql> SELECT NOW(), DATE_ADD(NOW(), INTERVAL 1 MONTH) \G
*************************** 1. row ***************************
NOW(): 2008-09-25 11:43:29
DATE_ADD(NOW(), INTERVAL 1 MONTH): 2008-10-25 11:43:29
1 row in set (0.00 sec)
Try using NOW() for the error.
Some engines (i.e. tsql) use DateAdd()... MySql uses Date_Add() - your missing an underscore.
Try
Date_Add(CurDate(), INTERVAL -1 DAY)
I voted up NgM's response ABOVE for his idea to use NOW() - here's the final version that worked:
select *
from table_name
where column_name < DATE_ADD(NOW(), INTERVAL -1 DAY)
and user_id = 1
and column_name <> '0000-00-00 00:00:00'
Isnt it
Date_Add(....)
or
AddDate(...)

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