Only select columns with values

0

I have a table with a sequence of fields with almost the same name, changing only the final character, and I need to make a query that returns only the columns with some value, I currently do the following:

select pag, descricao,cod_vcto1,cod_vcto2,cod_vcto3,cod_vcto4,cod_vcto5 from tabela_exemplo where pag =2;

pag descricao   cod_vcto1   cod_vcto2   cod_vcto3   cod_vcto4   cod_vcto5
2   28 DIAS         28          31      null       null        null       

I would like to return, only 28 DAYS 28/31. But I have 15 columns that start with cod_vcto, which vary from cod_vcto1 to cod_vcto15, is there any way to only select columns that are not null?

    
asked by anonymous 20.10.2014 / 19:37

2 answers

1

As suggested by gmsantos the clean solution is normalize, but a dirty solution (and a lot) is simply concatenate (converting the string) the columns will result in only columns with value, there would be no header to indicate "which."

But I recommend a normalization unless there is a good reason for the current model.

    
20.10.2014 / 23:50
0

I do not know of any way to do a select that returns only the columns that are not null, but if you are using some programming language to be doing the application, you can create an object class and make a list with the objects, and thus taking only the desired columns!

I hope to have helped!

    
20.10.2014 / 19:47