Pass a string into SP and parse on space

  • 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?

  • 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.

  • Actually, it's being called from VB and a string variable being passed in is holding the value "ABC DEF GHI".

  • 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?

  • 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'

  • 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...

  • 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'

  • In your example, what would happen if you used double quotes?

  • 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

  • Yes that helps...but I'm trying to resolve this without touching the VB code.

  • j2cagle (5/9/2013)


    Yes that helps...but I'm trying to resolve this without touching the VB code.

    then we need to see the target proc and a sample call.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • j2cagle (5/9/2013)


    Yes that helps...but I'm trying to resolve this without touching the VB code.

    Have you tried passing it in with the single or double quotes? I think the single quotes should resolve the issue.

    You say you don't want to change the VB code, so how is the variable entered? Through a text box in the interface? From a file? From another database or query?

    What version of VB is this? (The answer doesn't matter much here, I'm just curious)

    --------------------------------------
    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

  • j2cagle (5/9/2013)


    Yes that helps...but I'm trying to resolve this without touching the VB code.

    I don't think you have a choice in the matter. The problem probably isn't within the SQL but within the VB code. It would help us to help you if you posted both the VB code and the SQL code so we could verify.

    Are you using the DTS.Variable.ToString to pass the string from the VB code to the SQL?

    Bevan's suggestion is the best (exactly what I would do). I.E., using CHARINDEX() and then LEFT() in the T-SQL code, but if that's not working for you, then your issue is in the VB code, not in the T-SQL.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • in both vb6 or VB.Net, you can split the string into elements, and take the first element, or you can use the built in string functions.

    'VB.Net: both return "ABC"

    Dim SomeString As String = "ABC DEF GHI"

    Dim val = SomeString.Substring(0, SomeString.IndexOf(" "))

    Dim arr() As String = "ABC DEF GHI".Split(" ")

    val = arr(0)

    'VB6

    Dim SomeString As String

    SomeString = "ABC DEF GHI"

    Dim val As String

    val = Left(SomeString, InStr(SomeString, " ") - 1) '-1 to remove the space

    Dim arr() As String

    arr = Split("ABC DEF GHI", " ")

    val = arr(0)

    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!

  • Brandie Tarvin (5/10/2013)


    j2cagle (5/9/2013)


    Yes that helps...but I'm trying to resolve this without touching the VB code.

    I don't think you have a choice in the matter. The problem probably isn't within the SQL but within the VB code. It would help us to help you if you posted both the VB code and the SQL code so we could verify.

    Are you using the DTS.Variable.ToString to pass the string from the VB code to the SQL?

    Bevan's suggestion is the best (exactly what I would do). I.E., using CHARINDEX() and then LEFT() in the T-SQL code, but if that's not working for you, then your issue is in the VB code, not in the T-SQL.

    I think the likely scenario is that the vb code is doing something like this:

    ocmd = ADODB.Command

    ocmd.CommandText = "EXEC dbo.[Procedure] " + sVariable

    ocmd.Execute()

    If you notice, that is building dynamic sql (very common in the VB6 world). With Numerics this is fine and not an issue. With strings it's a huge problem. So, really, until we get the actual VB code, we can't determine the cause.

Viewing 15 posts - 1 through 15 (of 24 total)

You must be logged in to reply to this topic. Login to reply