sql server - Concatenate and format text in SQL -


i need concatenate city, state , country columns city, state, country.

this code:

select city + ', ' + state + ', ' + country outputtext places 

however, because city , state allow null (or empty) value, happen is, (for example) if city null/empty, output , iowa, usa; or state empty, output seattle, , usa

is there anyway can format output , remove "unnecessary" commas?

edited: because of requirements, should not use other mean (such pl/sql, store procedure) etc., has plain sql statement

select   isnull(city, '') +   case when isnull(city, '') != '' ', ' else '' end +   isnull(state, '') +    case when isnull(state, '') != '' ', ' else '' end +   isnull(country, '') outputtext     places 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -