Solved

SQL syntax

Posted on 2006-11-14
3
310 Views
Last Modified: 2008-02-01
Hello,

I need to transpose the information in a table and substitue words for letter abreviations, basically taking sevral rows of data and putting it into one row/column.  

Here is a sample of the information in my DB currently...

[Bird ID]            [Defects]
-----------------------------------
365965544         Slight Legs
365965544         Toes
365965545         Slight Legs
365965545         Toes
365965549         Severe Legs
365965549         Severe Toes
365965551         Slight Legs
365965553         Slight Legs

Here is what i want to turn it into...

[Bird ID]            [Defects]
-----------------------------------
365965544         SL, T
365965545         SL, T
365965549         SL, ST
365965551         SL
365965553         SL

Thanks!

-Navicerts
0
Comment
Question by:Navicerts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 17942026
what are the exact rules for the transformation fo  [Defects] into the short version?


in short:

create function dbo.concate_itemid ( @birdid )
returns varchar(8000)
as
begin
  declare @res varchar(8000)
  select @res = coalesce( @res + ',' , '') + defects
  from yourtable where [bird id]= @birdid
  return (@res)
end


and use it like this

select [bird id], dbo.concate_itemid([bird id])
from yourtable
group by [bird id]
0
 
LVL 7

Author Comment

by:Navicerts
ID: 17942160
I think this will work great.  I can add a case statement to replace the string with letter abbreviations, unless you know a better way?

Here is what I got it is working good for me, Thank You!! (I need to add the rest of the letter replacements in)


create function dbo.concate_itemid ( @birdid int )
returns varchar(8000)
as
begin
  declare @res varchar(8000)
  select @res = coalesce( @res + ',' , '') + case when defect = 'Slight Legs' then 'SL' else defect end
  from Defects where [bird id]= @birdid
  return (@res)
end

GO

select [bird id], dbo.concate_itemid([bird id])
from defects
group by [bird id]


-Navicerts
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17942208
I would, instead of doing it in the code, have a "translation" table of the values and the replacements...

you might also try to do it automatically, by extracting all the characters after a space...

create function dbo.GetShortValue ( @d varchar(100) )
returns varchar(100)
as
begin
  declare @res varchar(100)
  declare @x int

  set @res = substring(@d, 1,1)
  set @x = 2
  while (@x > 0)
  begin
    set @x = charindex(@d, ' ' , @x)
    if (@x>0)
    begin
      set @res = @res + substring(@d, @x+1, 1)
      set @x = @x +2
    end      
  end      

  return (@res)
end

and use it like this:


create function dbo.concate_itemid ( @birdid int )
returns varchar(8000)
as
begin
  declare @res varchar(8000)
  select @res = coalesce( @res + ',' , '') + dbo.GetShortValue (defect )
  from Defects where [bird id]= @birdid
  return (@res)
end

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some very basic things about SQL Server filegroups.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question