Remove value from string where it match another column - sql

I need to remove a file path from the beginning of a column. I have table with 3 columns one has just the path I want to remove.
So I have a table like:
Is there any way in SQL Server 2008 that I can remove the text where it matches the path column?
Thanks

Try this
UPDATE Table x
SET x.dataconv = SUBSTRING(x.dataconv,LEN(x.path),LEN(x.dataconv))
WHERE SUBSTRING(x.dataconv,1,LEN(x.path)) == x.path

You could try using replace?
DECLARE #table TABLE (id INT, dataconv VARCHAR(500), [path] VARCHAR(500));
INSERT INTO #table SELECT 1, 'C:\windows test_data_1', 'C:\windows';
INSERT INTO #table SELECT 2, 'C:\windows\test.ini test_data_2', 'C:\windows\test.ini';
INSERT INTO #table SELECT 3, 'C:\word docs\doc.ini test_data_3', 'C:\word docs\doc.ini';
INSERT INTO #table SELECT 4, '/P/TestUser/dfg002/SinglePageCV''s/JoeBloggs.doc33216root;tuser002_admin#IEroot;D‌​omain:Users#IE431104 1330820929 1003750078 1055597658', '/P/TestUser/dfg002/SinglePageCV''s/JoeBloggs.doc';
SELECT id, LTRIM(REPLACE(dataconv, [path], '')) FROM #table;
Returns:
1 test_data_1
2 test_data_2
3 test_data_3
4 33216root;tuser002_admin#IEroot;D??omain:Users#IE431104 1330820929 1003750078 1055597658

Related

SQL server, Find all columns that are contained in a string

Suppose I have a table:
Id int,
Name varchar(10)
with values:
(1, Nick), (2, Mike), (3, Eric)
I want to return all the names that are contained in a string. For example "Nick, Mick" would return Nick and Mike.
I've tried with LIKE but it works the other way around returning the values that contain a string.
I was hoping for somthing like this, but that it actually works.
SELECT * FROM table
WHERE Name.isContained("Nick, Mike");
Result:
1, Nick and 2, Mike
Try this:
SELECT Name
FROM mytable
WHERE PATINDEX('%'+Name+'%', 'Nick, Mike') > 0
Demo here
Try this:
select name from tbl_sound where SOUNDEX(`name`) like '%00%'
Demo. Finds exact names match
create table demo (
Id int,
Name varchar(10));
insert demo
values
(1, 'Nick'), (2, 'Mike'), (3, 'Eric');
declare #prm varchar(200) = 'Nick,Mike,Ann';
select *
from demo
where ','+ #prm +',' like '%,'+Name+',%'
;
Check This.
declare #tmp nvarchar(250)
SET #tmp = ''
select #tmp = #tmp + concat(ID,' , ',name,' and ')
from table
where Name in ('Nick','Mike')
select SUBSTRING(#tmp, -2, LEN(#tmp))
OutPut :

how to pick string basing on ID wise

i have a table where i have values like
CREATE TABLE VALUES (ID INT,VAL VARCHAR(40))
INSERT INTO VALUES (ID,VAL)VALUES (1,'Chemicals(Sulphur4123)'),(2,'Chemicals(Sulphur4123)/RAW')
How can i get result set of :
ID Val
1 Sulphur4123
2 Sulphur4123/RAW
so far i have tried but with no luck
my code
select Substring(val,0,CHARINDEX('/',val))+ right(val,4) from values
declare #t table (ID int,value varchar(40))
insert into #t (ID,value)values (1,'Chemicals(Sulphur4123)'),(2,'Chemicals(Sulphur4123)/RAW')
select SUBSTRING(value,
CHARINDEX('(',value) + 1,
CHARINDEX(')',value) - CHARINDEX('(',value) - 1)+
+reverse(SUBSTRING(reverse(value),1,CHARINDEX('/',reverse(value))))
AS RESULT from #t
You didn't specify your database .User custom function is what you find If your database support it.
CREATE ALIAS IF NOT EXISTS f AS $$ String f(String str) {
return str.substring(str.indexOf("/"));
}$$;
SELECT ID,F(VALUE ) FROM VALUES

SQL Substring on varying column

I have a database table with a column METADATA. This METADATA may or may not contain a string. Here's the sample string:
StudentID:1234,StudentName:TestName,StudentNickName:TestNName,StudentLevel:5
Now, I want to extract the StudentNickName:TestName if it exists. Please note of the following constraint:
METADATA column doesn't always contain a value
METADATA column can contain a value without the StudentNickName clause
The StudentNickName: is fixed, while the TestNName varies per row.
For mssql 2005+
declare #t table(metadata varchar(200))
insert #t values('StudentID:1234,StudentName:TestName,StudentNickName:TestNName,StudentLevel:5')
insert #t values('')
insert #t values('StudentID:1234,StudentName:Thomas,StudentNickName:Tom,StudentLevel:3')
select left(b.a, patindex('%_,%', b.a)) StudentNickName
from #t t cross apply
(select right(metadata, patindex('%_:emaNkciNtnedutS%'
, reverse('X'+ metadata)))+',' a) b
Result:
StudentNickName
---------------
TestNName
Tom
Works in Sql Server
DECLARE #test TABLE(metadata VARCHAR(200))
INSERT #test VALUES('StudentID:1234,StudentName:TestName,StudentNickName:TestNName,StudentLevel:5')
INSERT #test VALUES('StudentID:1235,StudentName:TestName1,StudentNickName:TestNName1,StudentLevel:6')
INSERT #test VALUES('StudentID:1236,StudentName:TestName2,StudentNickName:TestNName2,StudentLevel:2')
INSERT #test VALUES('')
SELECT split.s.value('.','VARCHAR(100)') as colname FROM
(
SELECT CAST('<s>' + REPLACE(metadata,',','</s><s>') + '</s>' AS XML) col FROM #test
) AS t CROSS APPLY col.nodes('/s') AS split(s)
WHERE split.s.value('.','VARCHAR(100)') LIKE '%StudentNickName%'

SQL scalar variable value between and

'Names' in table 'Data'
"type12pen105A"
"type12pen110A"
"type12pen121B"
Declare #n int;
select Names From Data
where Names ='type12pen'+cast(#n between 100 and 110 as varchar)+'A'
My Required out put is
"type12pen105A"
"type12pen110A"
SELECT Names
FROM Data
WHERE Names LIKE 'type12pen%'
AND CAST(SUBSTRING(Names,10,3) AS INT) BETWEEN 100 AND 110
AND RIGHT(Names,1) = 'A'
Based on the info you provided, this is ugly but it should work:
create table #data
(
names varchar(50)
)
insert into #data values('type12pen105A')
insert into #data values('type12pen101A')
insert into #data values('type12pen112A')
insert into #data values('type12pen120A')
insert into #data values('type12pen110A')
insert into #data values('type12pen106A')
insert into #data values('type12pen110C')
insert into #data values('type12pen110D')
insert into #data values('type12pen110E')
insert into #data values('type12pen121B')
SELECT Names
FROM #Data
WHERE Names LIKE 'type12pen%'
AND RIGHT(Names,1) = 'A'
AND replace(replace(names, 'type12pen', ''), 'A', '') BETWEEN 100 AND 110
drop table #data
results:
type12pen105A
type12pen101A
type12pen110A
type12pen106A
declare #Data table(
name varchar(32)
)
insert into #Data values('type12pen105A')
insert into #Data values('type12pen110A')
insert into #Data values('type12pen121B')
insert into #Data values('book11jil124C')
select name
from #Data
where cast(substring(name, 10, 3) as int) between 100 and 110
and name like 'type12pen%'
and right(name, 1) = 'A'
If this is a large table you'd probably be better served by running a process on the data and splitting the different aspects of the product name out into individual fields and querying on those. Using substring and right means you won't get the benefit of indexes.

display all column with one column masked

I have two columns - name and ccNumber. I want to display both the column with one of them masked.
This query is showing only one column but I want all column to be displayed:
declare #t table (card_no varchar(20))
insert into #t
select ccNUMBER from ccinfo
select 'XXXX-XXXX-XXXX-'+ substring(card_no, 13, 4) as card_no from #t
i want name column with ccnumber column to be masked
You can try below
declare #t table (name varchar(100),card_no varchar(20))
insert into #t
select name, ccNUMBER from ccinfo
select name, 'XXXX-XXXX-XXXX-'+ substring(card_no, 13, 4) as card_no from #t
check the following example. i think you need concatenation of columns.
create table demo
(
firstName varchar(30),
secondName varchar(30)
);
insert into demo
values
('soumyajit', 'chatterjee'),
('papai', 'chatterjee'),
('virat', 'kohli');
select concat(firstName, ' ', secondName)as Name from demo;
You need to get the second column, too:
declare #t table (card_no varchar(20), name varchar(64));
insert into #t
select ccNUMBER, name from ccinfo
select 'XXXX-XXXX-XXXX-'+ substring(card_no, 13, 4) as card_no, name from #t
or just use the original table:
select 'XXXX-XXXX-XXXX-'+ substring(ccNUMBER, 13, 4) as card_no, name from ccinfo