compare absolute value with double value present in table using PostgreSQL - sql

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.

Related

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.

how to calculate number with date in sql oracle cmd

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.

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.

How to remove the first character from the join statement in where clause of the SQL query

I want to remove the first character returning from the cvdl.C statement in the below SQL query and that value should be match up with the ccp.B value.
For an example if the real value return by the cvdl.C statement is 4500, I want to remove 4 and take only the 500 part to match with the value in the ccp.B value. Also I need to pass a input parameter value for the query.
How can I modify below SQL query to achieve this objective?
SELECT ccp.A
FROM ccp, cvdl
WHERE cvdl.J = 'Example' and ccp.B = cvdl.C
you should use the ansi-syntax to join
select ccp.A,
from ccp
inner join cvdl on ccp.B = substr(cvdl.C, 2)
where cvdl.J = 'Example'

Problem with MySQL Select query with "IN" condition

I found a weird problem with MySQL select statement having "IN" in where clause:
I am trying this query:
SELECT ads.*
FROM advertisement_urls ads
WHERE ad_pool_id = 5
AND status = 1
AND ads.id = 23
AND 3 NOT IN (hide_from_publishers)
ORDER BY rank desc
In above SQL hide_from_publishers is a column of advertisement_urls table, with values as comma separated integers, e.g. 4,2 or 2,7,3 etc.
As a result, if hide_from_publishers contains same above two values, it should return only record for "4,2" but it returns both records
Now, if I change the value of hide_for_columns for second set to 3,2,7 and run the query again, it will return single record which is correct output.
Instead of hide_from_publishers if I use direct values there, i.e. (2,7,3) it does recognize and returns single record.
Any thoughts about this strange problem or am I doing something wrong?
There is a difference between the tuple (1, 2, 3) and the string "1, 2, 3". The former is three values, the latter is a single string value that just happens to look like three values to human eyes. As far as the DBMS is concerned, it's still a single value.
If you want more than one value associated with a record, you shouldn't be storing it as a comma-separated value within a single field, you should store it in another table and join it. That way the data remains structured and you can use it as part of a query.
You need to treat the comma-delimited hide_from_publishers column as a string. You can use the LOCATE function to determine if your value exists in the string.
Note that I've added leading and trailing commas to both strings so that a search for "3" doesn't accidentally match "13".
select ads.*
from advertisement_urls ads
where ad_pool_id = 5
and status = 1
and ads.id = 23
and locate(',3,', ','+hide_from_publishers+',') = 0
order by rank desc
You need to split the string of values into separate values. See this SO question...
Can Mysql Split a column?
As well as the supplied example...
http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
Here is another SO question:
MySQL query finding values in a comma separated string
And the suggested solution:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set