Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to add multiple Case or If/then to Trigger

Author  Topic 

scholin
Yak Posting Veteran

56 Posts

Posted - 2010-02-09 : 00:17:46
I am not sure if I need a calculated field, a Trigger or a Procedure and I am not sure if I should use Case or If/Then statements but here is what I am trying to accomplish.

I need a function that will look up lets say 4 varchar fields: muscle1, muscle2, muscle3, muscle4 (there will actually be about 10 more). These fields are in a table called dbo.muscles.

I need this function to first see if the each field is not blank. If it is not blank, then it needs to add it to calculated field called muscle_string, add a comma and space .. . . thus making a long string properly formatted IDEALLY like the following example output: "Opponens Policis, Deltoid Muscle, Tricep Muscle and Trapezuis Muscle."

This is all in the same table. I would just like to have this calculated in SQL and availbe as field within dbo.muscles.

What is the best way/syntax etc to accomplish this?

Thank you for any help!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-02-09 : 00:24:48
if you are using SQL 2005 / 2008 you can refer to concatenate records without UDF

or for SQL 2000, Rowset string concatenation: Which method is best ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

scholin
Yak Posting Veteran

56 Posts

Posted - 2010-02-10 : 08:01:54
Thank you for responding KH - I am using sql 2008 standard. This is working with just one table. People will enter various muscles from a web site into this table and I need to create a function that will output a concatenated string (ideally properly formatted).


I know there are several steps here to look for the fields with data and then to add them to a string with commas and ideally and "and" before the last word in the list.


Just not sure how to set this up.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-02-10 : 08:20:39
have you take a look at the links, since you are using SQL 2008, you can use the method describe here : concatenate records without UDF


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-10 : 08:21:00
quote:
Originally posted by scholin

Thank you for responding KH - I am using sql 2008 standard. This is working with just one table. People will enter various muscles from a web site into this table and I need to create a function that will output a concatenated string (ideally properly formatted).


I know there are several steps here to look for the fields with data and then to add them to a string with commas and ideally and "and" before the last word in the list.


Just not sure how to set this up.


do you mean you need to concatenate values from multiple tables together?

------------------------------------------------------------------------------------------------------
SQL Server MVP
Go to Top of Page
   

- Advertisement -