Friday, October 11, 2013

Formatting Numbers in MySQL

I like SQL output to align properly for numbers, so I did some digging and came up with this:
lpad(format(seconds/60,2),9,' ')

The lpad function will pad the output from the left with whatever the 3rd argument is. In this case, I set it to ' ' (a space between quotes) for 9 characters.

The format function will round the output to the number of decimals specified. The example is set for 2 decimal places.

The "seconds/60" is obviously a math equation to convert seconds to minutes. The seconds is the data field I am formatting.

In Navicat, under Tools>Options..., I set the Grid Font (found under Appearance>Font) to DejaVu Sans Mono at 8 pt. This is nice, smooth mono-spaced font.

No comments: