SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


STUFF Function


STUFF Function

Author
Message
SanDroid
SanDroid
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2816 Visits: 1046
brazumich (5/18/2011)
I'm sure there is code from 1904 in there that does all this Smile

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.
Peter Trast
Peter Trast
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1208 Visits: 655
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
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29922 Visits: 12598
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

Melsen Asllani
Melsen Asllani
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 51
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.
Orlando Colamatteo
Orlando Colamatteo
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51988 Visits: 14413
Tom.Thomson (5/17/2011)
I've submitted a connect item 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
UMG Developer
UMG Developer
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4644 Visits: 2204
Nice question, thanks! I do love STUFF. :-)
Orlando Colamatteo
Orlando Colamatteo
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51988 Visits: 14413
opc.three (5/23/2011)
Tom.Thomson (5/17/2011)
I've submitted a connect item 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
SanDroid
SanDroid
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2816 Visits: 1046
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.
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29922 Visits: 12598
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). :-D

Tom

Bob Razumich
Bob Razumich
SSC Eights!
SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)

Group: General Forum Members
Points: 840 Visits: 1223
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 :-D
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