[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL Query Advice

Posted on 2009-07-06
10
Medium Priority
?
257 Views
Last Modified: 2012-05-07
I need help on a SQL Query. I have 3 specific columns that will be merged into one string in Crystal Reports and they have to be a total length (between the 3) = 75 characters.

My specific coding on these 3 fields:

ISNULL(LEFT(ic.ListName, 25), 'xxxxxxxxxxxxxxxxxxxxxxxxx') AS [Primary Payer] ,
ISNULL(LEFT(ic2.ListName, 25), 'xxxxxxxxxxxxxxxxxxxxxxxxx') AS [Secondary Payer] ,
ISNULL(LEFT(ic3.ListName, 25), 'xxxxxxxxxxxxxxxxxxxxxxxxx') AS [Third Payer]

My issue:

I had a [Primary Payer] = 'Zales' and a [Secondary Payer] = 'Aetna Life and Casualty' and a [Third Payer] = 'United Health Care  SLCit'.

Is it possible to take the LEFT 25 characters and if not equal to a total of 25 characters pad it with blank spaces?
0
Comment
Question by:Jeff S
[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
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 400 total points
ID: 24787985
you can try to use the space(x) function
maybe something like:

isnull( LEFT(ic.ListName, 25) + substring(space(25),1, 25-LEFT(ic.ListName, 25)), space(25))
0
 
LVL 7

Author Comment

by:Jeff S
ID: 24788030
I get this back:

Msg 245, Level 16, State 1, Line 50
Syntax error converting the varchar value 'Universal Life' to a column of data type int.
0
 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 400 total points
ID: 24788081
The syntax you posted is in in SQL. So once you have the filed in crystal you can do something like this:
{Table.PrimaryPayer }+Space(25-Len({Table.Primary Payer }))
this should do it.
P.
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 17

Expert Comment

by:pssandhu
ID: 24788096
The ISNULL function in crystal returns either true if the field is null and false if the field is not, just so we all are clear on that.
P.
0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 400 total points
ID: 24788125
In SQL you can also try something like this, discarding the isnull
select .... left(ic.ListName, 25) + space(25 - len(ic.ListName)) .... from ....
 
0
 
LVL 18

Assisted Solution

by:UnifiedIS
UnifiedIS earned 400 total points
ID: 24788328
Why not just cast your names as char(25)?
SELECT CAST(ISNULL(ic.ListName, '') AS char(25)) + CAST(ISNULL(ic2.ListName, '') AS char(25)) + CAST(ISNULL(ic3.ListName, '') AS char(25))
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24788342
Another possibility
select isnull(left(ic.ListName, 25) + space(25 - len(ic.ListName)), space(25)) .... from ....

Open in new window

0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24788557
ralmada fixed it
0
 
LVL 5

Assisted Solution

by:DBDevl
DBDevl earned 400 total points
ID: 24789153
This works too

SELECT CASE WHEN LEN(ic.ListName) < 25
THEN ic.ListName + REPLACE(SPACE(25 - LEN(ic.ListName)), ' ', 'X')
ELSE ic.ListName END
0
 
LVL 7

Author Closing Comment

by:Jeff S
ID: 31600294
Great tips! Split points to be fair as all helped
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

650 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