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
Related
I have a table with two columns. ParameterName and ParameterValue.
The easy case is when my parameter has a value like this:
The problem is, sometimes, a parameter value can come from another parameter. Like this:
This situation may get more complicated and the second parameter also gets its value from the third parameter and so on...
I know it might be a common case and might have an easy solution but I couldn't find the answer and I don't know what is the name of this type of situation.
Can anyone help me? I need to bring the value for all parameters. I thought the answer was recursive cte but after trying it, it seems it is not the answer.
I put the code for my sample table below:
DECLARE #T TABLE
( ParameterName NVARCHAR(128) NULL,
ParameterValue NVARCHAR(128) NULL
)
INSERT #T
VALUES ( '$A', 'SOME VALUE'),
( '$B', '$A')
SELECT * FROM #T
The answer was recursive CTE and worked like this:
I also added more data to my table.
DECLARE #T TABLE
( ParameterName NVARCHAR(128) NULL,
ParameterValue NVARCHAR(128) NULL
)
INSERT #T
VALUES ( '$A', 'SOME VALUE'),
( '$B', '$A'),
( '$C', 'AAAAA'),
( '$D', '$A'),
( '$E', '$D')
;WITH VALS
AS ( SELECT ParameterName, ParameterValue
FROM #T
WHERE ParameterValue NOT LIKE '$%'
UNION ALL
SELECT T.ParameterName, V.ParameterValue
FROM #T AS T
INNER JOIN VALS AS V ON T.ParameterValue = V.ParameterName
)
SELECT * FROM VALS
Now it works like this:
Below is my table,
create table t(
id int,
colParam varchar(max))
insert into t values(1,'["param1", "param2"]')
insert into t values(2,'["param2"]')
insert into t values(3,'["param1"]')
insert into t values(4,'["param2", "param3"]')
insert into t values(5,'["param1", "param2"]')
tried
declare #str varchar(max) = 'param1'; Select * from t where colParam like '%'+ #str+'%'
its not working for
declare #str varchar(max) = 'param1,param2'; Select * from t where colParam like '%'+ #str+'%'
i want to select rows by passing colPar as 'param1,param2' so it will result me all the records containing param1 and param2 in colParam
This quiet tricky.
create table #t(
id int,
colParam varchar(max)
)
insert into #t values(1,'["param1", "param2"]')
insert into #t values(2,'["param2"]')
insert into #t values(3,'["param1"]')
insert into #t values(4,'["param2", "param3"]')
insert into #t values(5,'["param1", "param2"]')
declare #str varchar(max) = 'param1,param2';
to Return all matching values.
select distinct id, t1.colParam from #t t1
cross apply string_split(t1.colParam, ',') t2
cross apply string_split(#str, ',') t3
where t2.value like '%'+t3.value+'%'
Output:
If you are using SQL Server 2016 and above, then you can use STRING_SPLIT.
As MSDN says:
A table-valued function that splits a string into rows of substrings,
based on a specified separator character.
So your can look like this:
DECLARE #t TABLE
(
id int,
colParam varchar(max)
)
insert into #t values(1,'["param1", "param2"]')
insert into #t values(2,'["param2"]')
insert into #t values(3,'["param1"]')
insert into #t values(4,'["param2", "param3"]')
insert into #t values(5,'["param1", "param2"]')
SELECT
t.id
, s.col
FROM #t AS t
OUTER APPLY
(
SELECT
spl.value AS col
FROM STRING_SPLIT(
(SELECT _t.colParam FROM #t AS _t WHERE _t.id = t.id), ',') AS spl
)s
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
i have the data like this:
CODE_VD
N_10_19_xxx
N_0_3_xxx
N_121_131_xxx
N_100_120_xxx
N_80_90_xxx
N_20_29_xxx
as you can see i need to sort just the first number after N_,i don't know how can i get this number.
i have tried with susbsting(CODE_VD,2,3) but not exactly what i expected.
i want to get this:
CODE_VD
N_0_3_xxx
N_10_19_xxx
N_20_29_xxx
N_80_90_xxx
N_100_120_xxx
N_121_131_xxx
how can i do that ?
DECLARE #MyTable TABLE
(
CODE_VD VARCHAR(20)
)
INSERT INTO #MyTable
( CODE_VD )
VALUES
('N_10_19_xxx'),
('N_0_3_xxx'),
('N_121_131_xxx'),
('N_100_120_xxx'),
('N_80_90_xxx'),
('N_20_29_xxx');
SELECT * FROM
(
SELECT
*,
CONVERT(INT,
SUBSTRING(mt.CODE_VD,
3,
CHARINDEX('_', mt.CODE_VD, 3) - 3)) ConvCol
FROM #MyTable mt
) mt
ORDER BY mt.ConvCol
I converted to int to get the sort to work correctly, because 100 > 20
SELECT SUBSTRING(CODE_VD,3, CHARINDEX('_',CODE_VD, 3)-3)
declare #t Table (CODE_VD VARCHAR(MAX))
INSERT INTO #t (CODE_VD)VALUES ('N_10_19_xxx')
INSERT INTO #t (CODE_VD)VALUES ('N_0_3_xxx')
INSERT INTO #t (CODE_VD)VALUES ('N_121_131_xxx')
INSERT INTO #t (CODE_VD)VALUES ('N_100_120_xxx')
;WITH
sorted
AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY SUBSTRING(CODE_VD,3, CHARINDEX('_',CODE_VD, 3)-3) ORDER BY CODE_VD) AS sequence_id
FROM
#t
)
SELECT
CODE_VD
FROM
sorted
WHERE
sequence_id = 1
Simple question, just out of curiosity.
For example select 1,2,3 that will show table with one column and three rows.
Something like this: select values(1),(2),(3)
*with one select statement
An example for my comment in your post.
DECLARE #TABLE TABLE (ONE INT, TWO INT, THREE INT)
INSERT INTO #TABLE VALUES (1,2,3)
SELECT UP.COL, UP.VALUE
FROM #TABLE
UNPIVOT (VALUE FOR COL IN (ONE,TWO,THREE)) UP
Query:
DECLARE #t TABLE (i1 INT, i2 INT, i3 INT)
INSERT INTO #t VALUES (1, 2, 3)
SELECT t.*
FROM #t
CROSS APPLY (
VALUES(i1), (i2), (i3)
) t(value)
Output:
value
-----------
1
2
3
Additional info:
http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html
As it appears there is a simple code that I've been searching for:
select n from (values (1),(2),(3)) D(c);