Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have following values in my table "sharefile"
SQL
FilePath               Users   CompanyId

1043_CAB1\111.txt	   2083	  1043
1043_CabFW\111.txt	   2083	  1043
1040_Admin\Admin.docx  2089	  1040


I need to select all rows same like above,but need to remove companyid with "_" in select query
my need is
SQL
FilePath           Users   CompanyId

CAB1\111.txt	   2083	  1043
CabFW\111.txt	   2083	  1043
Admin\Admin.docx   2089	  1040

I am try with replace function in select query,i can remove only Companyid form filepath,still need to remove "_" underscore also

This is query i tried

SQL
SELECT replace (FilePath,CompanyId ,'') as FilePath , Users, CompanyId FROM ShareFile

and result is
SQL
_CAB1\111.txt	2083	1043
_CabFW\111.txt	2083	1043
_Admin\Admin.docx	2089	1040


I am try to add "_" in replace function like this,but cant add,bcz of comapnyid datatype is int.Only in string datatype can add "_"
SQL
SELECT replace (FilePath,CompanyId & N'_' ,'') as FilePath , Users, CompanyId FROM ShareFile


Or if possible in sql select query can remove letters upto first "_" using substring function
Pls reply me how to achieve this

Regards
Araindb
Posted
Updated 16-Sep-15 20:18pm
v2

2 possible solution:
-Use a nested replace and in the second try to remove underscore only (but I think that there is a character matching issue, caused by encoding)
-Or use simply a substring calculating len(companyId)+1
 
Share this answer
 
Comments
Aravindba 17-Sep-15 3:08am    
Thank u for ur reply,i solve by myslef ,first i convert companyid as string and i add underscore inside replace function,pls see my solution
Hi, i solve like this
SQL
SELECT  Replace (FilePath,CONVERT(varchar(10), CompanyId ) + N'_','') as FilePath, Users, CompanyId from ShareFile


After converting companyid to string,then i can append "_" and use in replace function.


Regards
Aravind
 
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