Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pass a string into SP and parse on space


Pass a string into SP and parse on space

Author
Message
Sioban Krzywicki
Sioban Krzywicki
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 8067
j2cagle (5/10/2013)
By "lost" I mean...the code is out there running, and we have versions of the source code in multiple locations. These multiple locations don't appear to be the version that is out there running. So, I was attempting to catch and correct the issue in the called SQL.

Just to reiterate...

I'm just looking for a way, that when passed "ABC DEF GHI", I could handle it in SQL to simply look only at the value up to the first space.

If that can't be done, that's fine...I was just doing some investigation...


How is it being passed currently. How is that data getting into the VB program? Is it a text box in an interface or from a file or from another database. This is important if you want to do this without changing code in VB.

Once you get it in there, you'll want to use
SUBSTRING(@TheVariable, 1, CHARINDEX(' ', @TheVariable) - 1)



The error you're getting is because when the ABC DEF GHI gets to your stored proc, there's no single quotes around it, so DEF is considered the beginning of another statement.

--------------------------------------
When you encounter a problem, if the solution isn't readily evident go back to the start and check your assumptions.
--------------------------------------
It’s unpleasantly like being drunk.
What’s so unpleasant about being drunk?
You ask a glass of water. -- Douglas Adams
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14959 Visits: 38976
Brandie Tarvin (5/10/2013)
j2cagle (5/10/2013)
Yes, Say Hey Kid, it's very similar to what you posted. I agree with a previous post...the real issue is in the VB code. However, I wanted to determine if I could handle this situation within the SQL code. The VB code is potentially lost and not re-buildable, unfortunately.


Until you can get the VB code to push the correct value to SQL Server, there's nothing T-SQL can do to rectify the problem.

When you say "lost," what exactly do you mean? I assume there's a working version of it somewhere or you wouldn't be getting anything at all from it.


you are correct, in this case, it cannot be done, because the error is coming from the syntax parsing of the statement when it hits the server; it doesn't get "inside" the stored procedure where you could manipulate the parameters.

the code needs to change in vb6 to use a parameter instead of creating and executing a string; only if that was done would you be able to chop the passed values inside the procedure:

"EXEC dbo.[Procedure] " + sVariable
--becomes
EXEC dbo.[Procedure] @MyStringParameter



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478

...
Once you get it in there, you'll want to use
SUBSTRING(@TheVariable, 1, CHARINDEX(' ', @TheVariable) - 1)


...


Just in case if you will need to pass string without any space:


SELECT LEFT(val,ISNULL(NULLIF(CHARINDEX(' ', val) - 1,-1),LEN(val)))
FROM (VALUES ('ABC DEF GHI'),('WDR')) q(val)



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Steve Thompson-454462
Steve Thompson-454462
SSC Veteran
SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)SSC Veteran (289 reputation)

Group: General Forum Members
Points: 289 Visits: 2056
j2cagle (5/10/2013)

I'm just looking for a way, that when passed "ABC DEF GHI", I could handle it in SQL to simply look only at the value up to the first space.

If that can't be done, that's fine...I was just doing some investigation...


Don't think it can be done (at least not in any way I'm familiar with - which might not be setting the bar very high).

You're basically sending in the string without the required single quotes. SQL will consume up to the first space without complaining, but once it hits characters after that space it will throw an exception. So you need a fix at the point Procedure is being called.
j2cagle
j2cagle
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 22
Thanks everyone! What everyone is describing is exactly what is happening...with the space followed by more text error. I appreciate everyone's input on this...it was very educational for me!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search