Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Server - Spaces in combined Computed Column

Posted on 2013-05-30
9
412 Views
Last Modified: 2013-05-31
Hey Experts
 I have this as a computed column.

 From the computed column i use 2 different triggers to update seperate tables (Segment & Parcel) with this value
Problem is it looks like i have extras spaces on the end of the computed column in my tables i would like to deleted them.
(ltrim((((((rtrim(isnull([Prefix_Full],''))+' ')+rtrim(isnull([Street_Name],'')))+' ')+rtrim(isnull([Suffix_Full],'')))+' ')+rtrim(isnull([Suffix_Direction],''))))

Open in new window


Is some cases i have 2 spaces and in some case i have 1.
How is the easiest way to trim these?
0
Comment
Question by:PtboGiser
9 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 39208948
Can you just call RTRIM on the whole thing?
(rtrim(ltrim((((((rtrim(isnull([Prefix_Full],''))+' ')+rtrim(isnull([Street_Name],'')))+' ')+rtrim(isnull([Suffix_Full],'')))+' ')+rtrim(isnull([Suffix_Direction],'')))))

Open in new window

0
 

Author Comment

by:PtboGiser
ID: 39208956
I was trying but couldnt get the right syntax
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39208966
Are you getting a error on this? (It looks long and messy, so I didn't try to come up with an example)
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:PtboGiser
ID: 39208978
+rtrim(isnull([Suffix_Direction],''))))RTRIM i was adding this to the end, and i get

Error ;
'Street' table
- Unable to modify table.  
Incorrect syntax near 'RTRIM'.

If i add a Bracket at the end Like so
+rtrim(isnull([Suffix_Direction],''))))RTRIM) and the Start of it all i get
Error
Validating Syntax
RTRIM.PNG
0
 

Author Comment

by:PtboGiser
ID: 39209019
((ltrim((((((rtrim(isnull([Prefix_Full],''))+' ')+rtrim(isnull([Street_Name],'')))+' ')+rtrim(isnull([Suffix_Full],'')))+' ')+rtrim(isnull([Suffix_Direction],''))))Rtrim[Street_Full_Name])

Open in new window


Playing with stuff like this
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 100 total points
ID: 39209083
try this.
rtrim(isnull(Prefix_Full + ' ','') + isnull(Street_Name + ' ','') + isnull(Suffix_Full + ' ','') + isnull(Suffix_Direction,''))

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39209251
To go off of what Sharath_123 posted, I would do something like this to make sure of every field

CREATE TABLE whatever ( ....
   ....
   COMPUTED_COLUMN AS (ISNULL(LTRIM(RTRIM(Prefix_Full)) + ' ','')
      + ISNULL(LTRIM(RTRIM(Street_Name)) + ' ','')
      + ISNULL(LTRIM(RTRIM(Suffix_Full)) + ' ','')
      + ISNULL(LTRIM(RTRIM(Suffix_Direction)),'')
   )
)

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 400 total points
ID: 39209327
You can't alter a computed column definition, you have to drop and re-add it:

ALTER TABLE dbo.tablename
DROP COLUMN <computed_column_name>

ALTER TABLE dbo.tablename
ADD <computed_column_name> AS CAST( LTRIM(RTRIM(
    ISNULL([Prefix_Full] + SPACE(1), '') +
    ISNULL([Street_Name] + SPACE(1), '') +
    ISNULL([Suffix_Full] + SPACE(1), '') +
    ISNULL([Suffix_Direction], '') )) AS varchar(nnn)) --<<-- nnn = desired total column length
0
 

Author Closing Comment

by:PtboGiser
ID: 39210656
Thanks as always
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question