?
Solved

SQL Server - Spaces in combined Computed Column

Posted on 2013-05-30
9
Medium Priority
?
419 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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 400 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 1600 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

800 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