Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««34567»»

STUFF Function Expand / Collapse
Author
Message
Posted Wednesday, May 18, 2011 9:00 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232, Visits: 1,046
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.
Post #1111069
Posted Wednesday, May 18, 2011 9:00 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, June 13, 2014 3:03 PM
Points: 594, 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
Post #1111070
Posted Wednesday, May 18, 2011 5:23 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 8:48 AM
Points: 8,739, Visits: 9,287
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...
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
Post #1111422
Posted Thursday, May 19, 2011 9:35 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:17 PM
Points: 30, Visits: 39
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.
Post #1111820
Posted Monday, May 23, 2011 4:29 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:34 AM
Points: 7,098, Visits: 12,606
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
Post #1113689
Posted Monday, May 23, 2011 5:11 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, June 16, 2014 9:38 AM
Points: 2,163, Visits: 2,189
Nice question, thanks! I do love STUFF.
Post #1113699
Posted Monday, May 23, 2011 5:50 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:34 AM
Points: 7,098, Visits: 12,606
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
Post #1113710
Posted Tuesday, May 24, 2011 7:35 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232, Visits: 1,046
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...
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.
Post #1114025
Posted Tuesday, May 24, 2011 10:38 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 8:48 AM
Points: 8,739, Visits: 9,287
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...
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
Post #1114223
Posted Tuesday, May 24, 2011 11:25 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, August 29, 2014 6:17 AM
Points: 421, Visits: 980
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
Post #1114261
« Prev Topic | Next Topic »

Add to briefcase «««34567»»

Permissions Expand / Collapse