Solved

Need help formating tsql output

Posted on 2014-01-24
6
316 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

717 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