Masking SQL Select Return

2

I'm using the following SELECT to return the IDs of a table.

SELECT id, duracao_sessao FROM usuario
  • For ID 1 I want the text "Object" to be displayed
  • For ID 2 I want the text "Class" to be displayed
  • For ID 3, I want the text "Structure" to be displayed

Is there any way to mask the return on a SELECT statement in this way?

    
asked by anonymous 02.12.2017 / 19:06

2 answers

3

According to the question, only a few IDs should be masked. If this is the purpose, you can use the expression CASE. For example, this way:

SELECT CASE
          WHEN ID = 1 THEN 'Objecto'
          WHEN ID = 2 THEN 'Classe'
          WHEN ID = 3 THEN 'Estrutura'
          ELSE CONVERT(VARCHAR, ID)
       END
  FROM usuario

The previous statement will return "Object," "Class," or "Structure" if the ID is 1, 2, or 3 respectively. For the remaining IDs, it will return the number itself. If you want to apply the exception to other IDs, just include them in the list.

    
02.12.2017 / 20:07
1

Consider which syntax may vary according to the DBMS. Here are some examples:

SELECT 'Texto' + cast(id as varchar(250)) FROM tabela (MS SQL SERVER)
SELECT 'Texto' || id FROM tabela (FIREBIRD)
SELECT CONCAT('Texto', id) AS campo from tabela (MYSQL)
    
02.12.2017 / 19:29