Solved

Truncate data in a column?

Posted on 2011-02-18
3
503 Views
Last Modified: 2012-05-11
I have data in a column that looks like this:

dbCatalog=DBTEST&UserID=admin&StoreID=12&Group=RG_Name&

When I run a query, I just want to return the UserID (admin) in this scenario.

How can I do this?
0
Comment
Question by:mattkovo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Expert Comment

by:mcv22
ID: 34930716

SELECT
	SUBSTRING(ColumnName, PATINDEX('%&UserID=%', ColumnName) + LEN('&UserID='), PATINDEX('%&StoreID=%', ColumnName) - PATINDEX('%&UserID=%', ColumnName) + LEN('&UserID='))
FROM
	TableName
WHERE
	PATINDEX('%&UserID=%', ColumnName) > 0

Open in new window

0
 
LVL 12

Expert Comment

by:mcv22
ID: 34930728
Actually,


SELECT
	SUBSTRING(ColumnName, PATINDEX('%&UserID=%', ColumnName) + LEN('&UserID='), PATINDEX('%&StoreID=%', ColumnName) - PATINDEX('%&UserID=%', ColumnName) - LEN('&UserID='))
FROM
	TableName
WHERE
	PATINDEX('%&UserID=%', ColumnName) > 0

Open in new window

0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 34930770
If possible, create a function to split the column values to tabular format and then do your search. Why I suggesting this is, if you have dbCatelog,UserID,StoreID and GroupID not in the same order everytime, then the query with PATINDX mentioned above would fail.
CREATE FUNCTION [dbo].[fn_Split](@text nvarchar(max), @delimiter char(1) = ' ')
RETURNS @Strings TABLE (position int IDENTITY PRIMARY KEY, value nvarchar(max)) AS 
  BEGIN 
DECLARE @index int
    SET @index = -1
  WHILE (LEN(@text) > 0)
  BEGIN
    SET @index = CHARINDEX(@delimiter , @text)
     IF (@index = 0) AND (LEN(@text) > 0)
  BEGIN 
 INSERT INTO @Strings VALUES (@text)
  BREAK 
    END
     IF (@index > 1)
  BEGIN
 INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
    SET @text = RIGHT(@text, (LEN(@text) - @index))
    END 
   ELSE 
    SET @text = RIGHT(@text, (LEN(@text) - @index))
    END
 RETURN
    END
 GO

Open in new window

Use the above created function in the query.
select t3.col,t4.value
  from (
select *
  from @table t1
 cross apply dbo.fn_Split(col,'&') t2) t3 
 cross apply dbo.fn_Split(value,'=') t4
where t4.position = 2 and t3.value = 'UserID'

Open in new window

Tested with your sample data.
declare @table table(col varchar(200))
insert @table values ('dbCatalog=DBTEST&UserID=admin&StoreID=12&Group=RG_Name&')
insert @table values ('dbCatalog=DBTEST&StoreID=12&UserID=admin&Group=RG_Name&')
insert @table values ('dbCatalog=DBTEST&StoreID=12&Group=RG_Name&')
insert @table values ('dbCatalog=DBTEST&StoreID=12&Group=RG_Name&UserID=admin')

select t3.col,t4.value
  from (
select *
  from @table t1
 cross apply dbo.fn_Split(col,'&') t2) t3 
 cross apply dbo.fn_Split(value,'=') t4
where t4.position = 2 and t3.value = 'UserID'
/*
col	value
dbCatalog=DBTEST&UserID=admin&StoreID=12&Group=RG_Name&	admin
dbCatalog=DBTEST&StoreID=12&UserID=admin&Group=RG_Name&	admin
dbCatalog=DBTEST&StoreID=12&Group=RG_Name&UserID=admin	admin
*/

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question