tsql - SQL Select Columns.. IF NULL then Select other Columns -


i have view this:

id| key | product | item | block | source | title | text | type | h1 | h2 | h3 | ------------------------------------------------------------------------------- 1 | 456 | abcd    | def  |  1    |  tp    | qwert | yuip | tgr  | a1 | a2 | a3 | 2 | 567 | fhrh    | klo  |  1    |  gt    | trewq | itgf | trp  | a1 | a2 | a3 | 3 | 891 | ufheu   | yut  |  2    |  fr    | werty | mnbv | uip  |null|null|null| 

i want export of these columns existing, empty table. want select first 6 columns , select other columns hierarchy going right left.

if h1, h2 , h3 not null, should come in output , title, text , type should null (even though contain values).

if h1, h2 , h3 null, want thet title, text , type in output.

it should this:

id| key | product | item | block | source | title | text | type | h1 | h2 | h3 | ------------------------------------------------------------------------------- 1 | 456 | abcd    | def  |  1    |  tp    | null  | null | null | a1 | a2 | a3 | 2 | 567 | fhrh    | klo  |  1    |  gt    | null  | null | null | a1 | a2 | a3 | 3 | 891 | ufheu   | yut  |  2    |  fr    | werty | mnbv | uip  |null|null|null| 

can me this? appreciated!

ok, i've wrapped column names in [square brackets] because you're using reserved names (key, text, type) , consistency, it's worth breaking habit possible.

if criteria 3 columns (h1, h2, h3) need null you'll want this;

select [id]     ,[key]     ,[product]     ,[item]     ,[block]     ,[source]     ,case          when h1 null             , h2 null             , h3 null             [title]         else null         end [title]     ,case          when h1 null             , h2 null             , h3 null             [text]         else null         end [text]     ,case          when h1 null             , h2 null             , h3 null             [type]         else null         end [type]     ,h1     ,h2     ,h3 datatable 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -