how to calculate number with date in sql oracle cmd - sql

so i try this
select sr.member,
sr.code_book,
bk.title_book,
sr.return_date,
(kg.borrowed_time - sr.borrow_date) as target back
from sirkulasi sr,
book bk,
kategori kg
where sr.code_book = bk.code_book
and sr.return_date = '';
but it say inconsistent datatypes: expected NUMBER got DATE
because the length of the loan is the number and the date of the loan is the date
the question is like this
Show circulation information that has not returned and when it is targeted to return
(The feature that has not returned is the data in the circulation table which is still dated
empty, the return target is calculated based on borrowed_time and borrowed_date according to
category)

Never use commas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax.
Second, the only part of the query that could generate the error is the -. I'm pretty sure you want:
select sr.member, sr.code_book, bk.title_book, sr.return_date,
(sr.borrow_date + kg.borrowed_time ) as target_back
from sirkulasi sr join
book bk
sr.code_book = bk.code_book
where sr.return_date is null;
Notes:
The target return date is (presumably) the borrowed day by the length of time. + is allowed between a date and a number, when first operand is a date and the second a number that represents a number of days.
return_date certainly should be a date. Non-returns should be NULL values not strings. A string is not even appropriate for a date comparison. And = '' never evaluates to "true" in Oracle because Oracle (mistakenly) treats an empty string as NULL.
The table kategori is not used in the query. Remove it.
JOIN. JOIN. JOIN.

Related

compare absolute value with double value present in table using PostgreSQL

select id from records where (mean_logratio = -4.81)
-4.810215473175049 value is present in the table which supposed to be fetched
-4.810215473175049 only exact value is accepted not absolute value, in search query for = and != condition
for absolute value like -4.81, not getting expected results
You can go with either approach:
If you want to compare after rounding off upto two decimal place.
select distinct(workflowid)
from cyto_records r join cyto_record_results rr on (r.recordid = rr.recordid)
where (round(rr.mean_logratio::numeric,2) = -4.81)
If you want to truncate upto two decimal and compare then use below mentioned query:
select distinct(workflowid)
from cyto_records r join cyto_record_results rr on (r.recordid = rr.recordid)
where (trunc(rr.mean_logratio::numeric,2) = -4.81)
In case of data type mismatch error, you may need to cast you data.

Oracle ERROR-01722 not showing up consistently

There seems to be inconsistencies with how ERROR-01722 error worked, for those who don't know the issue is due to an invalid number and to fix it you'll need to wrap the number to char.
But when filtering VARCHAR2 it is stated that Oracle will convert the data of the column being filtered based on the value given to it. (see: https://stackoverflow.com/a/10422418/5337433)
Now that this is explained for some reason, the error is inconsistent. As an example I have this query:
In this example filter1 is varchar2
select *
from table
where filter1 = 12345
and filter2 = ''
and filter3 = '';
When this statement run there were no issues, but when you run it like this:
select *
from table
where filter1 = 12345
and filter2 = '';
it errors out to ERROR-01722, im not sure why it is acting this way, and how to fix it.
When you compare a varchar column to a number, Oracle will try to convert the column's content to a number, not the other way round (because 123 could be stored as '0123' or '00123')
In general you should always use constant values that match the data type of the column you compare them with. So it should be:
where filter1 = '12345'
However if you are storing numbers in that column, you should not define it as varchar - it should be converted to a proper number column.
The reason the error doesn't show up "consistently" is that you seem to have some values that can be converted to a number and some can't. It depends on other conditions in the query if the those values are included or not.
Additionally: empty strings are converted to NULL in Oracle. So the condition filter2 = '' will never be true. You will have to use filter2 is null if you want to check for an "empty" column.

SQL query problem in WHERE clause, this returns all that start with

I've written the following SQL query to return all sites having "id" equal to 2.
SELECT * FROM `sites` WHERE id = '2'
And it works well. The problem is that even if I add some characters after "2" like this :
SELECT * FROM `sites` WHERE id = '2etyupp-7852-trG78'
It returns the same results as above.
How to avoid this ? that's to say return none on the second query ?
Thanks
The reason is that you are mixing types:
where id = '2'
------^ number
-----------^ string
What is a SQL engine supposed to do? Well, the standard approach is to convert the string to a number. So this is run as:
where id = 2
What happens when the string is not a number? In most databases, you get a type conversion error. However, MySQL does implicit conversion, converting the leading digits to a number. Hence, your second string just comes 2.
From this, I hope you learn not to mix data types. Compare numbers to numbers. Compare strings to strings.

Sql Query Builder Converting Data to char

I've had this problem while trying to read a variable which gets a date, for example "14.07.2018" and compares it to a date column. How can I solve this? I want to show a schedule from a festival in a specific day(Orar.Data is the column which indicates the date).
The idea is to always have both sides of the comparison as equals, i.e. comparing oranges to oranges, so to speak.
To that end, it is a good practice to modify both sides, LHS and RHS, to the same format.
SQL provides the "convert" function that does this. More documentation here.
Your query should be:
SELECT
* --insert your columns
FROM
Scena
INNER JOIN
Orar ON Scena.Id_scena = Orar.Id_scena
INNER JOIN
Artist ON Orar.Id_artist = Artist.Id_artist
WHERE
Scena.Titlu_scena = #var
AND
convert(varchar, Orar.Data, 9) = convert(varchar, #var1, 9)

DB2 SQL - How can I display nothing instead of a hyphen when the result of my case statement is NULL?

All,
I'm writing a query that includes a CASE statement which compares two datetime fields. If Date B is > Date A, then I'd like the query to display Date B. However, if Date B is not > Date A, then the user who will be getting the report created by the query wants the column to be blank (in other words, not contain the word 'NULL', not contain a hyphen, not contain a low values date). I've been researching this today but have not come up with a viable solution so thought I'd ask here. This is what I have currently:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN B.DTE_LNP_LAST
ELSE ?
END AS "DATE OF DISCONNECT"
If I put NULL where the ? is, then I get a hyphen (-) in my query result. If I omit the Else statement, I also get a hyphen in the query result. ' ' doesn't work at all. Does anyone have any thoughts?
Typically the way nulls are displayed is controlled by the client software used to display query results. If you insist on doing that in SQL, you will need to convert the date to a character string:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN VARCHAR_FORMAT(B.DTE_LNP_LAST)
ELSE ''
END AS "DATE OF DISCONNECT"
Replace VARCHAR_FORMAT() with the formatting function available in your DB2 version on your platform, if necessary.
You can use the coalesce function
Coalesce (column, 'text')
If the first value is null, it will be replaced by the second one.