?
Solved

Trim excess characters in SQL

Posted on 2014-08-12
9
Medium Priority
?
220 Views
Last Modified: 2014-11-26
I have a SQL report that has a field based on a query.  The Question is
"Current/Prior Military Service"  answer is Y or No,  if yes, which war.

When I run the report it prints out as the following:
{N|}
{N|}
{Y|}
{N|}
{Y|}
{N|}
{N|}
{N|}
{N|}
{N|Desert Storm}
{N|}
{N|}
{Y|}
{N|}
{N|}
{N|}

I used the following code to TRIM the field to separate into two columns but how do I get rid of the leading and trailing characters and leave only the Name of the war in the second column.  This is code I used:
RTRIM(RAQ_R.ValueInfo   ) as CurrentPriorMilitaryService,
LTRIM(    RAQ_R.ValueInfo) as NameOfWar

Here are the results after the TRIM:
CurrentPriorMilitaryService       NameOfWar
{N|}                                            {N|}
{Y|Desert Storm}                       {Y|Desert Storm}
{N|}                                            {N|}

i NEED IT LIKE THIS
 CurrentPriorMilitaryService        NameOfWar
N
Y                                                Dessert Storm
N
0
Comment
Question by:kvrogers
  • 5
  • 4
9 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 40256933
select Substring(RAQ_R.ValueInfo,2,1)CurrentPriorMilitaryService        , case when len(RAQ_R.ValueInfo) > 4 THEN SUBSTRING(RAQ_R.ValueInfo, Charindex('|',RAQ_R.ValueInfo)+1, CHARINDEX('}',RAQ_R.ValueInfo)-Charindex('|',RAQ_R.ValueInfo)-1 )  else '' end NameOfWar
0
 

Author Comment

by:kvrogers
ID: 40257334
Thank you for your quick response.  I will keep this reference in my book.
0
 

Author Comment

by:kvrogers
ID: 40467387
I have a similar field in another report that has a response of {Y|} that I need to just say  Y.
Here is what I entered:
Substring(RAQ_R.ValueInfo,2,1)HxInfectiousDisorder        , case when len(RAQ_R.ValueInfo) > 4 THEN SUBSTRING(RAQ_R.ValueInfo, Charindex('|',RAQ_R.ValueInfo)+0, CHARINDEX('}',RAQ_R.ValueInfo)-Charindex('|',RAQ_R.ValueInfo))  else '' end  Response

and my report shows

HxInfectiousDisorder           Response

Y
Y
Y

Can you break down the substring for me and show me how to just use a single response.  I tried to edit it but when I did then I got NO response at all.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40467401
case when len(RAQ_R.ValueInfo) > 4
  change that to case when len(RAQ_R.ValueInfo) >= 4
0
 

Author Comment

by:kvrogers
ID: 40467464
I entered:  
Substring(RAQ_R.ValueInfo,2,1)HxInfectiousDisorder        , case when len(RAQ_R.ValueInfo) >= 4 THEN SUBSTRING(RAQ_R.ValueInfo, Charindex('|',RAQ_R.ValueInfo)+0, CHARINDEX('}',RAQ_R.ValueInfo)-Charindex('|',RAQ_R.ValueInfo))  else '' end  

Here is what I get now.   I need to get rid of last column.
HxInfectiousDisorder      Response
Y                                         |
Y                                         |
Y                                         |


I just want it to say:  
HxInfectiousDisorder
Y
Y
Y
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40467471
you just need to use this , you can skip the rest of your query

Substring(RAQ_R.ValueInfo,2,1)HxInfectiousDisorder
0
 

Author Comment

by:kvrogers
ID: 40467604
Thanks, that worked.  I should have made this a new question and given you more points.  I will ask what to do.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40467637
that's fine. Glad I helped.
0
 

Author Comment

by:kvrogers
ID: 40467639
I sent an email to the moderator to see what I could do.
0

Featured Post

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.

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

862 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