1

I query my database and get the following row:

**NAME | DESCRIPTION | MOBILE | EMAIL | CENTER |**

George | Nice guy    | 644213568 | geo@gmail.com | NASA |

I'd like to convert it into:

LABEL | VALUE

Name | George

DESCRIPTION | Nice guy

MOBILE | 644213568

EMAIL | geo@gmail.com

CENTER | NASA

Thank you in advance!!!!!

Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
mantc_sdr
  • 451
  • 3
  • 17

1 Answers1

1

You want apply :

select tt.LABEL, tt.VALUE
from table t cross apply
     ( values ('DESCRIPTION', DESCRIPTION), ('MOBILE', MOBILE), ('EMAIL', EMAIL), ('CENTER', CENTER) 
     ) tt(LABEL, VALUE)
 where t.USER_ID = 23;
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
  • thanks, it works! but what about if I want to add a where clause? I mean, table **t** has many rows, I just want one row, as if my row was given from SELECT ... FROM T WHERE USER_ID = 23,,, thanks! – mantc_sdr Jan 29 '19 at 10:35
  • 1
    @mantc_sdr. . . You can use `WHERE` clause after `CROSS APPLY`. – Yogesh Sharma Jan 29 '19 at 10:36
  • 1
    You can use something like select tt.LABEL, tt.VALUE from [table] t cross apply ( values ('DESCRIPTION', DESCRIPTION) ) tt(LABEL, VALUE) where t.id=1; – A.M. Patel Jan 29 '19 at 10:37