I have the table ORDERS and while trying to find the processing time for each order by subtracting the date of receipt from the date of delivery, I keep getting this error:
SQL0206N "DAY" is not valid in the context where it is used.
SQLSTATE=42703
My query is the following:
SELECT DATEDIFF(DAY, ReceiptDate, DeliveryDate) FROM ORDERS
I suspect it has something to do with the date's format in the columns ReceiptDate and DeliveryDate: DD/MM/YY. Does this have to be converted? What am I doing wrong?
EDIT: Is there an alternative for using DATEDIFF? Can I convert each individual date to an int of days and then subtract the two ints?
If you are using DB2, then there is no DATEDIFF function, which is specific to SQL Server. However, we can easily simulate it by taking a difference of days, using the DAYS() function:
SELECT DAYS(DeliveryDate) - DAYS(ReceiptDate) AS days_diff
FROM ORDERS;
Related
I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.
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.
I am trying to get transaction count month to date. For now I am providing date between from_date and to_date.
How can I get results for a particular month in month to date ?
You don't mention what SQL your using, but if its SQL Server then it has a built in function DatePart description I'd expect most of the major SQL dialects to have something similar.
So your where clause would be something like where datepart(mm, to_date) = 4 (for April)
If your version SQL doesn't have the date functions, then you can fallback to the cruder string manipulation. Assuming your date is yyyymmdd, then you could use a sub string function to pull out the mm part, convert that to a integer and perform your comparison.
I'm trying to generate a query that will display the date difference between a date stored in a column and the current date, but i'm getting the error:
"Cannot call method "getTime" of null."
What function can I use to calculate this date difference in Google Cloud SQL?
Current code:
SELECT date, DATEDIFF(date, CURRENT_DATE()) AS daysLeft
FROM table;
Are you using a mysql instance on Google Cloud. If so try the following
SELECT date, DATEDIFF(date, CURDATE()) AS daysLeft
FROM table;
All that changed is CURDATE() instead of CURRENT_DATE()
Turns out, the error was in another part of the code. The originally posted SQL query works perfectly fine, the problem was the model was casting the result as a date variable instead of an int.
The DATEDIFF function requires three parameters.
SELECT date, DATEDIFF(day, date, CURRENT_DATE()) AS daysLeft
FROM table;
This should return your result in days left.
When I write below query it gives record .
SELECT [srno],[order_no],[order_date],[supplier_name],[item_code],[item_name],[quntity]
FROM [first].[dbo].[Purchase_Order]
WHERE order_date BETWEEN '22/04/2015' AND '4/05/2015'
In this query if I don't add 0 in '4/05/2015' it returns record.
But when I add 0 to the date i.e. '04/05/2015' it doesn't give any records.
SELECT [srno],[order_no],[order_date],[supplier_name],[item_code],[item_name],[quntity]
FROM [first].[dbo].[Purchase_Order]
WHERE order_date BETWEEN '22/04/2015' AND '04/05/2015'
The reason it's not working because SQL is trying to do a string comparison because both your types are string types, But what you really want to do a date comparison.
You should do something like this. Since you only need date part you can strip off the time and use style 103 for your format dd/mm/yyyy.
WHERE CONVERT(DATETIME,LEFT(order_date,10),103)
BETWEEN CONVERT(DATETIME,'20150422') AND CONVERT(DATETIME,'20150504')
Alternately you can use this as well if your order_date has dates like this 5/4/2015 03:20:24PM
WHERE CONVERT(DATETIME,LEFT(order_Date,CHARINDEX(' ', order_Date) - 1),103)
BETWEEN CONVERT(DATETIME,'20150422') AND CONVERT(DATETIME,'20150504')
A long term solution is to change your column order_date to DATE/DATETIME
It Better to Cast it to date rather than depend on IMPLICIT conversion
SELECT [srno],[order_no],[order_date],[supplier_name],[item_code],
[item_name],[quntity] FROM [first].[dbo].[Purchase_Order] where
convert(date,order_date,105) BETWEEN cast('22/04/2015' as Date) AND cast('04/05/2015' as date)