Link to home
Start Free TrialLog in
Avatar of tommym121
tommym121Flag for Canada

asked on

SQL - conditionally insert space between fields

I have two fields that I would like to combine to form a name.  I would like to have a space in between. There should be a between the two fields if both fields has a valid string.  There should be no space before and after the combined string (Name).
How do I structure it?
Avatar of Jared_S
Jared_S

If it's a valid string or not will be determined by the data type of the column (I'm assuming varchar, nvarchar, etc here)

You can combine them with a space in between by stating:

select column1 + ' ' + column2

from ....

If you can post some data examples of what you'd consider  valid string, and what you wouldn't, there may be another way to write the condition.
SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
select ltrim(rtrim(firstname)) + ' ' + ltrim(rtrim(lastname)) from tablename

if your column datatype is varchar, char, or any other variant of these, then you can't prevent alphanumeric as well as special characters from entering the columns and they too will be considered as character strings.
Avatar of tommym121

ASKER

sameer_goyal,

How can I get rid of non print character from a column?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks