DATE_ADD Function - sql

A glitch in code on my website caused the start date and end date in an sql table to be set to the same day for my online member directory. I have the code glitch corrected, but now need a way to fix the data in the database. I am trying to add a certain number of months (12, 18, 24, etc) to the end_date column depending on the package column. I only want to do this when the start_date column is equal to the end_date column. I have a query started, but keep running into an invalid syntax message. Can someone help? My sql is below
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATE_ADD(months,12,end_date)
WHERE start_date = end_date
AND package_id = 1;

SET end_date = DATE_ADD(start_date, INTERVAL 12 MONTH)
This is for MySQL.

If I understand you correctly, you want to use DATEADD instead.
Your syntax seems like DATEADD (datepart , number , date ) which is not the syntax for DATE_ADD
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATEADD(month, 12 ,end_date)
WHERE start_date = end_date
AND package_id = 1;
Check more here

Related

SQL return rows on or after an specific date

I am trying to export a list of member details that have an expiry date after 2019
|name |expiry |
|------|----------|
|arthur|2010-01-01|
|ben |2018-05-17|
|craig |2005-12-30|
|dean |2021-09-02|
so I am just after
|name |expiry |
|------|----------|
|dean |2021-09-02|
I thought I could simply use the date in a WHERE clause
SELECT name, expiry
FROM table
WHERE expiry < GETDATE()
AND expiry >= '2019-01-01'
However, it still returns the other entries and not sure why
Assuming that you are using SQL Server:
SELECT name, expiry
FROM tmp
WHERE expiry < GETDATE()
AND expiry >= CONVERT(DATE, '2019-01-01', 102)
dbfiddle
The query should work the way you intend it to do. Probably the issue is caused by the expiry column not being of type Date for some reason. This could lead to unwanted behavior.

Comparing start dates and end dates using SQL in SAS

I'm trying to compare start dates and end dates to make sure that start dates aren't later than end dates.
Tried posting onto sas community website, but too many post restrictions errors
table:
start_date
1-Mar-19
I have included the proc sql/quit thing, but it gives me "ERROR: Function SUBSTRING_INDEX could not be located."
SELECT SUBSTRING_INDEX(start_date, '-', 1) AS day
, SUBSTRING_INDEX(SUBSTRING_INDEX(start_date, '-', 2), '-',-1) as month
, SUBSTRING_INDEX(start_date, '-', -1) as year
FROM mylib.sheet1
;
Thanks
If you have date variables just compare the values.
start_date <= end_date
If you have strings instead of dates then first convert them into date values. Then you can compare them.
input(start_string,date9.) <= input(end_string,date9.)

Running functions inside of a view Oracle SQL

I'm trying to create a view that returns all the bookings that are between a certain date. I have a table called Booking which includes the columns:
startDate date,
noOfDays int,
and more columns which aren't relevant to the view I'm trying to create.
I'm trying to run the following query to create a view but it seems to fail with the error "ORA-00904: "DATEADD": Invalid identifier"
CREATE VIEW Present_bookings AS
SELECT * FROM Booking
WHERE startDate => '2018-03-12'
AND startDate <= DATEADD(Booking.startDate, 'YYYY-MM-DD', Booking.noOfDays);
What am I doing wrong?
The logic is not correct. All startdate is less than startdate+5 days. Are you sure about this condition? If yes, then it will be simplied as:
CREATE VIEW Present_bookings AS
select *
FROM Booking
where startDate >= to_date('2018-03-12','YYYY-MM-DD')
OR probably you are thinking about this:
CREATE VIEW Present_bookings AS
select *
FROM Booking
where startDate between to_date('2018-03-12','YYYY-MM-DD')
and to_date('2018-03-17','YYYY-MM-DD')
There are a couple of things wrong with your query. But the primary thing is that DATEADD() is not a valid function in Oracle. Oracle date arithmetic is very simple; just add noOfDays to startDate:
CREATE VIEW Present_bookings AS
SELECT * FROM Booking
WHERE startDate >= DATE'2018-03-12'
AND startDate <= startDate + noOfDays;
Note that I changed your operator => to >= and added the DATE keyword to the date literal 2018-03-12 to tell Oracle that it's a date.
As an aside, I don't know what the second condition is supposed to do; startDate should always be less than startDate + noOfDays unless noOfDays is negative. I think you might want something like the following:
CREATE VIEW Present_bookings AS
SELECT * FROM Booking
WHERE startDate <= DATE'2018-03-12'
AND DATE'2018-03-12' <= startDate + noOfDays;
OR
CREATE VIEW Present_bookings AS
SELECT * FROM Booking
WHERE DATE'2018-03-12' BETWEEN startDate AND startDate + noOfDays;
I guess you're looking for a easy way to add a number of days to a date.
Try using interval here:
select sysdate, sysdate + interval '5' day as "FUTURE" from dual;
=>
SYSDATE FUTURE
22.03.2018 14:37:27 27.03.2018 14:37:27
Take care of the '5' as a character, not a number.
This also works with years, months (take care of 29.02 here) etc.

Find data with specific date and month only

I am trying to find a data with specific where clause of date and month but I am receiving an error can anyone help me with this?
select *
from my_data
where date BETWEEN '11-20' AND '12-15'
MS SQL Server Management Studio
I am receving an error
Conversion failed when converting date and/or time from character string
Most databases support functions to extract components of dates. So, one way of doing what you want is to convert the values to numbers and make a comparison like this:
where month(date) * 100 + day(date) between 1120 and 1215
The functions for extracting date parts differ by database, so your database might have somewhat different methods for doing this.
The conversion is failing because you are not specifying a year. If you were to specify '11-20-2015' your query would work just insert whatever year you need.
SELECT *
FROM my_data
WHERE date BETWEEN '11-20-2015' AND '12-15-2015'
Alternatively if you wanted data from that range of dates for multiple years I would use a while loop to insert information in a # table then read from that table, depending on the amount of data this could be quick or sloooowww here is an example.
DECLARE #mindatestart date, #mindateend date, #maxdatestart date
SET #mindatestart = '11-20-2010'
SET #mindateend = '12-15-2010'
SET #maxdatestart = '11-20-2015'
SELECT top 0 *, year = ' '
INTO #mydata
FROM my_data
WHILE #mindatestart < #maxdatestart
BEGIN
INSERT INTO #mydata
SELECT *, YEAR(#mindatestart)
FROM my_data
where date between #mindatestart and #mindateend
SET #mindatestart = DATEADD(Year, 1, #mindatestart)
SET #mindateend = DATEADD(Year, 1, #mindateend)
END
This will loop and insert the data from 2010-2015 for those date ranges and add a extra column on the end so you can call the data and order by year if you want like this
SELECT * FROM #mydata order by YEAR
Hopefully some part of this helps!
FROM THE COMMENT BELOW
SELECT *
FROM my_data
WHERE DAY(RIGHT(date, 5)) between DAY(11-20) and DAY(12-15)
The reason '11-20' doesn't work is because its a character string which is why you have to input it between ' ' What the Month() function does is take whatever you put between the () and convert it to an integer. Which is why you're not getting anything back using the method in the first answer, the '-Year' from the table date field is being added into the numeric value where your value is just being converted from 11-20 you can see by using these queries
SELECT MONTH(11-20) --Returns 12
SELECT MONTH(11-20-2015) -- Returns 6
SELECT MONTH(11-20-2014) -- Returns 6
Using RIGHT(Date, 5) you only get Month-day, then you date the day value of that so DAY(RIGHT(DATE, 5) and you should get something that in theory should fall within those date ranges despite the year. However I'm not sure how accurate the data will be, and its a lot of work just to not add an additional 8 characters in your original query.
Since you only care about month and day, but not year, you need to use DATEPART to split up the date. Try this:
select *
from my_data
WHERE 1=1
AND (DATEPART(m, date) >= 11 AND DATEPART(d,date) >= 20)
AND (DATEPART(m, date) <= 12 AND DATEPART(d,date) <= 15)

how to update the date field for this specific condition using oracle query?

I want to update one date column in oracle table for the below scenario...
currentYR=Get the Current Year from the SYSDATE (2015)
currentYR=currentYR-2(2013)
Need to set date field like 01-JUN-13(Last TWO DIGITS OF currentYR(2013)
Basically i just want to set set date like year=2 years earlier from sysdate and month=june,date=1. and date format is like DD-MON-YY(01-JUN-13)
Please guide me to resolve this problem.
try this:
update yourtable
set yourcolumn = add_months(sysdate, -24)
-24 means 2 years earlier
Based on the comment above I think you would like to set the value of the column to a date where only the last two digits of the year are provided. You can do so by using a Datetime format model reflecting this:
UPDATE <your table> SET <yourcolumn> = TO_DATE('01-JUN-13','DD-MON-YY') WHERE ...;
Finally found the answer. This is the query which i wanted...
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), -19 ) FROM DUAL;
QUERY RESULT
01-JUN-13