Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concat Issue

Status
Not open for further replies.

PSQLNew

MIS
Sep 23, 2020
1
0
0
US
I am trying to pad a calculated field with leading zeros, so if its original value is '1' then the new value would be '001'. Or if its original value was '23' the new value is '023'. Or if its original value is '124' then new value is the same as original value. I am using below code but it only shows zeros in the output not the field values and if I pad these zeros in the last it returns correct output.
Can someone help me? I am new with pervasive sql and using PSQL v12. Thanks in advance :)

My query: (wholesale_1 is double datatype & size_3 is char datatype)(field calculation is wholesale_1/size_3)

select wholesale_1, size_3
,replicate('0',5) + cast(round((wholesale_1/(case when left((case when size_3 = ' ' then cast('1' as int) when locate('*', size_3) > 0 then cast(left(size_3, locate('*', size_3)-1) as int) else
cast(size_3 as int) end),1) = 0 then 1
else (case when size_3 = ' ' then cast('1' as int)
when locate('*', size_3) > 0 then cast(left(size_3, locate('*', size_3)-1) as int)
else cast(size_3 as int) end) end))*100,0) as VARCHAR(10)) as price
from inventry;
 
Without spending a lot of time on it, it looks like something is wrong with the CASE/ CAST statement where you are trying to get the integer value of SIZE_3. How big the size_3 char field? Does the CAST part of price return the correct values? What values are valid for the size_3 field? Have you tried simplifying the conversion of that size_3 calculation?
Simplifying the query does produce the desired result:

Code:
create table inventry_tt
(
wholesale_1 integer,
size_3 char);
insert into inventry_tt values (1, '1');
insert into inventry_tt values (5, '2');


select wholesale_1, size_3
,replicate('0',5) + cast(round((wholesale_1/cast(size_3 as integer))*100,0) as VARCHAR(10))
from inventry_tt;
Code:
wholesale_1   size_3   EXPR_1
          1   1        00000100                                                                                                                                                                                                
          5   2        00000250



Mirtheil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top