Solved

Need help formating tsql output

Posted on 2014-01-24
6
308 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

861 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

28 Experts available now in Live!

Get 1:1 Help Now