SQL Clone
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
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
I'm wanting to send a string into a stored procedure, but only use the first characters up to the first space.

For example, if I pass in:
"ABC DEF GHI"

I'm only interested in "ABC".

How would I go about handling that in the SP? Currently, I'm setting up the parameter to be VARCHAR(MAX). However, when I pass in a string like above, I get an error:
Incorrect syntax near 'DEF'

Any ideas?
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39135 Visits: 38518
j2cagle (5/9/2013)
I'm wanting to send a string into a stored procedure, but only use the first characters up to the first space.

For example, if I pass in:
"ABC DEF GHI"

I'm only interested in "ABC".

How would I go about handling that in the SP? Currently, I'm setting up the parameter to be VARCHAR(MAX). However, when I pass in a string like above, I get an error:
Incorrect syntax near 'DEF'

Any ideas?


Are you using double quotes around the string or single quotes? If double quotes, that's the problem. If single quotes are being used, we don't have enough info to really answer your question.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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
Actually, it's being called from VB and a string variable being passed in is holding the value "ABC DEF GHI".
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
But, whether it's being executed from VB or another SP, it shouldn't make any difference to how the current SP should be written...should it?
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39135 Visits: 38518
j2cagle (5/9/2013)
But, whether it's being executed from VB or another SP, it shouldn't make any difference to how the current SP should be written...should it?


Try it with this: 'ABC DEF GHI'

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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
It's just a string variable in VB. There is no " or ' choice. It works fine when you just pass in "ABC", but when a space is introduced...that's when things crash and burn...
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39135 Visits: 38518
j2cagle (5/9/2013)
It's just a string variable in VB. There is no " or ' choice. It works fine when you just pass in "ABC", but when a space is introduced...that's when things crash and burn...


Sorry, I'm not a VB programmer, I am a SQL Server DBA/Developer.

In SSMS you would execute the stored proc like this:

exec dbo.MyProc 'ABC DEF GH'

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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
In your example, what would happen if you used double quotes?
Bevan Keighley
Bevan Keighley
Old Hand
Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)

Group: General Forum Members
Points: 358 Visits: 834
Hi,

If you want to split the value inside SQL you can use this:


declare @input varchar(max)
declare @delimiter varchar(1)
declare @index int
declare @corrected_input varchar(20)

set @input = 'ABC DEF GHI'
set @delimiter = ' '
set @index = charindex(@Delimiter, @input)

if @index != 0
set @corrected_input = left(@input, @index - 1)
else
set @corrected_input = @input

select @corrected_input





But it looks like there is something funny going on with your VB code. Probably the parameter is being passed wrong.

You could always split it in VB:


Dim inputString As String = "ABC DEF GHI"

' Returns an array containing "ABC", "DEF", and "GHI".
Dim inputArray() As String = Split(inputString)



then use the first value in the array. Does that help?


Regards,
Bevan Keighley
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
Yes that helps...but I'm trying to resolve this without touching the VB code.
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