SQL Server Computed Column Trim - sql

I am trying to add a computed column to an SQL database. The Computed Column Specification looks like this
BusinessName + ' ' + Surname + ' ' + FirstName
which works fine.
Often the BusinessName is blank so I want to Trim it
Trim(BusinessName + ' ' + Surname + ' ' + FirstName)
But when I do I get an error

You can use below logic,
First option: check if value is null, replace it by empty string, you will have leading empty space if BusinessName is null
isnull(BusinessName, '') + ' ' + Surname + ' ' + FirstName
or
Second option: check if BusinessName is empty or null, do not take into account if so. You won't have leading empty space if BusinessName is null or empty
case when isnull(BusinessName, ' ') <> ' '
then BusinessName + ' ' + Surname + ' ' + FirstName
else Surname + ' ' + FirstName
end as FullName

I think it depends by column datatypes and constraints. A more general approach could be the following. Starting from this if as a rule SURNAME and NAME (for instance) are not NULLABLE and cannot be blank, you can omit some of the functions:
DECLARE #T AS TABLE (BUSIN_NAME VARCHAR(20), SURNAME VARCHAR(20), NAME VARCHAR(20))
INSERT INTO #T VALUES(NULL, NULL, NULL);
INSERT INTO #T VALUES(NULL, 'A', NULL);
INSERT INTO #T VALUES(NULL, 'B ', NULL);
INSERT INTO #T VALUES('', 'Karl ', ' Smith');
INSERT INTO #T VALUES('Dr.', 'Karl ', ' Smith');
INSERT INTO #T VALUES('Dr. ', 'Joe ', ' Martin ');
SELECT BUSIN_NAME, SURNAME, NAME
, LTRIM(RTRIM( LTRIM(RTRIM(ISNULL(BUSIN_NAME,'')))+ ' ' + LTRIM(RTRIM(ISNULL(SURNAME,'')))+' ' +LTRIM(RTRIM(ISNULL(NAME,''))))) DESCR
, DATALENGTH ( LTRIM(RTRIM( LTRIM(RTRIM(ISNULL(BUSIN_NAME,'')))+ ' ' + LTRIM(RTRIM(ISNULL(SURNAME,'')))+' ' +LTRIM(RTRIM(ISNULL(NAME,''))))) ) AS LENGTH
FROM #T
OUtput:
BUSIN_NAME SURNAME NAME DESCR LENGTH
NULL NULL NULL 0
NULL A NULL A 1
NULL B NULL B 1
Karl Smith Karl Smith 10
Dr. Karl Smith Dr. Karl Smith 14
Dr. Joe Martin Dr. Joe Martin 14

I would suggest doing the following:
select stuff( (coalesce(' ' + BusinessName, '') +
coalesce(' ' + Surname, '') +
coalesce(' ' + Firstname, '')
), 1, 1, '')
These easily generalizes to more fields. You can use ltrim() rather than stuff(), because you are using spaces as a separator. stuff() is more general, because it handles other separators (notably commas).
As a computed column:
alter table t add newcol as
(stuff( (coalesce(' ' + BusinessName, '') +
coalesce(' ' + Surname, '') +
coalesce(' ' + Firstname, '')
), 1, 1, ''
)
)

Related

How can I store a SELECT query result(one column) into a new table's column?

This is my query:-
select
(FirstName + ' ' + Lastname) as [Name]
from
c_client
where
MiddleInitial is null
union
select
(FirstName + ' ' + MiddleInitial + '''' + Lastname) as [Name]
from
c_client
where
MiddleInitial is not null
After executing it, I'm getting this output:
This is my new table:
CREATE TABLE AddData(Name VARCHAR(MAX))
I want to insert the result generated by the SELECT query into my new table AddData. Can you help me do this?
You would use insert:
insert into AddData (Name)
Select (FirstName + ' ' + Lastname) as [Name]
from c_client
where MiddleInitial IS NULL
UNION
Select (FirstName + ' ' + MiddleInitial +''''+ Lastname) as [Name]
from c_client
where MiddleInitial IS NOT NULL;
I would instead suggest writing the logic as :
select (FirstName +
coalesce(MiddleInitial + '''', '') +
' ' +
Lastname
) as Name
into AddData
from c_client c;
You won't have to create the table first.
Also, if you do want to remove duplicates then use select distinct. It is not clear if you are using union intentionally to remove duplicates or just to combine two separate subqueries.

sql concatenate columns if one or more columns is not null

I have a table that looks something like this:
BuildingID | RouterType | RouterPort | RouterInstaller | Notes
-----------+------------+------------+-----------------+-------
282 | Linksys | 1990 | Super | NULL
307 | Sonic Wall | NULL | Greg | NULL
311 | NULL | NULL | NULL | NULL
I would like the Notes column to be the concatenation of the 2nd 3rd and 4th columns only if the column is not null.
line 1: Router Type: Linksys Router Port: 1990 Router Installer: Super
line 2: Router Type: Sonic Wall Router Installer: Greg
line 3: NULL
Also the word 'Router Type:' should only come in if the value of Router type is not null etc.
I am pretty new to SQL - any help would be greatly appreciated.
Try this:
select case when [Note] = '' then null else Note from (
select BulidingId,
case when RouterType is null then '' else 'Router Type: ' + RouterType + '; '+
case when RouterPort is null then '' else 'Router Port: ' + RouterPort + '; '+
case when RouterInstaller is null then '' else 'Router Port: ' + RouterInstaller + '; '+
case when Notes is null then '' else 'Notes: ' + Notes + '; ' [Note]
from MY_TABLE
) a
This will do it by combining Coalesce and Concat. The column names are added as labels to the column values.
select COALESCE(Notes, COALESCE(CONCAT(COALESCE(CONCAT('RouterType: ',RouterType),''),
COALESCE(CONCAT(' RouterPort: ',RouterPort ),''),
COALESCE(CONCAT(' RouterInstaller: ',RouterInstaller),'')), NULL)) as Notes
from yourTable;
Try this select query, i think it will help you:
SELECT CASE WHEN (
COL2 IS NOT NULL
AND COL3 IS NOT NULL
AND COL4 IS NOT NULL )
THEN
CONCAT(COL2,' ', COL3,' ', COL4) END as ConcatedData,
* from YOUR_TABLE;
To get the spacing correct, I recommend:
select stuff(coalesce(' RouterType: ' + RouterType), '') +
coalesce(' RouterPort: ' + RouterPort ), '') +
coalesce(' RouterInstaller: ', RouterInstaller), ''),
1, 1, ''
) as Notes
from t;
In an update:
update t
set notes = stuff(coalesce(' RouterType: ' + RouterType), '') +
coalesce(' RouterPort: ' + RouterPort ), '') +
coalesce(' RouterInstaller: ', RouterInstaller), ''),
1, 1, ''
);
Note: This will not put in a NULL value, instead using an empty string. That is easily fixed -- if it is a problem:
update t
set notes = nullif(stuff(coalesce(' RouterType: ' + RouterType), '') +
coalesce(' RouterPort: ' + RouterPort ), '') +
coalesce(' RouterInstaller: ', RouterInstaller), ''),
1, 1, ''
), ''
)
Try this update statement:
DECLARE #TEMP_TABLE TABLE (
BUILDING_ID INT,
ROUTER_TYPE VARCHAR(20) NULL,
PORT INT NULL,
ROUTER_INSTALLER VARCHAR(20) NULL,
NOTES VARCHAR(1000) NULL
)
INSERT INTO #TEMP_TABLE VALUES(1,'Linksys Router', 1990, 'Super', NULL)
INSERT INTO #TEMP_TABLE VALUES(2,NULL, NULL, NULL, NULL)
UPDATE #TEMP_TABLE
SET NOTES = COALESCE(' Router type: ' + ROUTER_TYPE,'') + COALESCE(' Port: ' + CAST(PORT AS VARCHAR),'') + COALESCE(' Router installer: ' + ROUTER_INSTALLER,'')
WHERE ROUTER_TYPE IS NOT NULL OR PORT IS NOT NULL OR ROUTER_INSTALLER IS NOT NULL
SELECT * FROM #TEMP_TABLE

How To Use COALESCE() to Add Additional Characters

I have the following syntax on my SELECT Statement:
CONCAT(first_name, " ", COALESCE(middle_initial), " ", last_name) AS full_name
Obviously, what I get is the following:
For first_name='John' and middle_initial='A.' and last_name='Smith'
I get 'John A. Smith'
That is fine and is the desired result.
But I get an extra space for the following data (which I clearly understand why):
For first_name='John' and middle_initial='' and last_name='Smith'
I get 'John Smith'
Is there a way with COALESCE() to append " " if the condition returns a non-null value?
Thank you.
When middle_initial has '', you would want to:
SELECT CONCAT(first_name, ' ',
CASE
WHEN middle_initial is null OR middle_initial = '' then ''
ELSE CONCAT(middle_initial, ' ')
END
, last_name) AS full_name
SQLFiddle Example
COALESCE is used for checking NULL values, not handling empty strings, ''
select concat(first_name , ' ' ,
case when middle_initial is null then ''
when middle_initial = '' then ''
else concat(middle_initial, ' ') end ,
last_name)
this query asumes that first_name and last_name are not null nor empty string, in that case you can apply same logic to that fields
Try this
SELECT rtrim(Coalesce(first_name + ' ','')
+ Coalesce(nullif(middle_name,'') + ' ', '')
+ Coalesce(nullif(last_name,'') + ' ', ''))
FROM #table
SELECT rtrim(Coalesce('John' + ' ','')
+ Coalesce(nullif('A.','') + ' ', '')
+ Coalesce(nullif('Smith','') + ' ', ''))
SELECT rtrim(Coalesce('John' + ' ','')
+ Coalesce(nullif('','') + ' ', '')
+ Coalesce(nullif('Smith','') + ' ', ''))
I think best way is to do it like this. You can use such a constraction for any number of fields, variables and so on. Also it will correctly show you John or John A.
declare #Temp_Table table (first_name nvarchar(128), middle_initial nvarchar(128), last_name nvarchar(128))
insert into #Temp_Table
select 'John', null, 'Smith' union all
select 'John', 'A.', 'Smith' union all
select 'John', 'A.', null union all
select 'John', null, null
select
*,
stuff
(
isnull(' ' + nullif(first_name, ''), '') +
isnull(' ' + nullif(middle_initial, ''), '') +
isnull(' ' + nullif(last_name, ''), ''),
1, 1, ''
)
from #Temp_Table

column values into one string in sql stored procedure

I have and address table with the following columns:
addressId
buildingName
streetNo
streetName
streetType
subAddressNo
subAddressType
suburb
postCode
state
What I want is to SELECT * from address where addressId = #addressId, and that return would be all columns concatenated into one string. Something like:
set #addressString = (SELECT * from address where addressId = #addressId)
select coalesce(cast(addressId as varchar)+ ',', '') + coalesce(buildingName+ ',', '')
coalesce(cast(streetNo as varchar)+ ',', '') -- + and so on
from address
remember to cast the numeric types as varchar
Try this:
SELECT buldingName + ' ' + streetNo + ' ' + streetName + ' ' + theRestOfYourColumns AS ClientAddress
FROM YourAddressTable
WHERE addressId = #addressIdPreviouslyAssignedVariable

Combine First, Middle Initial, Last name and Suffix in T-SQL (No extra spaces)

I'm trying not to reinvent the wheel here...I have these four fields:
[tbl_Contacts].[FirstName],
[tbl_Contacts].[MiddleInitial],
[tbl_Contacts].[LastName],
[tbl_Contacts].[Suffix]
And I want to create a FullName field in a view, but I can't have extra spaces if fields are blank...
So I can't do FirstName + ' ' + MiddleInitial + ' ' + LastName + ' ' + Suffix... Because if there is no middle initial or suffix I'd have 2 extra spaces in the field. I think I need a Case statement, but I thought someone would have a handy method for this...Also, the middleinitial and suffix may be null.
Assuming that all columns could be nullable, you can do something like:
RTrim(Coalesce(FirstName + ' ','')
+ Coalesce(MiddleInitial + ' ', '')
+ Coalesce(LastName + ' ', '')
+ Coalesce(Suffix, ''))
This relies on the fact that adding to a NULL value yields a NULL.
Whichever options you choose, here's something to think about: this will be a rather involved and thus time consuming option, especially if you have it in a view which gets evaluated each and every time for each and every row in question.
If you need this frequently, I would recommend you add this to your base table as a persisted, computed field - something like:
ALTER TABLE dbo.tbl_Contacts
ADD FullName AS (insert the statement of your choice here) PERSISTED
When it's persisted, it becomes part of the underlying table, and it's stored and kept up to date by SQL Server. When you query it, you get back the current value without incurring the cost of having to concatenate together the fields and determine which to use and which to ignore...
Just something to think about - something that too many DBA's and database devs tend to ignore and/or not know about....
You may want to pass the FirstName + ' ' + MiddleInitial + ' ' + LastName + ' ' + Suffix concatenation through the REPLACE() function in order to substitute duplicate spaces into a single space.
REPLACE(FirstName + ' ' + MiddleInitial + ' ' + LastName + ' ' + Suffix, ' ', ' ')
-- -- -
EDIT:
Just noticed that some of your fields may be NULL, and therefore the above would not work in that case, as the whole string would become NULL. In this case, you can use the COALESCE() method as suggested by Thomas, but still wrapped it in a REPLACE():
REPLACE(RTRIM(COALESCE(FirstName + ' ', '') +
COALESCE(MiddleInitial + ' ', '') +
COALESCE(LastName + ' ', '') +
COALESCE(Suffix, '')), ' ', ' ')
Test:
SELECT REPLACE(RTRIM(COALESCE('John' + ' ', '') +
COALESCE('' + ' ', '') +
COALESCE('Doe' + ' ', '') +
COALESCE(NULL, '')), ' ', ' ')
-- Returns: John Doe
I had to join Firstname, Middlename, and Lastname. my challenge was to handle NULL values, used following code.
RTRIM(LTRIM(RTRIM(isnull(#firstname,'') + ' ' + isnull(#middlename,'')) + ' ' + isnull(#lastname,'')))
Test different scenarios if you are interested :)
DECLARE #firstname VARCHAR(MAX)
DECLARE #middlename VARCHAR(MAX)
DECLARE #lastname VARCHAR(MAX)
set #firstname = 'FirstName'
set #middlename = NULL
set #lastname = 'LastName'
SELECT '|'+RTRIM(LTRIM(RTRIM(isnull(#firstname,'') + ' ' + isnull(#middlename,'')) + ' ' + isnull(#lastname,'')))+'|'
--
If you are using SQL Server 2012+ you could use CONCAT and +:
SELECT RTRIM(
CONCAT(FirstName + ' ', MiddleInitial + ' ', LastName + ' ', Suffix)
) AS [FullName]
FROM tbl_Contacts;
How it works:
If any part of full name is NULL then NULL + ' ' → NULL
CONCAT handles NULL
In case that after part of name there are only NULLs, TRIM last space.
LiveDemo
Here is a solution:
CREATE FUNCTION dbo.udf_IsNullOrEmpty
(
#vchCheckValue VARCHAR(MAX)
,#vchTrueValue VARCHAR(MAX)
,#vchFalseValue VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
RETURN CASE WHEN NULLIF(RTRIM(LTRIM(#vchCheckValue)),'') IS NULL THEN #vchTrueValue ELSE #vchFalseValue END
END
SELECT FirstName + ' ' +
dbo.udf_IsNullOrEmpty(MiddleInitial,'',MiddleInitial + ' ') +
LastName +
dbo.udf_IsNullOrEmpty(Suffix,'',' ' + Suffix)
FROM tbl_Contacts
select CONCAT(IFNULL(FirstName, ''),
'',
IFNULL(MiddleName, ''),
'',
IFNULL(LastName, '')) AS name
from table
Why not like this:
select concat(fName,' ',
case length(mName)
when 0 then ''
else concat(mName, ' ') end, lName) as fullName
My columns are not null, so this works for me.
create function getfname(#n varchar(30))
returns varchar(30)
as
begin
declare #s varchar(30)
set #s=LEFT(#n,charindex(' ',#n)-1)
return #s
end
create function getLname(#n varchar(30))
returns varchar(30)
as
begin
declare #s varchar(30)
set #s=substring(#n,charindex(' ',#n+1),Len(#n))
return #s
end
the query:
SELECT retire.employeehrmsid,
Isnull(retire.firstname, '') + ' '
+ Isnull(retire.middlename, '') + ' '
+ Isnull(retire.lastname, '') AS FullName,
retire.dojtoservice,
retire.designation,
emphistory.currentdoj,
emphistory.presentddo,
emphistory.office,
transfer.generatetid AS TransferID,
transfer.transferdate,
transfer.currentlocation,
transfer.newlocation,
transfer.datas AS Transfer_Doc,
release.generaterid AS ReleaseID,
release.releasedate,
release.datar AS Release_Doc,
employeeserviceupdate.dataeu AS Join_Doc
FROM retire
INNER JOIN emphistory
ON retire.id = emphistory.id
INNER JOIN employeeserviceupdate
ON retire.id = employeeserviceupdate.id
INNER JOIN transfer
ON retire.id = transfer.id
AND emphistory.ehrid = transfer.ehrid
INNER JOIN release
ON transfer.tid = release.tid