how to pick string basing on ID wise - sql

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

Related

SELECT an Xml variable into a table

I have a variable called result of type Xml that contains one cell of Xml text. I cannot seem to use SELECT INTO to insert this value into another temp table.
SELECT #result
INTO dbo.xml_temp
Is there a way to achieve this?
If you want to insert the XML into an existing table, you have
VALUES
or INSERT INTO ... SELECT ...:
Try this:
DECLARE #tbl TABLE(ID INT IDENTITY,TargetColumn XML);
DECLARE #SomeXML XML ='<root>test</root>';
INSERT INTO #tbl VALUES(#SomeXML);
INSERT INTO #tbl(TargetColumn) SELECT #SomeXML;
SELECT * FROM #tbl;
If you really want to create a new temp table, your statement is just missing an alias (How should the new table know the column's name?):
SELECT #SomeXML AS SomeName INTO #tmpTable;
SELECT * FROM #tmpTable;

Remove value from string where it match another column

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

how to pick only last string

I have data like this I have seen functions and Substring and LEFT ,RIGHT also
but it is not serving my purpose
declare #t table (val varchar(50))
INSERT INTO #t(val)values ('E-001GHDEM120ENDORSEMENT'),
('E-001GHDEM120Renewal'),
('E-001GHDEM120Adjustment'),
('E-001GHDEM120ENDORSEMENT')
select * from #t
output
ENDORSEMENT
Renewal
Adjustment
ENDORSEMENT
I need to use that statement in where condition to filter records
Try this select
DECLARE #t TABLE
(
val VARCHAR(50)
);
INSERT INTO #t
(val
)
VALUES
('E-001GHDEM120ENDORSEMENT'
),
('E-001GHDEM120Renewal'
),
('E-001GHDEM120Adjustment'
),
('E-001GHDEM120ENDORSEMENT'
);
SELECT REVERSE(SUBSTRING(REVERSE(val), 0, PATINDEX('%[^a-zA-Z]%', REVERSE(val)))) AS val
FROM #t;
Try This. From your example here is what i understood.
select right(val,patindex('%[0-9]%', reverse(val))-1)
from #t

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.