问题描述
我有一个包含abc_def_ghi的行的表和另一个包含123_456_789的行的表。
create table t_info(infoId varchar(255),PKey varchar( 255))
插入t_info值('ZC','abc_def_ghi')
create table t_value(infoId varchar(255),PKey varchar(255))
插入t_value值('ZC','123_456_789')
我需要一个选择查询,显示输出如下
abc def ghi
123 456 789
请帮助我
我尝试了什么:
我尝试用'_'分割值。但我坚持制作专栏名称
选择
CHARINDEX('_',PKey)> 0
然后
SUBSTRING(PKey,1,CHARINDEX('_',PKey)-1)
别用PKey结束名字,
CASE WHEN CHARINDEX('_',PKey)> 0
那么SUBSTRING(PKey,CHARINDEX('_',PKey)+ 1,len(PKey))
ELSE NULL END作为姓氏
来自t_value的
其中infoId ='ZC'
I Have a table with a row containing abc_def_ghi and another table with a row containing 123_456_789.
create table t_info (infoId varchar(255),PKey varchar(255))
insert into t_info values ('ZC','abc_def_ghi')
create table t_value (infoId varchar(255),PKey varchar(255))
insert into t_value values ('ZC','123_456_789')
I need a single select query which displays the output as below
abc def ghi
123 456 789
Kindly help me
What I have tried:
I tried for splitting the values with '_'. But i'm stuck in the making the column name
select
case when CHARINDEX('_',PKey)>0
then
SUBSTRING(PKey,1,CHARINDEX('_',PKey)-1)
else PKey end firstname,
CASE WHEN CHARINDEX('_',PKey)>0
THEN SUBSTRING(PKey,CHARINDEX('_',PKey)+1,len(PKey))
ELSE NULL END as lastname
from t_value where infoId='ZC'
推荐答案
这篇关于表的行应该是列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!