Solved

substring with defined delimiters

Posted on 2006-06-09
14
693 Views
Last Modified: 2008-03-04
hi experts,

i'm looking for an SQL function where i can extract a string but instead of start and end position to set the limits of the substring I need it with specific characters.
This would be similar to extractString in Crystal Reports.

Any ideas,

Thanks
cm
0
Comment
Question by:carlosmonte
  • 7
  • 7
14 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16871758
you can use CHARINDEX('Delimitter',theString) instaed of start position andof course for lenght
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16871780
for example

declare @str varchar(100)
set @str = '1000-wer'
SELECT SUBSTRING(@str,CHARINDEX('-',@str)+1, LEN(@str)-CHARINDEX('-',@str)+1)
0
 
LVL 1

Author Comment

by:carlosmonte
ID: 16871920
The delimiters need to be pound "#" and closed parethesis ")"
can you show me how to do this?
Length is not a factor, i will never know the length of the substring
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 75

Expert Comment

by:Aneesh Retnakaran
ID: 16871944
declare @str varchar(100)
set @str = '1000#werterq)23'
SELECT SUBSTRING(@str,CHARINDEX('#',@str)+1, CHARINDEX(')',@str)-CHARINDEX('#',@str)-1)
0
 
LVL 1

Author Comment

by:carlosmonte
ID: 16872014
I get this error message

Invalid length parameter passed to the substring function

some fields that i'm getting the substring from do not have a "#" or ")"
this needs to be taken in account
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16872047
OK , try this


declare @str varchar(100)
set @str = '1000#werterq23'
SELECT SUBSTRING(@str,
        CHARINDEX('#',@str)+1 ,
        CASE WHEN  CHARINDEX(')',@str)=0 THEN LEN(@Str)+1 ELSE CHARINDEX(')',@str) END
        -CHARINDEX('#',@str)-1)
0
 
LVL 1

Author Comment

by:carlosmonte
ID: 16872102
seems to be working. I just need to check the output of the data. let you know soon.
thanks
0
 
LVL 1

Author Comment

by:carlosmonte
ID: 16872718
one last thing

if there is no substring this function gives me the entire @str variable again.
I need it to be NULL or empty (preferred NULL) if there is no substring.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16872803
I dont understand correctly.. can you give me a sample , presently what i am doing is i will return the entire string
0
 
LVL 1

Author Comment

by:carlosmonte
ID: 16872841
correct, it returns the entire string from @str because there is no substring with the delimiters we established.
instead though if there is no substring between those delimiters, the select should return empty string or NULL
let me know if this is clearer
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 16873721
Like this

declare @str varchar(100)
set @str = '1000#werterq23'
SELECT CASE WHEN LEN (SUBSTRING(@str,
        CHARINDEX('#',@str)+1 ,
        CASE WHEN  CHARINDEX(')',@str)=0 THEN LEN(@Str)+1 ELSE CHARINDEX(')',@str) END
        -CHARINDEX('#',@str)-1) ) = LEN(@Str) THEN NULL
            ELSE SUBSTRING(@str,
        CHARINDEX('#',@str)+1 ,
        CASE WHEN  CHARINDEX(')',@str)=0 THEN LEN(@Str)+1 ELSE CHARINDEX(')',@str) END
        -CHARINDEX('#',@str)-1) END
0
 
LVL 1

Author Comment

by:carlosmonte
ID: 16873723
not possible?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16873743
check my last post
0
 
LVL 1

Author Comment

by:carlosmonte
ID: 16874002
yes it worked. Thanks again
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server query 18 42
HTML <font style="color:red"> 9 32
SQL Recursion 6 20
Get rid of the last Last comma and space in a SQL statement. 7 19
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 shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

821 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