Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Server - Spaces in combined Computed Column

Posted on 2013-05-30
9
Medium Priority
?
421 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 70

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

609 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