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

x
?
Solved

Length of Substring

Posted on 2012-03-29
4
Medium Priority
?
421 Views
Last Modified: 2012-03-30
Code below sometimes works my problem is that when the subtraction for length is done sometimes it comes back negative and causes query to fail any ideas..

query below is the same just about is the subtraction I need to fix I need to ensure I at least get 1 or 0 what ever makes it not crash

My issue is updating the table to remove the value  "%2F" from the string
its right after the =5299

Any other suggestion will be great


DECLARE @str VARCHAR(8000)

SET @str = 'BT_XYZ=1&referrerID=60081270&campID=5299%2F&BTData=C0216787861617459544B4BBCB6A9BFA59F9B8A89F6EEF7E9E2D9D6C7DE2722BD22EB8&BT_TRF=118447&BT_WAV=20120322&BT_LNK=10151&BT_EML=5543&BT_VAR=1583'
--SET @str = 'BT_XYZ=1&referrerID=60081270&campID=6299&BTData=C0216787861617459544B4BBCB6A9BFA59F9B8A89F6EEF7E9E2D9D6C7DE2722BD22EB8&BT_TRF=118447&BT_WAV=20120322&BT_LNK=10151&BT_EML=5543&BT_VAR=1583'

PRINT @str

SELECT SUBSTRING(@str,CHARINDEX('&campID=',@str),CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str)-3)


SELECT LEN(SUBSTRING(@str,CHARINDEX('&campID=',@str),CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str)))

SELECT REPLACE(@str,(SUBSTRING(@str,CHARINDEX('&campID=',@str),
			   CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str))),(SUBSTRING(@str,CHARINDEX('&campID=',@str),
			   CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str))))

SELECT LEN(SUBSTRING(@str,CHARINDEX('&campID=',@str),CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str)))

Open in new window

0
Comment
Question by:Leo Torres
[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
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37785164
Why not just use REPLACE as in:
SELECT  REPLACE(@str, '%2F', '')
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 37785272
because that will only remove that value this is a string being created by an application %2F can be any thing if it became 3%D then then replace is no good
0
 
LVL 39

Accepted Solution

by:
appari earned 2000 total points
ID: 37785862
are you sure &campID and &BTData are always in the same order as in your sample data?
do you want to delete 3 charcaters before &BTData?
can you post sample data that's causing the error. the data and the sql you posted is not giving any error.  the following SQL is removing the %2F, (Bold part is added by me)


DECLARE @str VARCHAR(8000)

SET @str = 'BT_XYZ=1&referrerID=60081270&campID=5299%2F&BTData=C0216787861617459544B4BBCB6A9BFA59F9B8A89F6EEF7E9E2D9D6C7DE2722BD22EB8&BT_TRF=118447&BT_WAV=20120322&BT_LNK=10151&BT_EML=5543&BT_VAR=1583'
--SET @str = 'BT_XYZ=1&referrerID=60081270&campID=6299&BTData=C0216787861617459544B4BBCB6A9BFA59F9B8A89F6EEF7E9E2D9D6C7DE2722BD22EB8&BT_TRF=118447&BT_WAV=20120322&BT_LNK=10151&BT_EML=5543&BT_VAR=1583'

PRINT @str

SELECT SUBSTRING(@str,CHARINDEX('&campID=',@str),CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str)-3)


SELECT LEN(SUBSTRING(@str,CHARINDEX('&campID=',@str),CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str)))

SELECT REPLACE(@str,(SUBSTRING(@str,CHARINDEX('&campID=',@str),
                     CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str))),(SUBSTRING(@str,CHARINDEX('&campID=',@str),
                     CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str)-3)))

SELECT LEN(SUBSTRING(@str,CHARINDEX('&campID=',@str),CHARINDEX('&BTData=',@str)-CHARINDEX('&campID=',@str)))
0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 37787784
thanks this will do for now..
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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