SQL Syntax error using BETWEEN - sql

I have a table named ticket. I am trying to query between two dates, however; I keep getting a syntax error. Any help would appreciated as to what this syntax error may be.
SELECT date
FROM ticket
WHERE date BETWEEN YEAR(ticket.date)=2011 AND MONTH(ticket.date)=11 AND DAY(ticket.date)=06
AND YEAR(ticket.date)=2011 AND MONTH(ticket.date)=11 AND DAY(ticket.date)=12
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 '=2011 AND MONTH(ticket.date)=11 AND DAY(ticket.date)=06 AND YEAR(ticket.date)=20' at line 1

date BETWEEN YEAR(ticket.date)=2011
doesn't make any sense. The syntax is
date BETWEEN start_range AND end_range
So presumably something like
date BETWEEN '2011-11-06' AND '2011-11-12'
Or as you have a time component you should avoid use of BETWEEN at all and just use
date >= '2011-11-06' AND date < '2011-11-13'

This is an incorrect BETWEEN clause:
BETWEEN YEAR(ticket.date)=2011
It should look like:
BETWEEN YEAR(ticket.date) AND 2011

I think this we will do the trick:
"SELECT
date
FROM
ticket
WHERE
date BETWEEN '2011-11-06' AND 2011-11-12'"
You shouldn't use BETWEEN like you do. You are making a evaluation in a evaluation your (my)sql doesn't support that.

try this:
SELECT date
FROM ticket
WHERE DAY(ticket.date) BETWEEN 6 AND 12
AND MONTH(ticket.date) = 11
AND YEAR(ticket.date) = 2011

You cannot list multiple values in between like that.
Rewrite the query to:
SELECT date
FROM ticket
WHERE date BETWEEN '2011-11-06' and '2011-11-12'

Related

I need to get month and year from dateColumn

i'm stuck with this part of the query, where I need to get only just month and year from my table.
To put in context, i have tried with "TRUNC_DATE", and Oracle give me back an error like
ORA-00904: "DATE_TRUNC": invalid identifier sql
This piece of query get it from Udacity, but in sql Developer looks like it doesn't work.
When I try search deeper, I find something like convert(char(7), ia.invoice_date(), 120) yearMonth, It still not working and an error came back. ORA-00936: missing expression
I tried a lot of ways but no solutions.
If anyones have an idea o something that could help, I will be grateful.
Here below I paste the fragment of the query for guide help:
SELECT
COUNT(ia.invoice_id) total_de_facturas,
SUM(ia.invoice_amount) monto_total,
convert(char(7), ia.invoice_date(), 120) yearMonth
FROM AP.ap_invoices_all ia
GROUP BY ia.vendor_id;
You ae mixing Oracle syntax with Postgres (date_trunc()) and SQL Server (convert()). Many date functions are vendor-specific, so you learn the relevant syntax for your database.
In Oracle, you would use trunc() to truncate a date to the first day of the month:
trunc(ia.invoice_date, 'mm')
Use the EXTRACT function, try these out to test how it works and use it in your query -
SELECT EXTRACT(YEAR FROM DATE '2020-03-07') FROM DUAL;
SELECT EXTRACT(MONTH FROM DATE '2020-03-07') FROM DUAL;
If you want a string value in YYYY-MM format - which seems to be your intention from the 120 style and the char(7) in the SQL Server-syntax convert() call - then you can just use to_char() with that format model:
to_char(ia.invoice_date, 'YYYY-MM') as yearMonth
You don't need to truncate to the first of the month as you're discarding the day part anyway.

Get the number of days from a date field to a constant date

I am trying to get the number of days from a date field to a date that will be constant for all records but that constant date is not in the table. Here is what I tried:
SELECT ACCOUNT_NUMBER, DATEDIFF('30-SEP-20', MATURITY_DATE)
FROM portfolio
...
I'm using Oracle and I am getting an invalid identifier error. I am also trying to do this without an "INSET INTO" statement.
Any help would be great, thanks.
In Oracle, you can use . . . -:
select account_number, (date '2020-09-30' - maturity_date)
from portfolio;
Actually, datediff() is not available in Oracle. I'm not sure what direction you are looking for.

SQL Server - Deleting rows between a date range using SQL. Date conversion fails

DELETE FROM BIZ
WHERE [Orgnl_Cmpltn_Date]
BETWEEN '2014-02-31' AND '2014-04-01'
This is the DELETE statement I wrote. There is an error that says:
Conversion failed when converting date and/or time from character string.
I know I have to write the correct date format, but I am not sure how that goes.
This question has not been answered elsewhere because the answers I saw did not specify date format (in the context that I am asking for)
You wrote 31st of February... Maybe..... that date doesn't exists.
DELETE FROM BIZ
WHERE [Orgnl_Cmpltn_Date]
BETWEEN '2014-02-28' AND '2014-04-01'
For a general idea of convert date:
DELETE FROM BIZ
WHERE [Orgnl_Cmpltn_Date]
BETWEEN CONVERT(date,'2014.02.28',102) and CONVERT(date,'2014.04.01',102)
Here you can find the complete list of values for third parameter of CONVERT
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Use this instead
DELETE FROM BIZ
WHERE [Orgnl_Cmpltn_Date] >= '2014-02-28'
AND [Orgnl_Cmpltn_Date] <= '2014'04'01'
I don't know if this matters, but February has only 28 or 29 days.
I assume you use SQL Server, try this..
DELETE FROM BIZ
WHERE CONVERT(DATE,[Orgnl_Cmpltn_Date])
BETWEEN CONVERT(DATE,'2014-02-28') AND CONVERT(DATE,'2014-04-01')
a couple of things
1) There is no such Date as February 31st, this could be a problem.
2) If you put your date range in the following format, you may have more luck:
BETWEEN '20140228' AND '20140401'
Let me know how you get on :-)
You can use the following code:
DELETE from Tabel_Name WHERE Date BETWEEN CONVERT(datetime,'01/01/2001',103) and CONVERT(datetime,'31/12/2001',103)

How to get 6 months from a parametrized date

I have a where clause where I am trying to get a date within a certain range with parameters like so,
(AL.INSERTED_DATE BETWEEN (:begindate) AND (:enddate))
The problem is that I need to get six months before the begin date but I get an error, ORA-00904: "DATEADD": invalid identifier, when I try,
(AL.INSERTED_DATE BETWEEN DATEADD(Month,-6,(:begindate)) AND (:enddate))
Can anybody point me out to what I could be doing wrong?
You're not using SQL Server, you're using Oracle - that's why it's giving you an error in Oracle format.
http://psoug.org/definition/ADD_MONTHS.htm
ADD_MONTHS would probably be the best equivalent to what you're trying to do here -
(AL.INSERTED_DATE BETWEEN ADD_MONTHS((:begindate),-6) AND (:enddate))
Check this solution:
Oracle (10g) equivalent of DATEADD(weekday, -3, GETDATE())

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...