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 2008 Forums
 Transact-SQL (2008)
 Full Address column

Author  Topic 

kpgraci
Yak Posting Veteran

68 Posts

Posted - 2011-09-26 : 16:47:37
I have a table with first, last, addr1, addr2, city, state, zip and zip4.

I want a query that will return a column called FullAddr.

So I could do this:

SELECT [First] + ' ' + [Last] + ... AS FullAddr WHERE...

One problem is I need to have CRLF's delimite each line, how would I do that in a query.

My second and bigger problem, I don't want a line for Addr2 if it is not answered (blank or null) and I don't want a hyphen between zip and zip4 if zip4 is unanswered. (the addr2 issue is more important)

Can this be done?




kpg

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-26 : 16:53:44
Why do you want to format this data in T-SQL? Why not return the raw values to the application and have the application do the formatting?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-27 : 00:45:30
for dealing with blank/null address lines you can do like

SELECT [First] + ' ' + [Last] + COALESCE(NULLIF(Addr2,'')+CHAR(13) + CHAR(10),'')+... zip+COALESCE('-'+nullif(zip4,''),'') AS FullAddr WHERE...


the CRLF should be implemented at your front end as far as possible


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kpgraci
Yak Posting Veteran

68 Posts

Posted - 2011-09-28 : 09:19:39
Resolved

tkizer's suggestion is best, I'm sure, I'm binding directly to a dataset so I just need to figure out how to format with that setup, I'm sure it can be done.

thx visakh16 for the CRLF example.

kpg
Go to Top of Page
   

- Advertisement -