Need to get a value till precision of 15 digit after decimal in DB2 Query - sql

One of DB2 table column value is appearing as 0.3901369869709015 and we need to compare this value against my expected value as 0.390136986970901. I tried to get the value from DB2 by using Decimal/Dec method. With that the value is getting round off and appearing as 0.390136986970902. Could you please help me to correct the below query that i am using to extract data from my DB2 table.
SELECT DECIMAL(UV_FIELDSCOREMAP,15,15) AS UV_FIELDSCOREMAP From cvsinst.uv_occ WHERE CASEID = '20170720'

Use TRUNCATE(UV_FIELDSCOREMAP, 15) instead.

Related

Count of Character mismatch between two values in SQL Server stored procedure

How to find the count of character mismatch between two values in a SQL Server stored procedure? It's not about the length difference.
For example, there are two values,
Reference value ='Visual'
Test value ='Visvolc'
Mismatch = 3 chars (4th, 5th and 7th position)
There is 3 character mismatch based on the position. please help.
Sounds like what you are looking for is “edit distance”, a number of add/remove/replace operation to convert one string into another. Check this post: Levenshtein distance in T-SQL

How to get maximum size used by a string in Hive?

I want to know the maximum length a particular string column is taking.
I tried taking the approached mentioned here :
how to get the max size used by a field in table but did not work in Hive
but that did not work in Hive.
In that example they use len, use length instead:
select max(length(mycolumn)) from mytable;
This works fine in hive QL.
multiple ways to check length of column in where clause as well:
select max(length(column_name)),min(length(column_name)) from table_name where length(column_name)<15
Here, checked column length with max and min values. Also use where clause if checking column length lesser than 15

SQL Query - Greater Than with Text Data Type

I've searched around and couldn't find an answer anywhere.
I'm querying a database that has stored numbers as a VARCHAR2 data type. I'm trying to find numbers that are greater than 1450000 (where BI_SO_NBR > '1450000'), but this doesn't bring back the results I'm expecting.
I'm assuming it's because the value is stored as text and I don't know any way to get around it.
Is there some way to convert the field to a number in my query or some other trick that would work?Hopefully this makes sense.
I'm fairly new to SQL.
Thanks in advance.
If the number is too long to be converted correctly to a number, and it is always an integer with no left padding of zeroes, then you can also do:
where length(BI_SO_NBR) > length('1450000') or
(length(BI_SO_NBR) = length('1450000') and
BI_SO_NBR > '1450000'
)
You can try to use like this:
where to_number(BI_SO_NBR) > 1450000
Assuming you are using Oracle database. Also check To_Number function
EDIT:-
You can try this(after OP commented that it worked):
where COALESCE(TO_NUMBER(REGEXP_SUBSTR(BI_SO_NBR, '^\d+(\.\d+)?')), 0) > 1450000
If you are talking about Oracle, then:
where to_number(bi_so_nbr) > 1450000
However, there are 2 issues with this:
1. if there is any value in bi_so_nbr that cannot be converted to a number, this can result in an error
2. the query will not use an index on bi_so_nbr, if there is one. You could solve this by creating a function based index, but converting the varchar2 to number would be a better solution.

Truncation Warning when data should fit

We are updating column A with the exact value from column B. The length of column B is 255 and column A is 4. The data in column B has been verified by LEN(REVERSE(colB)) to be only 4. When we try to update the error message says:
'String or binary data would be truncated.'
here is the query:
update table
set columnA=columnB
where Column B in ('ABC','ABCD','AB')
we have also verified that this works:
update table
set columnA=left(columnB,4)
where Column B in ('ABC','ABCD','AB')
any guesses as to what could be wrong?
thanks
It could be that you are using a char (not varchar) data type for column B, in which case your database engine may consider the width of the column, regardless of the width of the data. A trim function may then get rid of the error.
SQL doesn't check the actual length of the string - it just detects that the string value COULD overflow the new container and gives you an error message. Try this:
update table set ColumnA=(select LEFT(ColumnB, 4) from table where ColumnB in ('ABC', 'ABCD', 'AB'));
This selects only the first four characters of the data from columnB.
This query works And I am ok with it.
update table
set columnA=columnB
where Column B in ('ABC','ABCD','AB') and len(columnb)<=4
I am still curious as to why

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