Oracle SQL Between Date, Except Some Month - sql

I've got a query which basically get information concerning the last 2 years from the current Date.
DateTime jodaCurrentDate = new DateTime();
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm");
String startingDate = formatter.print(jodaCurrentDate.minusYears(2));
String endingDate = formatter.print(jodaCurrentDate);
"DATE BETWEEN TO_DATE (\'"+ startingDate +"\',\'YYYY-MM-DD HH24:MI\') AND " +
"TO_DATE (\'"+ endingDate +"\',\'YYYY-MM-DD HH24:MI\')";
Is it possible to retrieve only for some particular months (01-02-03-10-11-12) for example?

This where clause filters for a date between two dates and also includes a filter on month. Replace some_date with your date column.
where some_date between to_date('01/01/2015','MM/DD/YYYY') and to_date('12/31/2015','MM/DD/YYYY')
and extract(month from some_date) in (1, 2, 3, 10, 11, 12);

Related

Filtering based on the month someone was born

Currently trying to create a query that filters on the month that someone was born but its returning errors.
I want it to display everyone who was born on 05 month.
SELECT "Person Names"."Full Name" saw_0,
   "Person"."Person Date Of Birth" saw_1
FROM "Workforce Management - Person Real Time"
WHERE "Person"."Person Date Of Birth" = date '%-05-%'
DATE followed by a string is a date literal. The string must have the format yyyy-mm-dd. The expression date '%-05-%' is hence syntactically incorrect.
You can, however, extract a month from a date:
WHERE EXTRACT(MONTH FROM "Person"."Person Date Of Birth") = 5
Considering "Person"."Person Date Of Birth" is of type DATE you could simply do:
WHERE TO_CHAR("Person"."Person Date Of Birth",'MM') = '05'
Update:
WHERE TO_CHAR("Person"."Person Date Of Birth",'MM') = TO_CHAR(SYSDATE,'MM')
This will give you the people born in the current month.
You can use EXTRACT to check the month. In case your column is already of type date, this will do:
SELECT * FROM yourtable WHERE EXTRACT(MONTH FROM datecolumn) = 5;
If the column is of type varchar, you can cast it as date and then do the same:
SELECT * FROM yourtable WHERE
EXTRACT(MONTH FROM TO_DATE(varcharcolumn,'dd.mm.yyyy')) = 5;
Please see a simple example here: db<>fiddle

DATE_ADD Function

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

How to extract records from SQL table falling within certain date range? [duplicate]

I have a SQL statement to display data between two dates. I almost got it but there's a problem.
If I input March 1,2012 to March 7, 2012.. it should show data with dates between the two.. but it also show all of the dates under March 2012.. but whenever I input March 10, 2012 to March 30, 2012 the SQL works perfectly.. any help will be appreciated. thanks
SELECT
agentname, noofcalls, qualified, booking, resched,
actualbooking, sales, remarks,
concat(month,' ',day,',',year) as 'date'
FROM
tblagents
WHERE
(month between '" & cbosmonth.Text & "' AND '" & cboemonth.Text & "')
AND (day between '" & cbosday.Text & "' AND '" & cboeday.Text & "')
AND (year between '" & cbosyear.Text & "' AND '" & cboeyear.Text & "')"
you are doing string comparisons in each of your 'between'. Every number starting with a 1, a 2 or a 3, regardless of what follows it, i.e. 21, or 26, or 31, they are all lower than 7 if you look at them as strings. 1 to 30 works because you're only leaving 31 behind, and 30 < 31 as a String as well.
Do the concatenation first and then the between:
WHERE concat(month,' ',day,',',year)
BETWEEN concat(cbosmonth.Text,' ', cbosday.Text,' ',cbosyear.Text)
AND concat(cboemonth.Text,' ', cboeday.Text,' ',cboeyear.Text)
(check out for correct syntax, I'm just copy pasting from your question, not tried it)
BTW, unless you have a reason to, you probably should be storing the entire date in a single column with the right data time (datetime, timestamp, ...) and not three separated columns.
That is the incorrect way to store a date in database.
Change the date column to datatype date.
You could then do:
SELECT *
FROM table
WHERE bookingDate between to_date ('2012/03/01', 'yyyy/mm/dd')
AND to_date ('2012/03/07', 'yyyy/mm/dd');
This approach is wrong.
In order for a date to be between two interval dates it does not have to have a day number between the two dates, e.g. (pseudocode)
date = May-25-2012; startDate = March-15-2012, endDate = June-01-2012
date is clearly between startDate and endDate, yet
day(date) is 25, which is not between day(startDate) = 15 and day(endDate) = 1
more, as 15 is larger than 1, there are no numbers between them, so that condition will always be false
Similar example can be made for the month part of the date (e.g. date = May-25-2012; startDate = September-15-2010, endDate = Match-01-2015)
You need to take the values for day, month, year, and construct a Date either in the application or on the server and use that to compare the values against.
To make a date from text fields in VB
Dim startDate = new DateTime(
Convert.ToInt32(cbosyear.Text),
Convert.ToInt32(cbosmonth.Text),
Convert.ToInt32(cbosday.Text))
Note that this will fail if the user enters, e.g. "some text" for the year value. You'll need to add some data validations to achieve that.
To make a datetime from parts in SQL Server, take a look here, there are quite a few techniques explained.
Also, you should always avoid just pasting values into the sql string, that's asking for sql injection problems. You should do something like this:
Dim command = new SqlCommand()
command.CommandText = "SELECT .... FROM tblagents where DATEFROMPARTS(year, month, day) between #startDate AND #endDate"
command.Parameters.AddWithValue("#startDate", startDate)
command.Parameters.AddWithValue("#endDate", endDate)

Update birthdate incase the date is Hijri by the value of converting hijri birthdate to Gregorian

Table contains many records with Hijri date value and Gregorian value in BirthDate column, so please how to update Birthdate by the value of converting Hijri date to Gregorian
thanks
I tried this script but it dosen't work - I meant no changes - although I get
(18422 row(s) affected)
UPDATE MEMBER
SET BIRTHDATE = case when (SUBSTRING(cast(birthdate as nvarchar), 1, 2) ='14')
or
(SUBSTRING(cast(birthdate as nvarchar), 1, 2) ='13')
then
(SELECT CONVERT(date, birthdate , 131) )
else
birthdate
end
try the below code it will convert birthdate column records into Hijri format:
update MEMBER
set BIRTHDATE = CONVERT(VARCHAR(100),birthdate,131) --- [Hijri date to Gregorian date]
update MEMBER
set BIRTHDATE = CONVERT(datetime, birthdate, 131) --- [ Gregorian date to Hijri date]
There are several blogs about this, likethis one: http://blogs.msdn.com/b/wael/archive/2007/04/29/sql-server-hijri-hijra-dates.aspx

BETWEEN clause in SQL

I have a SQL statement to display data between two dates. I almost got it but there's a problem.
If I input March 1,2012 to March 7, 2012.. it should show data with dates between the two.. but it also show all of the dates under March 2012.. but whenever I input March 10, 2012 to March 30, 2012 the SQL works perfectly.. any help will be appreciated. thanks
SELECT
agentname, noofcalls, qualified, booking, resched,
actualbooking, sales, remarks,
concat(month,' ',day,',',year) as 'date'
FROM
tblagents
WHERE
(month between '" & cbosmonth.Text & "' AND '" & cboemonth.Text & "')
AND (day between '" & cbosday.Text & "' AND '" & cboeday.Text & "')
AND (year between '" & cbosyear.Text & "' AND '" & cboeyear.Text & "')"
you are doing string comparisons in each of your 'between'. Every number starting with a 1, a 2 or a 3, regardless of what follows it, i.e. 21, or 26, or 31, they are all lower than 7 if you look at them as strings. 1 to 30 works because you're only leaving 31 behind, and 30 < 31 as a String as well.
Do the concatenation first and then the between:
WHERE concat(month,' ',day,',',year)
BETWEEN concat(cbosmonth.Text,' ', cbosday.Text,' ',cbosyear.Text)
AND concat(cboemonth.Text,' ', cboeday.Text,' ',cboeyear.Text)
(check out for correct syntax, I'm just copy pasting from your question, not tried it)
BTW, unless you have a reason to, you probably should be storing the entire date in a single column with the right data time (datetime, timestamp, ...) and not three separated columns.
That is the incorrect way to store a date in database.
Change the date column to datatype date.
You could then do:
SELECT *
FROM table
WHERE bookingDate between to_date ('2012/03/01', 'yyyy/mm/dd')
AND to_date ('2012/03/07', 'yyyy/mm/dd');
This approach is wrong.
In order for a date to be between two interval dates it does not have to have a day number between the two dates, e.g. (pseudocode)
date = May-25-2012; startDate = March-15-2012, endDate = June-01-2012
date is clearly between startDate and endDate, yet
day(date) is 25, which is not between day(startDate) = 15 and day(endDate) = 1
more, as 15 is larger than 1, there are no numbers between them, so that condition will always be false
Similar example can be made for the month part of the date (e.g. date = May-25-2012; startDate = September-15-2010, endDate = Match-01-2015)
You need to take the values for day, month, year, and construct a Date either in the application or on the server and use that to compare the values against.
To make a date from text fields in VB
Dim startDate = new DateTime(
Convert.ToInt32(cbosyear.Text),
Convert.ToInt32(cbosmonth.Text),
Convert.ToInt32(cbosday.Text))
Note that this will fail if the user enters, e.g. "some text" for the year value. You'll need to add some data validations to achieve that.
To make a datetime from parts in SQL Server, take a look here, there are quite a few techniques explained.
Also, you should always avoid just pasting values into the sql string, that's asking for sql injection problems. You should do something like this:
Dim command = new SqlCommand()
command.CommandText = "SELECT .... FROM tblagents where DATEFROMPARTS(year, month, day) between #startDate AND #endDate"
command.Parameters.AddWithValue("#startDate", startDate)
command.Parameters.AddWithValue("#endDate", endDate)