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
Related
I have a table, I want to pivot the table, my desired output is #tab2.
My table is as follows:
declare #tab1 table(name varchar(50),mobile varchar(10),address varchar(100))
insert into #tab1 values('Test','2612354598','CG-10')
select * from #tab1
My desired output is:
declare #tab2 table(colname varchar(50),value varchar(100))
insert into #tab2 values('name','Test'),('mobile','2612354598'),('address','CG-10')
select * from #tab2
Please help
what are you looking is Unpivot not Pivot. Use Unpivot as follows. make sure that all columns have same datatype.
select
colname,
value
from #tab1
unpivot
(
value
for colname in ([name]
,[mobile]
,[address]
)
) unpiv;
If using SQL server you can use UNPIVOT.
SELECT colname, valueid
FROM
(SELECT CAST(name as varchar(100)) name, CAST(mobile as varchar(100))
mobile, address FROM #tab1) p
UNPIVOT
(valueid FOR colname IN
(name,mobile, address)
)AS unpvt;
You need to CAST() the columns so the type is the same for all of them.
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
I have a self refrenced table that represents some data like below:
declare #table table(
ID int,
ParentID int,
Name varchar(50),
levelNode int
)
declare #Temptable table(
ID int,
ParentID int,
Name varchar(50),
levelNode int
)
declare #maxlevel int
insert into #table (ID,ParentID,Name,levelNode) select 11,null,'A',1
insert into #table (ID,ParentID,Name,levelNode) select 12,11,'B-1',2
insert into #table (ID,ParentID,Name,levelNode) select 13,11,'B-2',2
insert into #table (ID,ParentID,Name,levelNode) select 14,12,'B-1-1',3
insert into #table (ID,ParentID,Name,levelNode) select 15,12,'B-1-2',3
insert into #table (ID,ParentID,Name,levelNode) select 16,12,'B-1-3',3
insert into #table (ID,ParentID,Name,levelNode) select 17,13,'B-2-1',3
insert into #table (ID,ParentID,Name,levelNode) select 18,13,'B-2-2',3
insert into #table (ID,ParentID,Name,levelNode) select 19,13,'B-2-3',3
insert into #table (ID,ParentID,Name,levelNode) select 20,19,'B-2-3-1',4
insert into #table (ID,ParentID,Name,levelNode) select 21,19,'B-2-3-2',4
insert into #table (ID,ParentID,Name,levelNode) select 22,17,'B-2-1-1',4
insert into #table (ID,ParentID,Name,levelNode) select 23,17,'B-2-1-2',4
declare #ID int
select #ID=11;
With ret AS(
select * from #table
where ID=#ID
union all
select t.* from #table t inner join ret r ON t.ParentID=r.ID
)
insert into #Temptable select * from ret where ID<>#ID
order by ??????????????????
select * from #Temptable
I want to order them like this:
What should I write in order by section!
Try this..
Fiddle demo
INSERT INTO #Temptable
SELECT *
FROM ret
WHERE id <> #ID
ORDER BY Substring(NAME, 3, 1),
Substring(NAME, 5, 1),
Substring(NAME, 7, 1)
This is my table
create table #t(id varchar(10),value varchar(10))
insert into #t values('0001','a'),('0001','b'),('0002','c'),('0002','d')
I need a efficient method of making csv
id value
0001 a,b
0002 c,d
try this!
using Stuff with xml path('')
select id,
stuff(
(
select ',' + value from #t t1 where t1.id=t2.id for xml path(''),type).value('.','varchar(max)'),1,1,'') from #t t2
group by t2.id
Hello below given requirement both columns are in VARCHAR . So we can use SUBSTRING FUNCTION and CROSS APPLY to get Expected output
DECLARE #t TABLE (ID VARCHAR(10) ,VALUE VARCHAR(10) )
INSERT INTO #t (ID,VALUE)VALUES ('0001','a')
INSERT INTO #t (ID,VALUE)VALUES ('0001','b')
INSERT INTO #t (ID,VALUE)VALUES ('0002','c')
INSERT INTO #t (ID,VALUE)VALUES ('0002','d')
INSERT INTO #t (ID,VALUE)VALUES ('0003','e')
INSERT INTO #t (ID,VALUE)VALUES ('0003','f')
SELECT [Id],
SUBSTRING(d.Value,1, LEN(d.Value) - 1) VALUE
FROM
(
SELECT DISTINCT [Id]
FROM #t
) a
CROSS APPLY
(
SELECT Value + ', '
FROM #t AS B
WHERE A.[Id] = B.[Id]
FOR XML PATH('')
) D (Value)
I have a table like this
Declare #Temp Table(Data VarChar(20))
Insert Into #Temp Values('F_200_100_')
Insert Into #Temp Values('F_50_')
Insert Into #Temp Values('F_30_')
Insert Into #Temp Values('F_50_10')
Insert Into #Temp Values('F_100_')
Insert Into #Temp Values('F_20_')
I want my output to be distinct values of the numbers extracted from data column
20
30
50
100
200
I have tried using patindex but I am looking for ideas
tried this
select
Left(
SubString(Data, PatIndex('%[0-9]%', Data), 8000),
PatIndex('%[^0-9]%', SubString(Data, PatIndex('%[0-9]%', Data), 8000) + 'X')-1
)
from #temp
Reference
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/extracting-numbers-with-sql-server
try this:
DECLARE #YourTable table (RowID int, Layout varchar(200))
INSERT INTO #YourTable
select ROW_NUMBER() over (order by (select 0)) as rn,replace(RIGHT(data,len(data)-CHARINDEX('_',data,1)),'_',',') from temptab
;WITH SplitSting AS
(
SELECT
RowID,LEFT(Layout,CHARINDEX(',',Layout)-1) AS Part
,RIGHT(Layout,LEN(Layout)-CHARINDEX(',',Layout)) AS Remainder
FROM #YourTable
WHERE Layout IS NOT NULL AND CHARINDEX(',',Layout)>0
UNION ALL
SELECT
RowID,LEFT(Remainder,CHARINDEX(',',Remainder)-1)
,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder))
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0
UNION ALL
SELECT
RowID,Remainder,null
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)=0
)
SELECT distinct cast(part as int) FROM SplitSting where len(part) > 0 order by cast(part as int)