Concatenate multiple columns in SQL Server -


i have address information parsed several columns , i'm trying concatenate columns 1 no leading or trailing spaces using view. have tried ltrim , rtrim can't seem figure out how rid of leading/trailing spaces.

i using sql server 2008 r2. below sample of how our data stored. address column output i'm looking/hoping get.

thanks in advance!

houseno|predir|stname|suftype|sufdir|unittype|unitno|city    |state|zip  |address| 123    |      |main  |st     |      |        |      |anytown |ca   |55555|123 main st anytown, ca 55555 245    |      |3rd   |ave    |ne    |        |      |anytown |ca   |55555|245 3rd ave ne anytown, ca 55555 4675   |w     |elm   |dr     |      |        |      |anytown |ca   |55555|4675 w elm dr anytown, ca 55555 789    |e     |1st   |st     |      |apt     |5     |anytown |ca   |55555|789 e 1st st apt 5 anytown, ca 55555 432    |      |locust|blvd   |se    |        |      |anytown |ca   |55555|432 locust blvd se anytown, ca 55555 

what want combination of string concatenation , coalesce operator. coalesce returns first non-null value of comma-separated list of expressions.

example:

set concat_null_yields_null on; select houseno + coalesce(' ' + predir, '') + coalesce(' ' + stname, '') address yourtable 

will result in:

address 123 main 245 3rd 4675 w elm 789 e 1st 432 locust

note above requires concat_null_yields_null on. if unsure if on connection, call set concat_null_yields_null on before select. concat_null_yields_null off default unless has been set otherwise database (rare).

with concat_null_yields_null on, adding ' ' + predir result in null records have null in predir column, result in coalesce using next value, '', empty string , nothing.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -