Solved

TSQL string manupulation

Posted on 2013-05-29
7
261 Views
Last Modified: 2013-06-20
i want to check if the column in inserted row in the table like this format .

@msg = "age 20";
@msg=  "region new york";

i want to check the first one like age or region
than i trim the @msg and get the other part after "age " or "region " or "sex "
0
Comment
Question by:afifosh
[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
7 Comments
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39203984
Here is an example

Just use case statements to check, in the example i split it into two columns just for easy reading

http://sqlfiddle.com/#!3/d41d8/14651

Declare @msg1 varchar(30) 
Set @msg1 = 'age 20'

Declare @msg varchar(30) 
set @msg =  'region new york'


SELECT 
Case 
when CHARINDEX('region',@msg) > 0  then REPLACE (@msg ,'region','')
else @msg
end as Test,
Case 
when CHARINDEX('age',@msg1) > 0  then REPLACE (@msg1 ,'age','')
else @msg1
end as Test1

Open in new window

0
 
LVL 1

Author Comment

by:afifosh
ID: 39204037
Declare @msg1 varchar(30)
Set @msg1 = 'age 20'

Declare @msg varchar(30)
set @msg =  'a region  0 new york'


SELECT
Case
when CHARINDEX('region',@msg) > 0  then REPLACE (@msg ,'region','')
else @msg
end as Test,
Case
when CHARINDEX('age',@msg1) > 0  then REPLACE (@msg1 ,'age','')
else @msg1
end as Test1




test this one the message should be start with Region !!!!!!
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39204045
if you could show the data before and how you want it to look after we can knock something up for you

the more info the better
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 1

Author Comment

by:afifosh
ID: 39204056
look , @msg = 'region XXX XXXX'
or @msg = 'age XX XX'

if @msg start with region take the second part after the space

begin

end

else if @msg start with age take the second part after the space

begin


end
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39204062
You mean something like this

http://sqlfiddle.com/#!3/05aa5/2
or
http://sqlfiddle.com/#!3/f974a/1

SELECT 
Case 
when CHARINDEX('region',Msg) > 0  then REPLACE (Msg ,'region','')
when CHARINDEX('age',Msg) > 0  then REPLACE (Msg ,'age','')
else msg
end as Test
from 
TEST

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39204092
Is it just the 3 strings and no more? ("age " or "region " or "sex ")
and always with a space after?
are they 'mutually exclusive'? i.e. this does not occur 'Region Australia Age Young-at-heart'
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 39204576
>>set @msg =  'a region  0 new york'<<
With this example, using replace would yield 'a  0 new york' when I suspect you would want '0 new york'
Perhaps something like
case
 when charindex('region ',@msg) > 0
 then substring(@msg,charindex('region ',@msg) + 7,len(@msg))
 when charindex('age ',@msg) > 0
 then substring(@msg,charindex('age ',@msg) + 4,len(@msg))
 when charindex('sex ',@msg) > 0
 then substring(@msg,charindex('sex ',@msg) + 4,len(@msg))
end
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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