SQL Server 2008 inner join with commas - sql

I am trying to figure out how to inner join a table that has ID's to my table that have all the ids in one row separated by commas.
The table with the commas is called (PA):
usersIDs
56488,51233,71055,98304,21577,12500
The table (SU) has the id and the name of the user:
UserID | Name
51233 | Bob Barker
21577 | Billy Knox
56488 | David Miller
etc etc
How can I inner join the name with the comma separated ID's in my first table?
SELECT DISTINCT SU.Name, SU.UserID, SU.Enabled
FROM Sec_User AS SU INNER JOIN
Program_Access AS PA ON SU.UserId = PA.userIDS

Here's a way without a function:
SELECT DISTINCT SU.UserID, SU.Name, SU.Enabled
FROM dbo.Sec_User AS SU
INNER JOIN dbo.Program_Access AS PA
ON ',' + PA.usersIDs + ',' LIKE '%,' + RTRIM(SU.UserID) + ',%';
But including the function for completeness (and because having such a function is very useful for many purposes). First create a table-valued function:
CREATE FUNCTION dbo.SplitInts
(
#List VARCHAR(MAX),
#Delimiter CHAR(1)
)
RETURNS TABLE
AS
RETURN ( SELECT Item = CONVERT(INT, Item) FROM (
SELECT Item = x.i.value('(./text())[1]', 'int') FROM (
SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(#List, #Delimiter, '</i><i>')
+ '</i>').query('.') ) AS a CROSS APPLY [XML].nodes('i') AS x(i)) AS y
WHERE Item IS NOT NULL
);
Now the JOIN:
SELECT DISTINCT SU.UserID, SU.Name, SU.Enabled
FROM dbo.Program_Access AS PA
CROSS APPLY dbo.SplitInts(PA.usersIDs, ',') AS s
INNER JOIN dbo.Sec_User AS SU
ON SU.UserID = s.Item;

1) First of all, create split function.
Example here.
2) Then, try to do like this:
DECLARE #PA TABLE
( usersIDs VARCHAR(50))
INSERT INTO #PA(usersIDs) VALUES('56488,51233,71055,98304,21577,12500')
DECLARE #SU TABLE
(UserID INT, Name VARCHAR(50))
[enter link description here][2]INSERT INTO #SU(UserID, Name) VALUES (51233, 'Bob Barker')
INSERT INTO #SU(UserID, Name) VALUES (21577, 'Billy Knox')
INSERT INTO #SU(UserID, Name) VALUES (56488, 'David Miller')
SELECT * FROM #PA
CROSS APPLY dbo.fnSplit(usersIDs,',')
LEFT JOIN #SU ON item = UserID

According to this link How To Split A Comma Delimited String, you can have results like this:
NewPA_Table
RecID UserID
1 56488
2 51233
3 71055
4 98304
5 21577
6 12500
and by generating that result, you could now join it to table SU.

Related

I need help parsing an HL7 string with TSQL

I have a column in a table that looks like this
Name
WALKER^JAMES^K^^
ANDERSON^MICHAEL^R^^
HUFF^CHRIS^^^
WALKER^JAMES^K^^
SWEARINGEN^TOMMY^L^^
SMITH^JOHN^JACCOB^^
I need to write a query that looks like this
Name
FirstName
LastName
MiddleName
WALKER^JAMES^K^^
JAMES
WALKER
K
ANDERSON^MICHAEL^R^^
MICHAEL
ANDERSON
R
HUFF^CHRIS^^^
CHRIS
HUFF
BUTLER^STEWART^M^^
STEWART
BUTLER
M
SWEARINGEN^TOMMY^L^^
TOMMY
SWEARINGEN
L
SMITH^JOHN^JACCOB^^
JOHN
SMITH
JACCOB
I need help generating the LastName column.
This is what I've tried so far
SUBSTRING
(
--SEARCH THE NAME COLUMN
Name,
--Starting after the first '^'
CHARINDEX('^', Name) + 1 ),
--Index of second ^ minus the index of the first ^
(CHARINDEX('^', PatientName, CHARINDEX('^', PatientName) +1)) - (CHARINDEX('^', PatientName))
)
This produces:
Invalid length parameter passed to the LEFT or SUBSTRING function.
I know this can work because if I change the minus sign to a plus sign it performs as expected.
It produces the right integer.
Where am I going wrong? Is there a better way to do this?
If you are using the latest SQL Server versions 2016 13.x or higher, you can maximize the use of string_split function with ordinal (position).
declare #strTable table(sqlstring varchar(max))
insert into #strTable (sqlstring) values ('WALKER^JAMES^K^^')
insert into #strTable (sqlstring) values ('ANDERSON^MICHAEL^R^^')
insert into #strTable (sqlstring) values ('HUFF^CHRIS^^^')
insert into #strTable (sqlstring) values ('SWEARINGEN^TOMMY^L^^');
with tmp as
(select value s, Row_Number() over (order by (select 0)) n from #strTable
cross apply String_Split(sqlstring, '^', 1))
select t2.s as FirstName, t1.s as LastName, t3.s as MiddleInitial from tmp t1
left join tmp t2 on t2.n-t1.n = 1
left join tmp t3 on t3.n-t1.n = 2
where t1.n = 1 or t1.n % 5 = 1
I recommend SUBSTRING() as it will perform the best. The challenge with SUBSTRING is it's hard to account to keep track of the nested CHARDINDEX() calls so it's better to break the calculation into pieces. I use CROSS APPLY to alias each "^" found and start from there to search for the next. Also allows to do NULLIF() = 0, so if it can't find the "^", it just returns a NULL instead of erroring out
Parse Delimited String using SUBSTRING() and CROSS APPLY
DROP TABLE IF EXISTS #Name
CREATE TABLE #Name (ID INT IDENTITY(1,1) PRIMARY KEY,[Name] varchar(255))
INSERT INTO #Name
VALUES ('WALKER^JAMES^K^^')
,('ANDERSON^MICHAEL^R^^')
,('HUFF^CHRIS^^^')
,('SWEARINGEN^TOMMY^L^^');
SELECT ID
,A.[Name]
,LastName = NULLIF(SUBSTRING(A.[Name],0,idx1),'')
,FirstName = NULLIF(SUBSTRING(A.[Name],idx1+1,idx2-idx1-1),'')
,MiddleInitial = NULLIF(SUBSTRING(A.[Name],idx2+1,idx3-idx2-1),'')
FROM #Name AS A
CROSS APPLY (SELECT idx1 = NULLIF(CHARINDEX('^',[Name]),0)) AS B
CROSS APPLY (SELECT idx2 = NULLIF(CHARINDEX('^',[Name],idx1+1),0)) AS C
CROSS APPLY (SELECT idx3 = NULLIF(CHARINDEX('^',[Name],idx2+1),0)) AS D

How to use like operator in sql to find some word in a text on another text

I need to use like operator in sql query to check a text words in another text on database records to find any records like this text words
for example:
my text is : ajax,net,apache,sql
and records like :
assembly,c#,java,apache
ajax,pascal,c,c++
...
I need a query to find any rows has my text words
this picture is search input
and I'll find any rows has words in my search string
SELECT
dbo.tblProjects.id, dbo.tblProjects.prTitle, dbo.tblUsers.id AS UserID,tblUsers.nickname,
dbo.tblProjects.prTags, dbo.tblProjects.prDesc, dbo.tblProjects.prFaDate
FROM
dbo.tblUsers
INNER JOIN
dbo.tblProjects ON dbo.tblUsers.id = dbo.tblProjects.UserID
where (tblUsers.id=#userid)and(dbo.tblProjects.tags like #userskills + '%')
order by dbo.tblProjects.id desc
Sami Update
ALTER procedure [dbo].[Dashboard_My_Skills_Projects]
(
#userid bigint,
#userskills nvarchar(100)
)
as
begin
SELECT T.id,
T.prTitle,
U.id AS [UserID],
U.nickname,
T.prTags,
T.prDesc,
T.prFaDate
FROM dbo.tblUsers U INNER JOIN dbo.tblProjects T ON U.id = T.UserID
WHERE U.id = #userid
AND
T.tags IN(
SELECT tags
FROM dbo.tblProjects T1 CROSS JOIN
(
SELECT Value
FROM STRING_SPLIT(#userskills, ',')
) TT
WHERE T1.tags LIKE '%' + Value + '%'
)
order by T.id desc
end
tblUsers
tblProjects
You can use CROSS APPLY, STRING_SPLIT() and LIKE as
CREATE TABLE T(
Tags VARCHAR(100)
);
INSERT INTO T VALUES
('Analytics,Amazon Web Service,Active Directory'),
('BMC Remedy,Big Data,Ajax'),
('Azure,Assembly,Appache'),
('Azure,Assembly,Appache'),
('Azure,Assembly,Appache'),
('Azure,Assembly,Appache'),
('Apple Safari,Analytics,Ajax');
DECLARE #Search VARCHAR(45) = 'Active Directory,Ajax,Azure';
SELECT DISTINCT Tags
FROM T CROSS APPLY (SELECT Value FROM STRING_SPLIT(#Search, ',')) TT
WHERE Tags LIKE '%' + Value + '%';
Returns:
+-------------------------------------------------+
| Tags |
+-------------------------------------------------+
| Analytics, Amazon Web Service, Active Directory |
| Apple Safari, Analytics, Ajax |
| Azure, Assembly, Appache |
| BMC Remedy, Big Data, Ajax |
+-------------------------------------------------+
Demo
Note: STRING_SPLIT() function is avaliable only on 2016+ versions, so you need to create your own if you are not working on 2016+ versions.
UPDATE
CREATE TABLE Tags(
Tags VARCHAR(100),
UserID INT
);
CREATE TABLE Users(
UserID INT,
UserName VARCHAR(45)
);
INSERT INTO Tags VALUES
('Analytics,Amazon Web Service,Active Directory', 1),
('BMC Remedy,Big Data,Ajax', 2),
('Azure,Assembly,Appache', 3),
('Azure,Assembly,Appache', 1),
('Azure,Assembly,Appache', 4),
('Azure,Assembly,Appache', 2),
('Apple Safari,Analytics,Ajax', 1);
INSERT INTO Users VALUES
(1, 'User1'),
(2, 'User2'),
(3, 'User3'),
(4, 'User4');
DECLARE #Search VARCHAR(45) = 'Active Directory,Azure',
#UserID INT = 1;
SELECT U.UserID,
U.UserName,
T.Tags Skills
FROM Users U INNER JOIN Tags T ON U.UserID = T.UserID
WHERE U.UserID = #UserID
AND
T.Tags IN(
SELECT Tags
FROM Tags T1 CROSS JOIN
(
SELECT Value
FROM STRING_SPLIT(#Search, ',')
) TT
WHERE T1.Tags LIKE '%' + Value + '%'
)
Here is a live demo
and here is your stored procedure works just fine and as expected
Like are to search for a specified pattern in a column..
And as #Pedro state in comment.. I hope you are not make this in just 1 record..
You can use
SELECT *
FROM your_table
WHERE your_text LIKE '%ac%';
You can read more about Like operator in Here
EDIT :
You can use String_Split function
SELECT
*
FROM
table
WHERE
EXISTS (SELECT
*
FROM
STRING_SPLIT(Tags, ',')
WHERE
value IN ('Active Directory', 'Ajax', 'Azure')
First Create Function
Create FUNCTION fn_SplitADelimitedList
(
#String NVARCHAR(MAX)
)
RETURNS #SplittedValues TABLE(
Value nvarchar(500)
)
As
BEGIN
DECLARE #SplitLength INT
DECLARE #Delimiter VARCHAR(10)
SET #Delimiter = ','
WHILE len(#String) > 0
BEGIN
SELECT #SplitLength = (CASE charindex(#Delimiter, #String)
WHEN 0 THEN
datalength(#String) / 2
ELSE
charindex(#Delimiter, #String) - 1
END)
INSERT INTO #SplittedValues
SELECT cast(substring(#String, 1, #SplitLength) AS nvarchar(50))
WHERE
ltrim(rtrim(isnull(substring(#String, 1, #SplitLength), ''))) <> '';
SELECT #String = (CASE ((datalength(#String) / 2) - #SplitLength)
WHEN 0 THEN
ELSE
right(#String, (datalength(#String) / 2) - #SplitLength - 1)
END)
END
RETURN
END
Then Query Like this
DECLARE #yourtext NVARCHAR(400) = 'Apple, Active Directory'
SELECT
LTRIM(RTRIM(c.Value)),
em.tags
FROM tblTest em
CROSS APPLY fn_SplitADelimitedList(em.tags) c
inner join
(SELECT Split.a.value('.', 'NVARCHAR(MAX)') DATA
FROM
(
SELECT CAST('<X>'+REPLACE(#yourtext, ',', '</X><X>')+'</X>' AS XML) AS String
) AS A
CROSS APPLY String.nodes('/X') AS Split(a)) v
on LTRIM(RTRIM(c.Value)) = LTRIM(RTRIM(v.DATA))

SQL Query using inner join

CategoryTable
Code Name
1 Food
2 Non-Food
Existing Table Consists list of category, as for example, I have two only Food and Non-Food
As challenge, I am assigning tenants with category or categories (multiple assignment, as there are tenants which are categorized as food and non-food). I i used to insert Tenant and Code to a new table creating this output
TenantAssignTable
Tenant Code
Tenant1 1,2
Tenant2 1
What I need to do, is to load the TenantAssingTable to gridview consisting the Name of the CategoryCode too like this
Desired Output
Tenant CCode Name
Tenant1 1,2 Food,Non-Food
Tenant2 1 Food
I used inner join in my code, but this is limited as I have a string of combined code in Code column.
Select a.tenant, a.ccode, b.name
from TenantAssignTable a inner join CategoryTable b
on a.CCode = b.code
Is there anyway to achieve this kind of output? I know that this is unusual in SQL coding but this is what is challenge as what the desired output is concerned and needs which is to have a multiple assignment of category to a single tenant.
Thanks in advance!
Think simple;
You can with LIKE and XML PATH
DECLARE #CategoryTable TABLE (Code VARCHAR(50), Name VARCHAR(50))
INSERT INTO #CategoryTable
VALUES
('1', 'Food'),
('2', 'Non-Food')
DECLARE #TenantAssignTable TABLE (Tenant VARCHAR(50), Code VARCHAR(50))
INSERT INTO #TenantAssignTable
VALUES
('Tenant1', '1,2'),
('Tenant2', '1')
SELECT
T.Tenant ,
T.Code,
STUFF(
(SELECT
',' + C.Name
FROM
#CategoryTable C
WHERE
',' + REPLACE(T.Code, ' ', '') + ',' LIKE '%,' + C.Code + ',%'
FOR XML PATH('')
), 1, 1, '') A
FROM
#TenantAssignTable T
Result:
Tenant Code A
--------------- ------------ ---------------
Tenant1 1,2 Food,Non-Food
Tenant2 1 Food
You can use some XML transformations:
DECLARE #x xml
SELECT #x = (
SELECT CAST('<t name="'+a.tenant +'"><a>'+REPLACE(a.code,',','</a><a>') +'</a></t>' as xml)
FROM TenantAssignTable a
FOR XML PATH('')
)
;WITH cte AS (
SELECT t.v.value('../#name','nvarchar(max)') as Tenant,
t.v.value('.','int') as CCode,
ct.Name
FROM #x.nodes('/t/a') as t(v)
INNER JOIN CategoryTable ct
ON ct.Code = t.v.value('.','int')
)
SELECT DISTINCT
c.Tenant,
STUFF((SELECT ','+CAST(CCode as nvarchar(10))
FROM cte
WHERE c.Tenant = Tenant
FOR XML PATH('')
),1,1,'') as CCode,
STUFF((SELECT ','+Name
FROM cte
WHERE c.Tenant = Tenant
FOR XML PATH('')
),1,1,'') as Name
FROM cte c
Output:
Tenant CCode Name
Tenant1 1,2 Food,Non-Food
Tenant2 1 Food
The first part (defining #x variable) will bring your table to this kind of XML:
<t name="Tenant1">
<a>1</a>
<a>2</a>
</t>
<t name="Tenant2">
<a>1</a>
</t>
Then in CTE part we join XML with table of categories. And after all get data from CTE with the help of FOR XML PATH.
Create Function as below which return Table from separated Value
CREATE FUNCTION [dbo].[fnSplit]
(
#String NVARCHAR(4000),
#Delimiter NCHAR(1)
)
RETURNS TABLE
AS
RETURN
(
WITH Split(stpos,endpos)
AS(
SELECT 0 AS stpos, CHARINDEX(#Delimiter,#String) AS endpos
UNION ALL
SELECT endpos+1, CHARINDEX(#Delimiter,#String,endpos+1)
FROM Split
WHERE endpos > 0
)
SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'Data' = SUBSTRING(#String,stpos,COALESCE(NULLIF(endpos,0),LEN(#String)+1)-stpos)
FROM Split
)
Create Function as below which return comma separated Name
CREATE FUNCTION [dbo].[GetCommaSeperatedCategory]
(
#Codes VARCHAR(50)
)
RETURNS VARCHAR(5000)
AS
BEGIN
-- Declare the return variable here
DECLARE #Categories VARCHAR(5000)
SELECT #Categories= STUFF
(
(SELECT ',' + convert(varchar(10), Name, 120)
FROM Category
WHERE Code IN (SELECT Id FROM [dbo].[fnSplit] (#Codes,',') )
ORDER BY Code
FOR XML PATH (''))
, 1, 1, '')
RETURN #Categories
END
AND Last:
SELECT
Tenant,
Code,
(SELECT [dbo].[GetCommaSeperatedCategory] (Code)) AS Name
FROM TblTenant

sql Id concatenation in sequence in a separate column like running total

I need running Id concatenation just like running balance or total..
Concatenate the previous Ids to current Id row wise just like shown in picture
query is
with relation (Id, [orderSequence])
as
(
select Id,cast(Id as varchar(20))
from [ACChartofAccount]
union all
select p.Id, cast(Cast(r.Id as varchar) + ',' + cast(p.Id as varchar) as varchar(20))
from [ACChartofAccount] p
inner join relation r on p.ParentId = r.Id
)
select Id,orderSequence
from relation
order by orderSequence
You can use below query to get above result.
DECLARE #Table TABLE(ID VARCHAR(10));
INSERT INTO #table(ID) VALUES ('320'),(332),(333),(334),(335);
SELECT mt.ID,
STUFF((
SELECT ', ' + ID
FROM #table t
WHERE t.ID <= mt.ID
FOR XML PATH('')), 1, 2, '') AS oldersequence
FROM #table mt
ORDER BY ID

Comma Separated SQL Server Result Set 'JOINED' with other Columns

I have a table say ProjectMaster:
Id ProjectName
1 A
2 B
3 C
another table ProjectMeter
Id ProjectId MeterNumber
1 1 #0001
2 1 #0002
3 1 #0003
4 2 #0004
5 2 #0005
6 3 #0006
I wish to have following output
ProjectName MeterNumbers
A #0001, #0002, #0003
B #0004, #0005
C #0006
I tried this and this, but unable to solve my problem.
I cannot use a table variable.
I have a already written Stored Procedure and it brings data from many joined tables. ProjectMaster also happens to be joined in one of these tables. Now am required to fetch data from ProjectMeter, such that, each row has concatenated ProjectMeter.MeterNumber corresponding to the ProjectId in that column.
right now, I get concatenated list of all meternumbers in all the rows.
I cannot use CURSOR, TABLE variable , Temp TABLE
( I hope still something can be done to my cause)
please help.....
Try this:
SELECT projectname, STUFF((SELECT distinct ', ' + meternumber
from projectmeter m
where p.id = m.projectid
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'') MeterNumbers
from projectmaster p
See SQL Fiddle with Demo
DECLARE #ProjectMaster AS TABLE
(
ID INT IDENTITY(1, 1) ,
ProjectName VARCHAR(2)
)
DECLARE #ProjectMeter AS TABLE
(
ID INT IDENTITY(1, 1) ,
ProjectID INT ,
MeterNumber VARCHAR(50)
)
INSERT INTO #ProjectMaster
( ProjectName )
VALUES ( 'A' )
INSERT INTO #ProjectMeter
( ProjectID, MeterNumber )
VALUES ( 1, '#0001' )
INSERT INTO #ProjectMeter
( ProjectID, MeterNumber )
VALUES ( 1, '#0002' )
SELECT pMaster.ID, STUFF(( SELECT ',' + MeterNumber
FROM #ProjectMeter
FOR
XML PATH('')
), 1, 1, '') AS 'Concat Result'
FROM #ProjectMeter pMeter
INNER JOIN #ProjectMaster pMaster ON pMaster.ID = pMeter.ProjectID
GROUP BY pMaster.ID
I have used table variables here but surely you just need to drop the #'s as I have used the same table names as you have specified? Not sure if this is okay? :)
Also in MS SQL you can do it using recursive query with CTE.
Here is a SQLFiddle demo
;with t1 as (
select t.*,
cast(meternumber as varchar(max)) as m2,
0 as level
from ProjectMeter t
where not exists
(select id
from ProjectMeter l
where l.id<t.id and l.ProjectId=t.ProjectID
)
union all
select b.*,
cast(c.m2+','+b.MeterNumber as varchar(max)) as m2,
c.level+1 as level
from ProjectMeter b
inner join t1 c
on (c.id < b.id) and (b.ProjectID=c.ProjectId)
)
select pm.ProjectName as ProjectName,
t1.m2 as MeterNumbers
from t1
inner join
(select ProjectId,max(level) ml
from t1
group by ProjectId
) t2
on (t1.ProjectId=t2.ProjectID) and (t1.level=t2.ml)
left join ProjectMaster pm
on (t1.ProjectId=pm.Id)
order by t1.ProjectID