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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 String Concatination Help

Author  Topic 

erhimc
Starting Member

8 Posts

Posted - 2006-01-19 : 17:39:49
Hello All,

Im very new to programming with SQL Server, and I need some code to do this:

I have a table... a BIG table that has names, and a field for every STATE (name, ak, al, ar, az, ca...) you get the idea. I need to generate a string that will look at all of those state fields, and if they are not null fields then add them to a variable?... that can be displayed. Does that make sense

So each could have all 50, or they could have none, but the string for each would only display the fields which are not NULL..

The output needs to be something like this

Name, ak, ca
Name, al, ar, az, ca
Name, ak, az, ca
.
.
.

I hope Im describing that ok. none the less, I am a novice at this, and I need to do this rather quiclky, so I thought I would drop it out here to see if anybody has an idea.

Thanks in Advance,
Ed

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-01-19 : 17:49:37
SELECT COLEASCE(FirstState + ',', '') + COLEASCE(SecondState + ',', '') + ... COLEASCE(LastState, '') AS States
FROM <Your Table>
WHERE <some where clause>

This will work but if the last state is null in a row then you will have a trailing comma.

Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-01-19 : 17:51:27
Also you might want to normalize this table. There should be a 1 to many relationship between your main table and the states (since 1 row in your table can belong to 0, 1, or more states). This would get rid of those ugly NULLS in your database.

Dustin Michaels
Go to Top of Page

erhimc
Starting Member

8 Posts

Posted - 2006-01-19 : 18:00:03
Thank you for the input Dustin.

I would LOVE to redo this entire database, but unfortunately I have inherited a beast, and the 'rest' of the application depends on the current poorly designed database.

I have my hands full getting it cleaned up.

E.
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-01-19 : 18:13:31
quote:
Originally posted by erhimc

Thank you for the input Dustin.

I would LOVE to redo this entire database, but unfortunately I have inherited a beast, and the 'rest' of the application depends on the current poorly designed database.

I have my hands full getting it cleaned up.

E.



If you only access the database thru stored procedures / views you should be able to change the database however you want. Because if you change the tables in your database you just need to change the queries in your stored procedures / views.
Go to Top of Page

erhimc
Starting Member

8 Posts

Posted - 2006-01-19 : 18:15:11
Dustin,

When I use your code idea...

I get this error message:

Server: Msg 195, Level 15, State 10, Line 3
'COLEASCE' is not a recognized function name

E.
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-01-19 : 18:20:06
I misspelled the function name, try COALESCE instead.
Go to Top of Page

erhimc
Starting Member

8 Posts

Posted - 2006-01-19 : 18:25:48
quote:
Originally posted by DustinMichaels

I misspelled the function name, try COALESCE instead.



No wonder I could not find it...
It creates the string, but I need it to look more like this:

Name, ak, ca
Name, al, ar, az, ca
Name, ak, az, ca

So if the field is not NULL, and the db field is CA, it needs to show up in the list as 'CA'

E.
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-01-19 : 19:18:59
quote:
Originally posted by erhimc

quote:
Originally posted by DustinMichaels

I misspelled the function name, try COALESCE instead.



No wonder I could not find it...
It creates the string, but I need it to look more like this:

Name, ak, ca
Name, al, ar, az, ca
Name, ak, az, ca

So if the field is not NULL, and the db field is CA, it needs to show up in the list as 'CA'

E.




How are you storing the values in the table are they boolean values? Maybe this would work.

SELECT CASE WHEN Arkansas IS NOT NULL THEN 'ak,' ELSE '' END + CASE WHEN Arizona IS NOT NULL THEN 'az,' ELSE '' END + ... CASE WHEN Wyoming IS NOT NULL THEN 'wy' ELSE '' END AS States
FROM <Your Table>
WHERE <some where clause>
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-20 : 01:16:45
Are you looking for something like this?
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -