Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have record in SQL database with varchar(max) datatype like as below
ColumnName :: UserID
Row-1 | 1,2,3,4,5,6
Row-2 | 1,4,5,6
Row-3 | 1

Than i need to retrive record from that column like as below

select * from Table where UserID=1

But that only display only Row-3

i need the result as all Rows which have entry 1

is it possible in SQLserver ???
Posted
Updated 28-Mar-14 23:32pm
v2
Comments
Siva Hyderabad 29-Mar-14 5:25am    
Try this]
select * from Table where Column1=1 and Column2=1 and Column3=1
JatinKhimani 29-Mar-14 5:33am    
i have records of Rows in one column so its not possible..

Yes...but it's not at all simple, and it would be a much, much better idea to separate those into separate rows in a different table, with a foreign key relationship back to this one. The problem is that SQL string handling is...basic...and it doesn't know about comma separated data, or arrays, or anything which would make this task easier.

This shows you how to do it: Converting comma separated data in a column to rows for selection[^] - but if you do it often, or on a large set of data, it's a very slow process.

Seriously, I'd use another table.
 
Share this answer
 
Comments
JatinKhimani 29-Mar-14 5:13am    
Thanking you so much.. i will try for it if its working than okay
otherwise i will follow you !!
use like command with wildcards
 
Share this answer
 
There isn't proper way to store numbers as a text.

If you want to extract numbers from text, please try this:
SQL
DECLARE @tmp TABLE (UserID VARCHAR(30))

INSERT INTO @tmp (UserID)
VALUES('1,2,3,4,5,6'), ('1,4,5,6'), ('1')

;WITH MyCTE AS
(
	SELECT 1 AS RowNo, UserID AS NewUserId, NULL AS Remainder 
	FROM @tmp
	WHERE CHARINDEX(',',UserID)=0
	UNION ALL
	SELECT 2 AS RowNo, LEFT(UserID,CHARINDEX(',',UserID)-1) AS NewUserId, RIGHT(UserID,LEN(UserID) - CHARINDEX(',', UserID)) AS Remainder
	FROM @tmp
	WHERE CHARINDEX(',',UserID)>0
	UNION ALL
	SELECT RowNo+1 AS RowNo, LEFT(Remainder,CHARINDEX(',',Remainder)-1) AS NewUserId, RIGHT(Remainder,LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
	FROM MyCTE
	WHERE CHARINDEX(',',Remainder)>0
	UNION ALL 
	SELECT RowNo+1 AS RowNo, Remainder AS NewUserId, NULL AS Remainder
	FROM MyCTE 
	WHERE CHARINDEX(',',Remainder)=0
)
SELECT RowNo, CONVERT(INT,NewUserID) AS NewUserID, Remainder 
FROM MyCTE
--OPTION (MAXRECURSION 0)


Result:
RowNo  NewUID Remainder
1      1      NULL
2      1      2,3,4,5,6
2      1      4,5,6
3      4      5,6
4      5      6
5      6      NULL
3      2      3,4,5,6
4      3      4,5,6
5      4      5,6
6      5      6
7      6      NULL
 
Share this answer
 
Comments
JatinKhimani 30-Mar-14 23:33pm    
Thankig ypu !! That's working but after getting result i want to compare that record to another table record that can't be implemented..
Maciej Los 31-Mar-14 2:33am    
Try:
SELECT t1.RowNo, CONVERT(INT,t1.NewUserID) AS NewUserID, t2.UserID AS UserIDFromTable2
FROM MyCTE AS t1 INNER JOIN AnotherTable AS t2 ON t1.NewUserID = t2.UserID
Maciej Los 31-Mar-14 2:34am    
It would be nice to see above answer as a solution. Please use green button to accept it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900