Solved

TSQL string manupulation

Posted on 2013-05-29
7
258 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 48

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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 …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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