Solved

TSQL string manupulation

Posted on 2013-05-29
7
256 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
7 Comments
 
LVL 7

Expert Comment

by:Ross Turner
Comment Utility
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
Comment Utility
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
Comment Utility
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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 1

Author Comment

by:afifosh
Comment Utility
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
Comment Utility
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 48

Expert Comment

by:PortletPaul
Comment Utility
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 31

Accepted Solution

by:
awking00 earned 500 total points
Comment Utility
>>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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now