I want to convert the result of a division to a character string in DB2.
My result when I do '0.24'||'%' is '.24%'
I don't understand how to get exactly the output '0.24%'
hi try Something like this
case when myval<1 and myval>0 then '0' || cast(myval as char(15))
when myval>-1 and myval<0 then '-0' || cast(abs(myval) as char(15))
else cast(myval as char(15)) end myvaltochar
try this
to_char( yourzone
repeat('0', length(trim(int(abs(yourzone))))) || '.'||
repeat('0', length(trim(
INTEGER(SUBSTR(CHAR(-123.36),LOCATE(',',CHAR(yourzone))+1)))))
)
SELECT VARCHAR_FORMAT(24.0/100,'0.00')||'%' ...
If you are dividing integer types, you'll have to cast them as DOUBLE() otherwise the result will be 0
Related
I have 2 varchar(64) values that are decimals in this case (say COLUMN1 and COLUMN2, both varchars, both decimal numbers(money)). I need to create a where clause where I say this:
COLUMN1 < COLUMN2
I believe I have to convert these 2 varchar columns to a different data types to compare them like that, but I'm not sure how to go about that. I tried a straight forward CAST:
CAST(COLUMN1 AS DECIMAL(9,2)) < CAST(COLUMN2 AS DECIMAL(9,2))
But I had to know that would be too easy. Any help is appreciated. Thanks!
You can create a UDF like this to check which values can't be cast to DECIMAL
CREATE OR REPLACE FUNCTION IS_DECIMAL(i VARCHAR(64)) RETURNS INTEGER
CONTAINS SQL
--ALLOW PARALLEL -- can use this on Db2 11.5 or above
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN
DECLARE NOT_VALID CONDITION FOR SQLSTATE '22018';
DECLARE EXIT HANDLER FOR NOT_VALID RETURN 0;
RETURN CASE WHEN CAST(i AS DECIMAL(31,8)) IS NOT NULL THEN 1 END;
END
For example
CREATE TABLE S ( C VARCHAR(32) );
INSERT INTO S VALUES ( ' 123.45 '),('-00.12'),('£546'),('12,456.88');
SELECT C FROM S WHERE IS_DECIMAL(c) = 0;
would return
C
---------
£546
12,456.88
It really is that easy...this works fine...
select cast('10.15' as decimal(9,2)) - 1
from sysibm.sysdummy1;
You've got something besides a valid numerical character in your data..
And it's something besides leading or trailing whitespace...
Try the following...
select *
from table
where translate(column1, ' ','0123456789.')
<> ' '
or translate(column2, ' ','0123456789.')
<> ' '
That will show you the rows with alpha characters...
If the above does't return anything, then you've probably got a string with double decimal points or something...
You could use a regex to find those.
There is a built-in ability to do this without UDFs.
The xmlcast function below does "safe" casting between (var)char and decfloat (you may use as double or as decimal(X, Y) instead, if you want). It returns NULL if it's impossible to cast.
You may use such an expression twice in the WHERE clause.
SELECT
S
, xmlcast(xmlquery('if ($v castable as xs:decimal) then xs:decimal($v) else ()' passing S as "v") as decfloat) D
FROM (VALUES ( ' 123.45 '),('-00.12'),('£546'),('12,456.88')) T (S);
|S |D |
|---------|------------------------------------------|
| 123.45 |123.45 |
|-00.12 |-0.12 |
|£546 | |
|12,456.88| |
I have a float8 that contains a number of seconds ie 65.455. I was trying to format a column in a view so that it would read as 1:05.455.
Using postgres command like this:
TO_CHAR((user_data.totaltime || ' second')::interval, 'MI:SS')
I can format it as 1:05 but i loose the precision i need.
Anyone know of a way to achieve what i need ? It doesn't look like interval allows the formatting of milliseconds.
Try justify_interval:
SELECT justify_interval(
CAST(65.455::text || ' seconds' AS interval)
);
Here is a rather brute force you may do this, using the base string functions:
SELECT
FLOOR(65.455 / 60)::text || ':' || LPAD(FLOOR(65.455 % 60)::text, 2, '0') ||
TRIM(LEADING '0' FROM (65.455 % 1)::text);
This outputs:
1:05.455
Demo
I have a simple CASE statement where in my ELSE block numeric column I want to display as '-'.
But it gives me an error
Arithmetic overflow error converting varchar to data type numeric.
How would I do that?
I want it like this:
SELECT
CASE WHEN ChargeName = 'Premium' THEN CompanyCommissionPercentage ELSE '-' END AS CompanyCommissionPercentage
,CASE WHEN ChargeName = 'Premium' THEN RemitterCommissionPercentage ELSE '-' END AS RemitterCommissionPercentage
,CASE WHEN ChargeName = 'Premium' THEN RemitterCommission ELSE '-'END AS RemitterCommission
,CASE WHEN ChargeName = 'Premium' THEN GrossCommission ELSE '-'END AS GrossCommission
FROM #tmpAccountsPayable
`SELECT
CASE WHEN ChargeName = 'Premium' THEN CAST(CompanyCommissionPercentage as varchar(10)) ELSE CAST('-' as varchar(10)) END AS CompanyCommissionPercentage
FROM #tmpAccountsPayable`
Here's a picture of where in excel you can set formatting to a -
You may be better off passing in a zero instead of the dash and allowing excel formatting to make the 0 a - (assuming of course the output is going into excel)
Notice below in E20 the 0 value is converted to a - when accounting format (comma format) is used.
Also notice how a regular dash is left aligned while the accounting - is indented.
I have a set of decimal values, but I need to remove the values after the decimal if they are zero.
17.00
23.50
100.00
512.79
become
17
23.50
100
512.79
Currently, I convert to a string and replace out the trailing .00 - Is there a better method?
REPLACE(CAST(amount as varchar(15)), '.00', '')
This sounds like it is purely a data presentation problem. As such you should let the receiving application or reporting software take care of the formatting.
You could try converting the .00s to datatype int. That would truncate the decimals. However, as all the values appear in one column they will have to have the same type. Making everything an int would ruin your rows with actual decimal places.
As a SQL solution to a presentation problem, I think what you have is OK.
I would advice you to compare the raw decimal value with itself floored. Example code:
declare
#Val1 decimal(9,2) = 17.00,
#Val2 decimal(9,2) = 23.50;
select
case when FLOOR ( #Val1 ) = #Val1
then cast( cast(#Val1 as int) as varchar)
else cast(#Val1 as varchar) end,
case when FLOOR ( #Val2 ) = #Val2
then cast( cast(#Val2 as int) as varchar)
else cast(#Val2 as varchar) end
-------------
17 | 23.50
I was trying to make a function to work in db2:
CREATE FUNCTION TO_DATE8(DATE_STRING numeric(8,0))
RETURNS DATE
LANGUAGE SQL
IF DATE_STRING > 0 THEN
// ERROR ->
RETURN DATE ( TO_DATE ( SUBSTR ( DATE_STRING , 1 , 8 ) , 'YYYYMMDD' ) )
ELSE
RETURN DATE ( TO_DATE ( '00000000' , 'YYYYMMDD' ) )
END IF
END
ERROR: DATE IS NOT VALID
What to do?
The form of the procedure required seems to be like this (at least on the iSeries version):
CREATE FUNCTION TO_DATE8(DATE_STRING numeric(8,0))
RETURNS DATE
LANGUAGE SQL
BEGIN
RETURN(CASE WHEN DATE_STRING > 0 THEN DATE(SUBSTR(DATE_STRING, 1, 4) || '-' ||
SUBSTR(DATE_STRING, 5, 2) || '-' ||
SUBSTR(DATE_STRING, 7, 2))
ELSE DATE('0001-01-01')
END);
END
However:
Your procedure is misnamed (reading from a date-8, not to it).
Your DATE_STRING is not a string (or even a char), it's numeric. Please rename it to something that does not include the datatype (dateToConvert works)
You seem to want to return something that is not a valid date (all 0s). I'm returning *loval here, although it's possible it should actually be null.
I didn't put in enough checks for a valid date - this will blow up really easily.
If at all possible, the database should be changed to contain actual dates, not a numeric value. Disk is (relative to programmer/architect headaches) cheap.
You may also find a calendar file helpful, if the 8-digit numeric was one of the included columns.
For the benifit of others, this can be done in one line rather than a function:
CASE WHEN MYDATE = 0 THEN NULL ELSE DATE(INSERT(INSERT(LEFT(CHAR(MYDATE),8),5,0,'-'),8,0,'-')) END
MYDATE was 8 packed in my case.