Click here to Skip to main content
16,017,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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'
Posted
Updated 17-Jul-16 17:47pm
Comments
[no name] 14-Jul-16 7:29am    
At least for MSSQL you can use "REPLACE". E.g. SELECT REPLACE(Col, '_', ' ') FROM....

1 solution

is this what you're trying to do ?

SELECT REPLACE(i.pkey, '_', ' ') AS Expr2, REPLACE(v.pkey, '_', ' ') AS Expr1
FROM t_info AS i INNER JOIN
t_value AS v ON i.infoID = v.infoID ;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900