Solved

SQL Server - Spaces in combined Computed Column

Posted on 2013-05-30
9
405 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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:
ScottPletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now