Change entire column format - phpMyAdmin - mysql

In a project I am working on, I have a column in my database named Amount. This is a money format, but there is no need for it to have a dollar sign in front.
I would like every value that is entered in here to be this format:
123456 -> 123,456.00
How can I achieve this?

No, the stored value should not be formatted, it should be stored as a plain number, otherwise you can't do any arithmetic calculations.
When the number is displayed, that's when you format its appearance either in the application layer (that's the preferred approach) or you can use MySQL's own format() function in the select statement to achieve this:
select format(amount) from your_table

Related

Format Text Field To Date In Access Query

I have a form in Access that has two text boxes that are format to be a short date. I am now attempting to capture those values and use in a query in the WHERE clause. I tried this syntax
Between CDate([Forms]![Form1]![date1]) And CDate([Forms]![Form1]![date2])
This expression is typed incorrectly, or it is to complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables.
How should I capture & convert the entries from my form text boxes so that I can use them in my query?
Specify the parameters to free Access from guessing, then use these "as is":
Parameters
[Forms]![Form1]![date1] DateTime,
[Forms]![Form1]![date2] DateTime;
Select
<your select statement>
From
<your table/query>
Where
[YourDateField] Between
[Forms]![Form1]![date1] And
[Forms]![Form1]![date2]

How to change the field type of result set from SQL pass-through (SPT)?

I have a mysql table column defined as unit_price float(12,4).
If I assign a value of 0.1234, when I create a remote view with dbsetprop, I can retrieve 0.1234 with myview.unit_price.
However, when I use SQL pass-through (SPT) like this:
sqlexec(nHandle, "select * from table", "oResult")
the result of oResult.unit_price only shows 0.12.
How can I ensure that I return the correct, full value?
Not having explicitly tried, you can do some simple math forcing to create larger precision, such as adding an additional column to your query... something like...
select *, unit_price * 1.00000 as UnitPrice5 from...
This will force the field to be computed to 5 decimal position and MIGHT actually change the result column to properly handle this forced decimal capacity. Then, you would use the "UnitPrice5" column instead of "unit_price"...
Don't know if that would be a big issue for you, but once it is in VFP, you have more control too.
The length of the decimal values are defined in your FoxPro settings. Go to Tools->Options->Regional and set the Decimal Digits to a higher value.

MySQL is cutting '00' in decimals

I have a table with a decimal column with a lenght = 9 and decimals = 2.
If I put a value of 21.59 (for example) it works ok.
If I put 52.00 it writes only 52. I need to keep 52.00 instead.
Master question: Can the database store the value this way? Instead of
using format/cast in select to retrieve the value...
As noted bellow, this make sense:
"You shouldn't worry about display formatting issues at the database
level but at the ... display level"
Use the FORMAT function:
select format(mycolumn, 2) from mytable;
This also has the effect of adding thousand's separator into the number, so you would get output like 123,456.70. There are workarounds if this doesn't work for you.
Given that MySQL doesn't have the world's best facilities for formatting numbers, display issues like this are usually handled in client code.

why this MySQL SELECT doesn't include the right dates?

The main problem is, that I have stored in database datetime , not the date (what I need). Ok never mind.
I have thousands of reports stored each day.
I need to LEFT by 10 my datetime_view (to cut the time) and everything's fine. Except this. I'm trying to figure out why do I have to put in the condition + one day from the future? Otherwise it won't search what I want.
SELECT
LEFT(datetime_view,10),
count(type)
FROM reports
WHERE
type IN (1,2,5)
AND
datetime_view>='2012-10-28'
AND
datetime_view<='2012-11-04'
group by LEFT(datetime_view,10);
You can see I must search from the future. Why??
It gives me an output from 28.10 to 3.11 ....
don't use string operations on date/time values. MySQL has a huge set of functions for date/time manipulation. Try
GROUP BY DATE(datetime_view)
instead, which will extract only the date portion of the datetime field. Your string operation is not y10k compliant. Using the date() function is.
As for your plus one day, consider how the comparisons are done: A plain date value, when used in date/time comparisons, has an implicit 00:00:00 time value attached to it, e.g. all dates have a time of "midnight".
i think it's better to use DATE(datetime_view) to cut the time instead of LEFT(datetime_view,10), also on the where condition:
DATE(datetime_view) <= '2012-11-03'

could the MySQL show percent sign (%) in table

i want to show some data in percent.
i have a mathematics formula like:
(qty(S) + qty(B))/qty(id)*100%
could i show the result for example like 25%? how do i do that?
Databases are used for storing data. Presentation of data should not be in its responsibilities. By that, I mean you should very rarely thing about storing a string value in the database like '75%'.
If you want specific formatting, the best place to do it is after extracting the data:
select concat(your_column,'%') as percent ...
Because concat expects strings, numeric values are automagically cast into string before joining them together.
It's a presentation thing, but it's handled in the same fashion. You need to change the data type of the result to a string based one:
CAST((qty(S) + qty(B))/qty(id)*100 AS CHAR(2))+'%'