Using VSQL as a Calculator

Jim_KnicelyJim_Knicely Administrator
edited January 2019 in Tips from the Team

A calculator is something used for making mathematical calculations, in particular a small electronic device with a keyboard and a visual display. VSQL mostly fits that description!

Example:

dbadmin=> SELECT 325.25 * 1.60934 "Miles to Kilometers";
Miles to Kilometers
---------------------
         523.4378350
(1 row)

dbadmin=> SELECT 119.25 * 0.25 "Excellent Waitress's Tip";
Excellent Waitress's Tip
--------------------------
                  29.8125
(1 row)

dbadmin=> SELECT to_char(525 * 40 * 52, '$9,999,999.00') "My Yearly Compensation";
My Yearly Compensation
------------------------
$ 1,092,000.00
(1 row)

And for fun, you can also astound your friends using this old calculator trick!

1) Have a buddy pick a number between 1 and 9.
2.) Now have him or her use a calculator (i.e. VSQL) to first multiply their number by 9
3) Then multiply it by 12,345,679 (note there is no 8 in this number).
4) Have the person show you the result so you can tell them the original number they selected!

dbadmin=> SELECT LEFT((1 /* One is my original Number */ * 9 * 12345679)::VARCHAR, 1)  "You picked!";
You picked!
-------------
1
(1 row)

dbadmin=> SELECT LEFT((2 /* Two is my original Number */ * 9 * 12345679)::VARCHAR, 1)  "You picked!";
You picked!
-------------
2
(1 row)

dbadmin=> SELECT LEFT((5 /* Five is my original Number */ * 9 * 12345679)::VARCHAR, 1)  "You picked!";
You picked!
-------------
5
(1 row)

Helpful Link:
https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/SQLReferenceManual/LanguageElements/Operators/MathematicalOperators.htm

Have fun!

Sign In or Register to comment.