Skip to content

How to align query results in a column #75

Answered by dideler
dideler asked this question in Q&A
Discussion options

You must be logged in to vote

Formatting a number (plus any numerical operations) then padding it as a string works. Not as intuitive as an alignment option.

select
  paid as paid_orig,
  number_format(paid, 2) as paid_num_fmt,
  lpad(paid, 9, ' ') as paid_str_fmt,
  lpad(number_format(paid, 2), 9, ' ') as paid_fmt
from stdin

┌───────────┬──────────────┬──────────────┬────────────┐
│ paid_orig │ paid_num_fmt │ paid_str_fmt │  paid_fmt  │
├───────────┼──────────────┼──────────────┼────────────┤
│ 85000.00  │ 85,000.00    │  85000.00    │ 85,000.00  │
│ 850.00    │ 850.00       │    850.00    │    850.00  │
│ 85.00     │ 85.00        │     85.00    │     85.00  │
│ 8.50      │ 8.50         │      8.50    │      8.50  │…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dideler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant