how to Migrate Date_ADD method into Db2 for dynamic values - mysql

how to Migrate Date_ADD method into Db2 for dynamic values
where updatedDate BETWEEN ? AND DATE_ADD(?, Interval 1 day)
I need to run a dateadd function that will minus 1 DAY to current date
SQL has a function called dateadd, but it appears DB2 does not have this function. IS there anything equivalent to this function? If so, can someone post a syntax example?
THANKS

In DB2 you can directly add days ( eg. date + 10 days ) , months ( eg. date + 10 months) and years just like arithmetic addition.
The statement actually worked for me in DB2:
Birthdate + 52 Years as AGE 52_DATE
reference: http://www.dbforums.com/showthread.php?1637371-Help-Is-there-a-DATEADD-function-in-DB2

Related

MYSQL appear error #1292 when delete record with in 7 day at 2021 first week

i got a problem for MySQL , last week , i get the error when run this MySQL script
delete from calendar.schedule
WHERE startdate >= DATE(NOW()) - 7 and status = 'ready'
it will display
#1292 - Incorrect datetime value: '20210100' for column 'startdate' at row 1" error .
my testing date is 2021/1/7 , if i change the Mysql script to
delete from calendar.schedule
WHERE startdate >= DATE(NOW()) - 6 and status = 'ready'
it will work normally. now, this code has no issues. but it will have bugs in the first week of the next year. anyone can help with this? Many Thanks!
Wilson
Your issue is with this expression:
DATE(NOW()) - 7
When you try to subtract 7 from a date, MySQL converts the date to its integer representation (in this case I presume it was 20210107) and then subtract 7 from it, giving 20210100. It then tries to compare this to a datetime column and fails, since 20210100 is not a valid date. The code works when you use 6 because you end up with 20210101, which is valid. What you should be doing instead is subtracting an interval (see the manual) so that you use date arithmetic, not integer arithmetic:
CURDATE() - INTERVAL 7 DAY
Note that CURDATE() is equivalent to DATE(NOW())
If you do date arithmetic with integers as you are doing, the date is converted to an integer in the format YYYYMMDD, and then the value is subtracted.
The problem is this can produce a result integer that is not a valid date.
For example if NOW() is '2021-01-10' as it is right now when I run that expression, then DATE('2021-01-10) - 10 evaluates as 20210110 - 10 which is 20210100.
But there is no date with 00 as the day. The subtraction should be 2020-12-31, right? But when doing integer subtraction, that's not what you get.
Solution: Use date arithmetic, not integer arithmetic. You can write date arithmetic in either of the following ways:
DATE('2021-01-10') - INTERVAL 10 DAY
DATE_SUB('2021-01-10', INTERVAL 10 DAY)

Sql -How to fetch the last 5 minutes of data

Hello I am attempting to pull the last 5 minutes of data from the database.
The query I have written below is not pulling the data I need.
Select e.*
from Event e
where e.whenoccurred >= datefunc('10/01/2019 00:00 -05:00', '-5 minutes')
and dateadd(minutes,-5,getdate())
I receive the error
Query has failed: no such column: minutes
Any ideas that can help?
SysDate
returns the current date and time set for the operating system on which the database resides. The datatype of the returned value is DATE, and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter. The function requires no arguments. In distributed SQL statements, this function returns the date and time set for the operating system of your local database.
this query get sysdate minus five minutes.
select *
from event
where whenoccured >= sysdate - (5/1440)
Use
Query #1 Demo
SELECT * FROM event
where whenoccured >= date_sub(now(), interval 5 minute);
Query #2 Demo
SELECT * FROM event
WHERE whenoccured >= NOW() - INTERVAL 5 MINUTE;
Query #3 Demo
SELECT * FROM event
WHERE DATE_ADD(whenoccured , INTERVAL 5 MINUTE) >= NOW();
You can use
select *
from event
where whenoccured >= systimestamp - interval '5' minute
where systimestamp stands to return the current system date, including fractional seconds and time zone.
Update (if MySQL DB is the case instead of Oracle initially as tagged) use date_sub() function:
select *
from event
where whenoccured >= date_sub(now(), interval 5 minute);
assuming whenoccured column is of type datetime or timestamp
Demo
On SQL Server, the first parameter of dateadd function should written in singular. In your case, instead of "minutes" you should use "minute".
Here's a working example:
select getdate()
select dateadd(minute,-5,getdate())
For further details on dateadd function I would suggest you to refer to https://www.w3schools.com/sql/func_sqlserver_dateadd.asp

How to generate a range of dates in MariaDB?

I want to insert the date and day of the week for the whole of 2019-01-01 to 2019-12-31. but I`m unable to.
I try date_format function.
This is my table. table name is food_user and I want to insert the date and day in re_date, re_day 2019-01-01~2019-12-31
INSERT INTO food_user (NAME,re_date,re_day) VALUES ('Alex' , ? , ?);
If you're using MariaDB, try this code to get all dates in year:
SELECT '2019-01-01' + INTERVAL seq DAY FROM seq_0_to_364;
or
SELECT '2018-12-31' + INTERVAL seq DAY FROM seq_1_to_365;
To undestand this, MariaDB has a built in SEQUENCE Engine - reference: https://mariadb.com/kb/en/library/sequence-storage-engine/ .
Basically typing SELECT seq FROM seq_1_to_10; will return you a number seq from 1 to 10. In this case, a whole year is 365 days hence seq_1_to_365 will return number from 1 to 365. IF you're counting from zero (0), then you'll need to consider the last value as 364, hence changing the sequence to seq_0_to_364 as the example above. From your query, you can do something like this:
INSERT INTO food_user (NAME,re_date,re_day)
SELECT 'Alex','2019-01-01' + INTERVAL seq DAY,DAYNAME('2019-01-01' + INTERVAL seq DAY) FROM seq_0_to_364;
Here's an update, a few months ago I've discovered a way of using recursive statement to generate date ranges. The sequence engine is quite useful but it's only specific to MariaDB. WITH RECURSIVE is supported on both MySQL & MariaDB, although it's also version specific; MySQL from version 8 & MariaDB from version 10.2.2. This is the query:
WITH RECURSIVE date_ranges AS (
SELECT '2019-01-01' dt UNION ALL
SELECT dt + INTERVAL 1 DAY FROM date_ranges
WHERE dt + INTERVAL 1 DAY <= '2019-12-31')
SELECT dt FROM date_ranges;
Other than the reason above there's a simple yet important reason why I include this option; it's because of leap years. We had one in 2020 so the total days in that year is actually 366 days instead of 365 days.
Here is a fiddle showing the difference using sequence engine and with recursive.
As you can see in the fiddle, the sequence engine query (for leap year) still return 365 rows and the last date of the year is 2020-12-30.. we're missing a day. That is caused by the numbering sequence constraint that we've defined (seq_1_to_365). Therefore, it might not be the best option to use sequence engine unless you're only using it on non-leap years..
Here is the complete query for the INSERT operation according to the question:
INSERT INTO food_user(NAME,re_date,re_day)
/*recursive statement here*/
WITH RECURSIVE date_ranges AS (
SELECT '2019-01-01' dt UNION ALL
SELECT dt + INTERVAL 1 DAY FROM date_ranges
WHERE dt + INTERVAL 1 DAY <= '2019-12-31')
/*select statement here*/
SELECT 'Alex',dt, dayname(dt) FROM date_ranges;
Demo fiddle
If you use MySQL,
You can try
insert into food_user(name, re_date, re_day) values("Hello", "2019-01-01", dayofweek("2019-01-01"));
If you want to insert all days from 2019-01-01 to 2019-12-31, you can use php or any other server side language to insert records.
If you are using Oracle, you can use PL/SQL feature to insert all of them
maybe this can help
insert into food_user (name,re_date,re_day) values ('Alex' , DATE_FORMAT(NOW(), "%Y-%m-%d") , DATE_FORMAT(NOW(), "%W"));
from https://www.w3schools.com/sql/func_mysql_date_format.asp

How to convert datediff & curdate() from MySql to PostgreSQL for a weekly selection range

I have the following SQL query which works fine in MySQL:
SELECT floor(datediff(users.created_at, curdate()) / 7) AS weeks_ago,
I'm want to convert this from MySQL to PostgreSQL. How can I get this query working?
Something like this:
SELECT TRUNC(DATE_PART('day', CURRENT_DATE - users.created_at )/7) AS weeks_ago
If we subtract two DATE or TIMESTAMP in PostgreSQL, we get an interval "ddd days hh:mi:ss"
We can use DATE_PART to extract just the ddd value. (Note that the first argument 'day' is a string enclosed in single quotes, not a keyword.)
Since the MySQL expression appears to be counting weeks as intervals of 7 days, we can do the same thing in PostgreSQL, divide by 7 and take the integer portion.
(n.b. not tested)
Reference:
https://www.postgresql.org/docs/8.0/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

Getting years 2014+ from csv import to mysql

I used phpmyadmin for the csv import to mysql. The data contains date of birth (DOB). Some of the dates go over the current date. Ex: 2035-06-15. I am trying to find a way to fix it. Ex: 2035 to 1935. One approach is to UPDATE query all dates over the year 2013/2014. Is there a way I can make that a statement? I read around and heard that using wildcards for date types is a no-no.
Instead of a wildcard, you can use the YEAR function to get the year:
YEAR(DOB)
Then you can use the DATE_SUB function to subtract the 100 years:
DATE_SUB(DOB, INTERVAL 100 YEAR)
The whole query would look something like this:
UPDATE myTable
SET DOB = DATE_SUB(DOB, INTERVAL 100 YEAR)
WHERE YEAR(DOB) > 2013