0

I'm trying to translate 2 types of data using only postgres SQL I've got a column "type" that may contains the kind of data.

type is a string column and may have "ACTUAL" or "OLD" values

+-type-+
+ACTUAL+
+OLD   +
+------+

when I show the list with a lot of other joins I would like to show only "A" or "O" values I couldnt find other way to do this than:

SELECT replace(replace(mytable.type, 'ACTUAL', 'A'),'OLD', 'O');

With that I can replace the text the way I need, but I was looking for some more function using an array as parameter.

something like a cross-reference simple function:

translate(['ACTUAL','OLD'], ['A','O']) 

Does anyone know a way to do this that doesn't use SQL views and neither needs another table like joining the results of this value with other table?

Thanks in advance, Andre

0

1 Answer 1

3

I would use something like CASE...

SELECT
  (CASE type 
   WHEN 'ACTUAL' THEN 'A' 
   WHEN 'OTHER' THEN 'O' 
   ELSE '?' END) 
FROM 

Using this method, you can return whatever you want based on whatever criteria you want, not just sub-stringing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.