DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples > SQL

Discuss "Concatenation of Column data function...." in the SQL forum.

SQL - Post your SQL code samples here.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old March 17th, 2008, 12:32 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 37
Thanks: 1
Thanked 2 Times in 1 Post
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

Default Concatenation of Column data function....

Here is a little sample of a function to get multiple values from a database into a comma delimited string.

Code:
--Variable Declaration 
DECLARE @Count int 
DECLARE @i int 
DECLARE @String varchar(8000) 
DECLARE @NameString varchar(100) 

-- A table to store all the results that match criteria.  Change the criteria by altering the select statement. 
DECLARE @NameTable TABLE 
(    
    [id] int IDENTITY(1,1),    
    [engineer] varchar(100)
) 
INSERT INTO
    @NameTable 
SELECT ENGINEER 
FROM tbl_engineer 
WHERE (ACTIVE = 1) AND (HASHHT = 1) AND (RDTLOGGEDON = 0) 

SET @i = 1 -- initialize the counter to increment 
SET @Count = (SELECT COUNT(*) FROM @NameTable) -- determine when to stop the loop 
SET @String = '' -- a starting value for the string to return. 

-- While loop... while there are still records in the table, add the engineer to the string. 
WHILE @i <= @Count 
BEGIN
    SET @NameString = (SELECT engineer FROM @NameTable WHERE id = @i)
    SET @String = @String + @NameString + ', '
    SET @i = @i +1
END 

-- Remove the last comma from the string. 
SELECT SUBSTRING(@String, 1, LEN(LTRIM(RTRIM(@String))) - 1)
There are other ways of course, but I thought this might help any of you out there that need to do this.

Comments on this post
jmurrayhead agrees: coolio
richyrich agrees: Good work Laura...
sbenj69 agrees: nice
Reply With Quote
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples > SQL

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 08:27 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46