SQL - Align Column Values to Right

Asked

Viewed 580 times

0

Hello!

I wonder if by means of an SQL sentence there is any possibility of aligning the values of the columns to the right. Here is an example below that I can do via C#, but I wanted to do the alignment only via T-SQL:

C# code that works:

result = string.Format("{0,16}{1,8}{2,4}{3,6}{4,15}{5,15}{6,16}",
                       reader["Coluna1"].ToString(),
                       reader["Coluna2"].ToString(),
                       reader["Coluna3"].ToString(),
                       reader["Coluna4"].ToString(),
                       reader["Coluna5"].ToString(),
                       reader["Coluna6"].ToString(),
                       reader["Coluna7"].ToString());

Result of variable "result": 211582060520130004059:30 0,00 0,00 0,00N

  • 1

    SQL has nothing to do with it, here it works. SQL is for taking data, not for formatting data. Do not gambiarras, use the cverta tool for the problem. As a curiosity your code could be much simpler: https://dotnetfiddle.net/A8JDgi. I had it printed for you to see, but I could have saved it in variable as it was, just change the WriteLine() by the allocation. And of course the x for each item of reader, without .ToString().

  • These are the functions available in SQL.https://www.w3schools.com/sql/sql_ref_sqlserver.asp And of course you can create your own.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.