wetmatter nonsense

let's get random 
Filed under

case expressions

 

sqlshots: SELECT-TRIM-CASE-CONCAT

In this particular scenario I needed to concatenate a set of fields into a single column. So essentially I needed to trim all the leading zeros from the street number field first (via my udf_TrimLeadingZeros), then pieced the street number, pre-direction, street name and suffix all into one field. However, every row did not have a pre-direction or a suffix; nonetheless, I needed everything formatted (evenly spaced between words) and displayed as a single string.

So I figured I would use a CASE expression (http://bit.ly/PiYQF) in conjunction with a String Concatenation (http://bit.ly/FJtq9) to achieve my end result. Typically I see a period (.) appended to the pre-direction, but in this case it wasn't required. However if you do need it simply add a period to line 14 between the ticks '. ' of the attached select script (see pre-direction with periods screen shot for details).

Pre-Direction without periods

Pre-Direction with periods

Click here to download:
CREATE.udf_TrimLeadingZeros_v0.01.sql (0 KB)

Click here to download:
SELECT.Trim-Case-ConCat_v0.01.sql (0 KB)

Loading mentions Retweet
Filed under  //   case   case expressions   select   select case   sql   sql server 2005   sql server management studio   ssms   string concatenation   tsql  
Posted by Samson Loo 

Comments [0]