Solved

SQL Server - Spaces in combined Computed Column

Posted on 2013-05-30
9
416 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
[X]
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
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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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 41

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

728 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