Solved

Need help formating tsql output

Posted on 2014-01-24
6
302 Views
Last Modified: 2014-01-25
I have the following sql statement:

SELECT TOP(4) ('arrDeck['), (HandRank),+ ',0] = "',(Cards),'"'
FROM HMHoleRankings

It returns:

               HandRank                 Cards
arrDeck[      1      ,0] = "      aa              "
arrDeck[      2      ,0] = "      kk              "
arrDeck[      3      ,0] = "      qq              "
arrDeck[      4      ,0] = "      aks             "

Needs to return:

arrDeck[1      ,0] = "aa"
arrDeck[2      ,0] = "kk"
arrDeck[3      ,0] = "qq"
arrDeck[4      ,0] = "aks"

What should I change to trim the values?

HandRank is an integer that needs to be returned as a character string.

Thanks,
0
Comment
Question by:Dovberman
  • 4
  • 2
6 Comments
 

Author Comment

by:Dovberman
ID: 39808329
Sorry,
Needs to return:

arrDeck[1,0] = "aa"
arrDeck[2,0] = "kk"
arrDeck[3,0] = "qq"
arrDeck[4,0] = "aks"
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 400 total points
ID: 39808503
Try this:
SELECT TOP(4) 'arrDeck[' + convert(varchar, HandRank) + ',0] = "' + (Cards) + '"'
FROM HMHoleRankings

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39808640
Great improvement.

arrDeck[1,0] = "aa        "
arrDeck[2,0] = "kk        "
arrDeck[3,0] = "qq        "
arrDeck[4,0] = "aks       "

How do I eliminate the remaining blanks to return as follows?

arrDeck[1,0] = "aa"
arrDeck[2,0] = "kk"
arrDeck[3,0] = "qq"
arrDeck[4,0] = "aks"

Thanks,
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39808642
probably a char field instead of varchar, use
RTRIM(Cards)

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39808663
Works perfectly.

Thanks
0
 

Author Closing Comment

by:Dovberman
ID: 39808665
Works perfectly.

Thanks
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

760 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

17 Experts available now in Live!

Get 1:1 Help Now