Solved

Need help formating tsql output

Posted on 2014-01-24
6
314 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
[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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 43
Star schema daily updates 2 35
Need to trim my database size 9 19
T-SQL: I need to add an index on a field 5 20
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

756 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