Solved

sql server string manipulation in stored procedure

Posted on 2004-08-02
3
1,164 Views
Last Modified: 2012-06-27
is there a way to unstring an input string delimited by a character into two string variables. i was able to do the same in cobol. basically users can type in a string with a '.' anywhere within a string. i need to extract the part of the string befrore the '.' and after the '.' into two seperate variables. i am coding this in a stored procedure on sql server.any suggestions.

your help is greatly appreciated.
0
Comment
Question by:suryajandhyala
3 Comments
 
LVL 7

Expert Comment

by:Lori99
ID: 11697488
See if this works for you:

create procedure SplitString
            @inputstring varchar(40),
            @outputStr1 varchar(40) OUTPUT,
            @outputStr2 varchar(40) OUTPUT
AS

SELECT @outputStr1 = substring(@inputstring,1,(charindex('.',@inputstring) - 1))
SELECT @outputStr2 = substring(@inputstring,(charindex('.',@inputstring) + 1),len(@inputstring))
0
 
LVL 10

Accepted Solution

by:
ibost earned 500 total points
ID: 11697577
You might add a check:

IF CHARINDEX('.', @inputstring) <> 0
BEGIN
   SELECT @outputStr1 = substring(@inputstring,1,(charindex('.',@inputstring) - 1))
   SELECT @outputStr2 = substring(@inputstring,(charindex('.',@inputstring) + 1),len(@inputstring))
END

Or maybe
IF CHARINDEX('.', @inputstring) = 0
BEGIN
   RAISERROR('No delimiter found in inputstring', 16, 1)
   RETURN -1
END

SELECT...
SELECT...


Otherwise you'll get a strange exception if the input string does not contain a delimiter character.

-Ian
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11697875
Try this:

Declare @Value varchar(200)

Set @Value = 'Before part of the string.After part of the string'

Select PARSENAME(@Value, 2), PARSENAME(@Value, 1)
0

Featured Post

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.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

831 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