Passing Variable in SQL script

  • I am having trouble passing a variable into my sql statement. Best way of explaining is show a script.

    declare @extract varchar(10), @extract2 varchar(10)

    set @extract = '2,4'

    select SUBSTRING('Hello Paul', @extract)

  • there's two ways to do it Paul; the best way is to parameterize the integers:

    declare @extract varchar(10),@Start int,@length int

    set @Start =2,@length =4

    select SUBSTRING('Hello Paul', @Start ,@length )

    to do it the way you had it, you'd have to revert to dynamic sql, and you'll want to avoud that for something so simple.

    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!

  • Fantastic, thank you

Viewing 3 posts - 1 through 2 (of 2 total)

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