Skip to content
Advertisement

how to copy values from a column to a non-existent column in postgresql

i have a table like this:

enter image description here

and i want to copy the values from the first column to the non existent column that is ‘qty’ how do i do that if they have different data type?

what i originally wanted is when the 1st column’s value is 0, then the column qty should be blank or empty, not [null], i acheived that by my code like this:

SELECT round(od_qty),
CASE when round(od_qty) = 0 Then ''
END qty,
FROM vodetl 

now my problem is what about the other values that are not 0, how do i copy those values to the column qty? i tried this:

SELECT round(od_qty),
CASE when round(od_qty) = 0 Then ''
ELSE round(od_qty)
END qty,
FROM vodetl 

but because of the numeric data type it showed

ERROR:  invalid input syntax for type numeric: ""
LINE 2: CASE when od_item = 'C' and round(od_price,2) = 0.00 Then ''

how do i do it without changing the data type of the first column??

Advertisement

Answer

All branches of a CASE expression must have the same type. So, if you want to display empty string as one of the values, then the other numerical value should also be cast to text. Consider:

SELECT ROUND(od_qty),
       CASE WHEN ROUND(od_qty) = 0 THEN '' ELSE ROUND(od_qty)::text END qty,
FROM vodetl;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement