STUFF Function

  • Ugh -- they did it on purpose? For God's sake why? It violates all good database protocol? That really needs to be flagged heavily in BOL.

  • I suppose we'd have to ask the folks at Sybase why it was done that way. In the mean time, don't believe all the Sybase doc for what happens with an empty string. In SqlServer

    select stuff('abcdef', 2, 3, '') as MyStuff

    and

    select stuff('abcdef', 2, 3, NULL) as MyStuff

    produces the same result, as opposed to the blank character that Sybase inserts when using an empty string. I'm sure there is code from 1904 in there that does all this 🙂

  • I just keep wondering how I never heard of STUFF in 12 years working with SQL...

    Maybe because people avoid using it for this reason??

    Peter Trast
    Microsoft Certified ...(insert many literal strings here)
    Microsoft Design Architect with Alexander Open Systems

  • jeff.mason (5/18/2011)


    Ugh -- they did it on purpose? For God's sake why? It violates all good database protocol? That really needs to be flagged heavily in BOL.

    When you say they I hope you mean Syabase, Oracle, and Microsoft.

    I can't speak to eactly what was going through there minds, but maybe it was SQL 98? or even SQL 92...

    If you read the other RDBMS systems docuemtation you should see the use of the wording "NULL string" and NULL there also.

    My best guess is that this was done so the STUFF function worked thes same in more than one place.

    I do remember learning in classes at school the differance between "" (NULL string) and NULL (NULL value or UNKNOWN) and the behavior of STUFF being used as an example.

  • jeff.mason (5/18/2011)


    Ugh -- they did it on purpose? For God's sake why? It violates all good database protocol? That really needs to be flagged heavily in BOL.

    Yes, I found the same result yesterday the first time I experimented with it.

    Strange that '' and NULL do the same thing...

    Peter Trast
    Microsoft Certified ...(insert many literal strings here)
    Microsoft Design Architect with Alexander Open Systems

  • brazumich (5/18/2011)


    I'm sure there is code from 1904 in there that does all this 🙂

    LOL... 1974 might be closer to the time.

    Or even 1989 when C89 came out...

    It is very old behavior.

    Those of you that recognize these dates might be able to guess I have been researching if this behavior of STUFF started in straight C, ANSI C, or C++.

    You are right about STUFF inserting of a single space string for NULL, that is a SYBASE thing only. It comes from some other old behavior.

  • Upon further research, I found this tidbit:

    "This is not true for all database implementations. In an Oracle RDBMS for example NULL and the empty string are considered the same thing and therefore 'Fish ' || NULL || 'Chips' results in 'Fish Chips'." (concatenation)

    From wiki http://en.wikipedia.org/wiki/Null_(SQL) (yeah, I know, it's wiki)

    So our assumptions about NULL may not be completely correct? If this behavior has existed in SQL and Oracle for years, we were just unaware of the inconsistency?

    Always something to learn...

    Peter Trast
    Microsoft Certified ...(insert many literal strings here)
    Microsoft Design Architect with Alexander Open Systems

  • SanDroid (5/18/2011)


    Carlton Leach (5/18/2011)


    Another bug vote here, and another wrong answer for me.

    NULL is NULL...fullstop. You never know what NULL is so regardless if you insert, concatenate, stick it somewhere in the string: the whole string is (SHOULD BE!) now unknown.

    My guess is anyone who got this right either ran the commands first, read the question wrong, of has bumped into this before.

    Carlton.

    Or has used STUFF for the 11+ years it has worked this way... :hehe:

    http://www.mssqltips.com/tip.asp?tip=1026

    I can say the documentation used to be better.

    That link doesn't say what STUFF does with a NULL last argument. So how can you say it tells us it worked that way for however many years, when it's the result of a NULL last argument that's at issue in this converstaion?

    Tom

  • Toreador (5/17/2011)


    Koen Verbeeck (5/16/2011)


    And the explanation doesn't mention why inserting NULL in another string doesn't result in NULL, but in an empty space instead.

    That's what got me.

    Surely it's a bug?

    select 'Vinay, ' + NULL + ', Amit'

    returns NULL, so I'd have thought

    select STUFF('Vinay, Vijay, Amit',8,5,NULL)

    should do the same?

    That's what got me, too.

    I thought concatenating NULL would yield NULL,

    but it seems like it applies Coalesce(..., '') or IsNull(..., '') to the last argument before concatenating it. This behavior is not consistent with that of CharIndex(expression1 ,expression2 [ , start_location ]), for example, which returns NULL if any of the arguments IS NULL.

  • Tom.Thomson (5/17/2011)


    I've submitted a connect item[/url] suggesting this behavious be changed.

    Closed "as Won't Fix" with the reason being "Backward Compatibility Issues"...Booooo!

    I guess we can officially call STUFF a legacy function 🙂

    Anyone have insight into what the "ANSI SQL OVERLAY function" is?

    From Umachandar's response:

    We might consider adding the ANSI SQL OVERLAY function in the future based on more customer feedback...

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Nice question, thanks! I do love STUFF. 🙂

  • opc.three (5/23/2011)


    Tom.Thomson (5/17/2011)


    I've submitted a connect item[/url] suggesting this behavious be changed.

    Closed "as Won't Fix" with the reason being "Backward Compatibility Issues"...Booooo!

    I guess we can officially call STUFF a legacy function 🙂

    Anyone have insight into what the "ANSI SQL OVERLAY function" is?

    From Umachandar's response:

    We might consider adding the ANSI SQL OVERLAY function in the future based on more customer feedback...

    OK, the wording through me off...I thought Umachandar was alluding to a new server option that could be SET, but in fact OVERLAY is a scalar function defined by in the ANSI SQL Standard that does not yet exist in SQL Server. OVERLAY would do the same work as STUFF but could be implemented to properly handle NULL inputs. This was the only article I could find that defined it:

    http://users.atw.hu/sqlnut/sqlnut2-chp-4-sect-4.html#sqlnut2-CHP-4-TABLE-9

    In case the article moves:

    OVERLAY

    The OVERYLAY function embeds one string into another and returns the result.

    SQL2003 Syntax

    OVERLAY(string PLACING embedded_string FROM start[FOR length])

    If any of the inputs are NULL, the OVERLAY function returns a NULL. The embedded_string replaces the length characters in string starting at character position start. If the length is not specified, then the embedded_string will replace all characters after start in string.

    DB2, MySQL, Oracle, and SQL Server

    These platforms do not have support for the OVERLAY function. You can simulate the OVERLAY function on these platforms by using a combination of SUBSTRING and the concatenation operator.

    PostgreSQL

    PostgreSQL supports the ANSI standard for OVERLAY.

    Examples

    This is an example of how to use the OVERLAY function:

    /* SQL2003 and PostgreSQL */

    SELECT OVERLAY('DONALD DUCK' PLACING 'TRUMP' FROM 8) FROM NAMES;'DONALD TRUMP'

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Tom.Thomson (5/18/2011)


    SanDroid (5/18/2011)


    Carlton Leach (5/18/2011)


    Another bug vote here, and another wrong answer for me.

    NULL is NULL...fullstop. You never know what NULL is so regardless if you insert, concatenate, stick it somewhere in the string: the whole string is (SHOULD BE!) now unknown.

    My guess is anyone who got this right either ran the commands first, read the question wrong, of has bumped into this before.

    Carlton.

    Or has used STUFF for the 11+ years it has worked this way... :hehe:

    http://www.mssqltips.com/tip.asp?tip=1026

    I can say the documentation used to be better.

    That link doesn't say what STUFF does with a NULL last argument. So how can you say it tells us it worked that way for however many years, when it's the result of a NULL last argument that's at issue in this converstaion?

    Been waiting to answer just that question. I will give you the same answer I was given... You should always test any code. Test it a hundred times if you dont know exactly how something works or have not used it before.

    I know it sounds harsh, but this is how STUFF is. STUFF has always been this way and STUFF will continue to be this way. STUFF is everywhere, but STUFF does not act the same everywhere. That is just how STUFF and the world works. If you do not understand STUFF, or the documentation for STUFF, you should test your STUFF. Afterwards take a look again at the documentation and STUFF. If you realy learned some STUFF, you should understand STUFF and the differances of other STUFF better. Hopefully you can find the Humor in how STUFF is. Most people that can't laugh at STUFF have a harder life than others.

  • SanDroid (5/24/2011)


    Tom.Thomson (5/18/2011)


    SanDroid (5/18/2011)


    Carlton Leach (5/18/2011)


    Another bug vote here, and another wrong answer for me.

    NULL is NULL...fullstop. You never know what NULL is so regardless if you insert, concatenate, stick it somewhere in the string: the whole string is (SHOULD BE!) now unknown.

    My guess is anyone who got this right either ran the commands first, read the question wrong, of has bumped into this before.

    Carlton.

    Or has used STUFF for the 11+ years it has worked this way... :hehe:

    http://www.mssqltips.com/tip.asp?tip=1026

    I can say the documentation used to be better.

    That link doesn't say what STUFF does with a NULL last argument. So how can you say it tells us it worked that way for however many years, when it's the result of a NULL last argument that's at issue in this converstaion?

    Been waiting to answer just that question. I will give you the same answer I was given... You should always test any code. Test it a hundred times if you dont know exactly how something works or have not used it before.

    I know it sounds harsh, but this is how STUFF is. STUFF has always been this way and STUFF will continue to be this way. STUFF is everywhere, but STUFF does not act the same everywhere. That is just how STUFF and the world works. If you do not understand STUFF, or the documentation for STUFF, you should test your STUFF. Afterwards take a look again at the documentation and STUFF. If you realy learned some STUFF, you should understand STUFF and the differances of other STUFF better. Hopefully you can find the Humor in how STUFF is. Most people that can't laugh at STUFF have a harder life than others.

    Unfortunately that tells us that things should be checked by testing. It doesn't tell us why you claimed that the page you referenced said something it quite clearly didn't say. I suggest you read the page again to test whether it actually says what you claimed it said - apply your own "check everything by testing" to your own assertion (which certainly needs testing, as it was completely wrong). 😀

    Tom

  • I think (before there is some serious bloodletting here), that perhaps what M$ needs to do is update the documentation so that it matches the functionality that was inherited/borrowed/etc from Sybase. We already know they ain't gonna change how it works, so doc is the next best thing.

    Perhaps Microsoft could just copy/plagarize the explanation pointed to by an earlier poster; I think it's on the third or 4th page of comments.

    Oh, and to SanDroid, I personally would like to have the right STUFF; I could be an astronaut then 😀

Viewing 15 posts - 46 through 60 (of 64 total)

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