Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TSQL string manupulation

Posted on 2013-05-29
7
Medium Priority
?
264 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 1500 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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