Why can you use"-+-+-+" in tsql?

  • When I was trying what kind of construction TSQL in SQL 2008 would allowed I came at this stupid construction.

    the problem is "-+-+-+"construction

    declare @i bigint = 6

    set @i -= @i

    print @i

    SET @i = 6

    set @i -=-@i

    print @i

    SET @i = 6

    set @i -=+@i

    print @i

    SET @i = 6

    set @i -=++@i

    print @i

    SET @i = 6

    set @i -=-+-@i

    print @i

    print @i

    SET @i = 6

    set @i -=-+-+@i

    print @i

    SET @i = 6

    set @i -=-+-+-@i

    print @i

    the result was

    0

    12

    0

    0

    0

    0

    0

    12

    My question is where can I find something about this construction "-+-+-" and is there any use for this?

    The only use I can think of is creating dynamic TSQL command

    where you do not want to have " '-' + '-' " in your string but " '-+' + '-+' "

    But i never had to use that.

  • I don't understand the question?

    All you're doing with the -+- operators is simple math, a + adds an equivalent value and a - takes it away. +++@param is the same as 3*@param.

    What business or data problem are you trying to solve or are you just demo'ing the behavior?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • It's not likely anyone uses this, but it's a valid extension of the operators. You might want two of them (or 3) for some reason, but you typically wouldn't use 5 or 6. However putting in something in the parser to check for this wouldn't make sense.

  • Grant Fritchey (11/3/2008)

    All you're doing with the -+- operators is simple math, a + adds an equivalent value and a - takes it away. +++@param is the same as 3*@param.

    no, because the "+" is not, in this context, a math operator but a sign. so +++@param is the same as @param.

  • Alexander Kovacs (11/3/2008)


    Grant Fritchey (11/3/2008)

    All you're doing with the -+- operators is simple math, a + adds an equivalent value and a - takes it away. +++@param is the same as 3*@param.

    no, because the "+" is not, in this context, a math operator but a sign. so +++@param is the same as @param.

    Oh, sorry, I missread it, you're right. But the question remains, why would you do this. I see the last example, @param -= -+-+-@param is the same as @param -= -@param. Sometimes a programming language lets you do something confusing or counter-productive, but that doesn't mean you should.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant Fritchey (11/3/2008)Sometimes a programming language lets you do something confusing or counter-productive, but that doesn't mean you should.

    dang, there goes my whole philosophy of programming . . .

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • jcrawf02 (11/4/2008)


    Grant Fritchey (11/3/2008)Sometimes a programming language lets you do something confusing or counter-productive, but that doesn't mean you should.

    dang, there goes my whole philosophy of programming . . .

    Unfortunately, you're not alone!

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant Fritchey (11/3/2008)


    I don't understand the question?

    All you're doing with the -+- operators is simple math, a + adds an equivalent value and a - takes it away. +++@param is the same as 3*@param.

    SQL doesn't (yet) support the @i++ (or ++@i) constructs that C++,C# and others do. It does support the +=, -= and the like.

    So

    SET @i -=@i is the same as SET @i = @i-@i (ie 0)

    SET @i +=@i is the same as SET @i = @i+@i

    If you start tossing - (negation, not subtraction) into the mix, it gets interesting

    set @i -=-+-+-@i can be expanded as SET @i -= (-1)*(1)*(-1)*(1)*(-1)*@i because the - is a negation, not a subtraction. It's leading the variable. If it were between two expressions, it would be a subtraction

    -1*-1 = +1, so two of the negatives in there cancel out and that can be collapsed down to

    SET @i -= -@i

    or

    SET @i = @i - (-@i)

    again the two negatives cancel out and we're left with

    SET @i = @i + @i

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • SS 2005 doesn't have the '-=' operator, but if you trydeclare @i int

    set @i=6

    set @i=-+-+-@i

    print @i you get -6, so it's consistent with 2K8.

    I think the parser is just treating -+-+-@i as -(+(-(+(-@i)))). I.e. it's just applying unary operators in order.declare @i int

    SET @i = 6

    set @i =- + + + + + - - - - - + - @i -- this is OK too

    print @i -- -6

    Obviously, '--' would start a comment but '- -' is treated as '+' 🙂

    Derek

  • I'm well and thoroughly confused by what business problem we're hoping to solve here. Or are we just engaged on an entertaining, though useless, mind excercise?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant Fritchey (11/4/2008)


    I'm well and thoroughly confused by what business problem we're hoping to solve here. Or are we just engaged on an entertaining, though useless, mind excercise?

    http://www.sqlservercentral.com/articles/Editorial/61757/

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • Grant Fritchey (11/4/2008)


    I'm well and thoroughly confused by what business problem we're hoping to solve here. Or are we just engaged on an entertaining, though useless, mind excercise?

    Not every discussion here relates directly to a specific business problem, since it's a discussion forum for T-SQL not business problems.

    I also don't think the discussion is useless as there is a potential problem that since 2 consecutive hyphens separated by a space are accepted as valid code, it's possible to mistype a '--' comment and get incorrect behaviour.declare @i int

    -- - - - - - - - - - - - - - - - - - - - - - - --

    set @i = 6

    -- - - - - - - - - - - - - - - - - - - - - - - -- 1. -- This looks pretty

    - - - - - - - - - - - - - - - - - - - - - - - - - 2. -- But is dangerous

    -- - - - - - - - - - - - - - - - - - - - - - - -- 3. -- Because the result

    - - - - - - - - - - - - - - - - - - - - - - - - - 4. -- Is not what you expect

    print @i -- 0!

    Of couse, you'd hope that someone would notice the syntax highlighting was wrong! 🙂

    Derek

  • jcrawf02 (11/4/2008)


    Grant Fritchey (11/3/2008)Sometimes a programming language lets you do something confusing or counter-productive, but that doesn't mean you should.

    dang, there goes my whole philosophy of programming . . .

    I prefer to call it "obfuscation"....:):P

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Derek Dongray (11/4/2008)


    Grant Fritchey (11/4/2008)


    I'm well and thoroughly confused by what business problem we're hoping to solve here. Or are we just engaged on an entertaining, though useless, mind excercise?

    Not every discussion here relates directly to a specific business problem, since it's a discussion forum for T-SQL not business problems.

    I also don't think the discussion is useless as there is a potential problem that since 2 consecutive hyphens separated by a space are accepted as valid code, it's possible to mistype a '--' comment and get incorrect behaviour.declare @i int

    -- - - - - - - - - - - - - - - - - - - - - - - --

    set @i = 6

    -- - - - - - - - - - - - - - - - - - - - - - - -- 1. -- This looks pretty

    - - - - - - - - - - - - - - - - - - - - - - - - - 2. -- But is dangerous

    -- - - - - - - - - - - - - - - - - - - - - - - -- 3. -- Because the result

    - - - - - - - - - - - - - - - - - - - - - - - - - 4. -- Is not what you expect

    print @i -- 0!

    Of couse, you'd hope that someone would notice the syntax highlighting was wrong! 🙂

    heck you are right... but the whole thing does not make sense.

    "Keep Trying"

Viewing 14 posts - 1 through 13 (of 13 total)

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