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 «««12345»»»

Concatenation Cursor Expand / Collapse
Author
Message
Posted Tuesday, February 16, 2010 3:29 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, July 17, 2014 4:14 PM
Points: 153, Visits: 257
bitbucket-25253 (2/16/2010)

Juan de Dios (2/16/2010)
--------------------------------------------------------------------------------
This script is very useful when all the rows are not NULL, but if you modify the insert of Mark with NULL, the script only returns Bill.

insert into @table values (4, NULL,'M')

Result: Bill

Where John goes???


stewartc-708166

To cate for this, an aditional clause should be added to the predicate, viz:
AND Name IS NOT NULL


In addition or an alternative to stewartc excellent suggestion, you can in effect negate the presence of the NULL by using an ORDER BY [name] clause

More interesting try the code with a BLANK name, without the ORDER BY clause it appears in the list of names as , ,
- with the ORDER BY clause it appears as a leading comma before Bill.

sanbornd
An EXCELLENT QOD thanks for devising it and submitting, may I encourage you to submit additional QODs


If you used this in the where clause AND ISNULL([name],'') <> '' you will negate both of the issues above.

A very good QOD which I have used a couple of times, but should be able to use more often

Post #866537
Posted Wednesday, February 17, 2010 1:16 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: Thursday, March 27, 2014 8:16 AM
Points: 534, Visits: 282
NICE question!

I learnt something new today.


Kelsey Thornton
MBCS CITP
Post #866800
Posted Wednesday, February 17, 2010 1:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 28, 2011 12:09 PM
Points: 7, Visits: 47
declare @table table (id int, [name] varchar(10), gender char(1))
declare @MaleNames table (id int identity(1,1), [name] varchar(10))

insert into @table values (1,'John','M')
insert into @table values (2,'Sue','F')
insert into @table values (3,'Jane','F')
insert into @table values (4,'Mark','M')
insert into @table values (5,'Bill','M')

insert into @MaleNames SELECT [name] from @table where gender = 'M'

SELECT * FROM @MaleNames

Post #866805
Posted Wednesday, February 17, 2010 2:00 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, July 17, 2014 5:17 AM
Points: 159, Visits: 429
A variation on this that is really useful for dynamic SQL generation is getting a column list for a table - I use variations of the code below (variations include excluding identity columns for inserts to an identity table):

      SET @Cols = ''
SELECT
@Cols = @Cols + CASE WHEN ORDINAL_POSITION = 1 THEN '' ELSE ',' END + COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_CATALOG=@tableCatalog
AND TABLE_SCHEMA=@tableSchema
AND TABLE_NAME=@tableName






James Horsley
Workflow Consulting Limited
Post #866809
Posted Wednesday, February 17, 2010 3:31 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 1:45 AM
Points: 2,826, Visits: 3,866
brad.green (2/16/2010)
In addition to the comment about the missing order by, the result of this is also dependent on the CONCAT_NULL_YIELDS_NULL option. If this option is OFF, the argument to IsNull() will NOT be null, and therefore the result would be: ", John, Mark, Bill" -- leading comma.

Good point! Was about to "complain" as well, but then saw you already caught this.

The settings-independend version would be something like this:
SELECT @MaleNames = 
CASE WHEN @MaleNames IS NULL THEN '' ELSE @MaleNames + ', ' END + [name]
FROM @table
WHERE gender = 'M'



Best Regards,
Chris Büttner
Post #866849
Posted Wednesday, February 17, 2010 7:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, January 29, 2014 10:01 AM
Points: 3, Visits: 54
I thought the answer would be the 3 mens names but when I ran it it returned null several times on a sql 2005 server? I then reset all the query options to default and connected to another server and it ran correctly there, so I tried it back on the original server it now works there too so i guess some settings options in the query had resulted in the null result.
Post #867062
Posted Wednesday, February 17, 2010 7:52 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 2, 2011 3:22 AM
Points: 1,227, Visits: 154
I tried the following:
declare @table table (id int, [name] varchar(10), gender char(1))
declare @MaleNames varchar(255)

insert into @table values (1,'John','M')
insert into @table values (2,'Sue','F')
insert into @table values (3,'Jane','F')
insert into @table values (4,'Mark','M')
insert into @table values (5,'Bill','M')
insert into @table values (6, null,'M')
insert into @table values (7,'Dingbat','M')

select @MaleNames = IsNull(@MaleNames + ', ','') + [name] from @table where gender = 'M' ORDER BY name

Select @MaleNames as MalePatients

Output is:
Bill, Dingbat, John, Mark
Post #867097
Posted Wednesday, February 17, 2010 11:16 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 4, 2011 7:20 AM
Points: 977, Visits: 1,499
Its a different look at Quirky Update.

Very elegant in my mind.


Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #867328
Posted Wednesday, February 17, 2010 3:10 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, January 3, 2014 10:59 AM
Points: 626, Visits: 836
I usually use COALESCE but ISNULL is easier to spell.

Thanks!

Good question, btw. lots of people haven't seen this trick and it's incredibly useful for generating D-SQL and executing it.


Craig Outcalt



Tips for new DBAs: http://www.sqlservercentral.com/articles/Career/64632
My other articles: http://www.sqlservercentral.com/Authors/Articles/Craig_Outcalt/560258
Post #867560
Posted Thursday, February 18, 2010 1:24 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: Thursday, March 27, 2014 8:16 AM
Points: 534, Visits: 282
SQLBOT (2/17/2010)
I usually use COALESCE but ISNULL is easier to spell.


LOL!


(feels a strange urge to go hide behind the sofa...)


Kelsey Thornton
MBCS CITP
Post #867791
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse