Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right-align a column when selecting data?

Tags:

sql

mysql

A school assignment I'm working on says to format a decimal(14,0) column "as currency [US$xxx,xxx] and right justified [all the commas line up vertically]."

I can select the data in the correct format using this:

CONCAT("US$", FORMAT(theColumn, 0))

But the data is not right justified. I've been searching and searching and simply haven't been able to find any way to right justify the output.

I did find an answer on here that shows how to do it if the column is a string type and has a fixed width, but I can't find a way to right justify the output for a decimal data type. Is it possible?

EDIT:

MySQL returns data left justified, like this:

US$18,100,000,000
US$130,100,000,000
US$1,200,000,000

I want to select it right justified, like this:

 US$18,100,000,000
US$130,100,000,000
  US$1,200,000,000
like image 387
Nate Avatar asked Nov 29 '22 00:11

Nate


2 Answers

I think you want

select lpad(column_name,x,' ') from table_name;

where x is the number of places you want that value you fill (so say 8 places)

like image 74
Outsider Avatar answered Dec 10 '22 04:12

Outsider


I would have done:

LPAD(CONCAT("US$", FORMAT(theColumn, 0)),20,' ')

What this does is present the output in a column of 20 characters, padding ' ' to the left of values.

like image 24
raydos Avatar answered Dec 10 '22 02:12

raydos