What if I wanted to define a custom column format in MySQL? The custom format that I want is always a 2 digit integer, followed by the # sign, then a space, then a decimal number with 2 digits after the decimal. Examples of allowed values are like this:
30# 11.00
27# 17.25
40# 17.25
values that are not allowed are like this:
30# 11
40# 20.5
some string
Is this possible in MySQL? If so, any hints on how to do it?
well since "The CHECK clause is parsed but ignored by all storage engines." in mysql, your only remaining option would be to add a before insert/update trigger on your table to regexp your input and proceed.
Related
Please look at my screenshots and help me to understand what I am missing.
What datatype should I choose for these columns in MYSQL? I keep getting mistakes in decimal datatype columns. I chose decimаl12,3 because no columns(revenue, product&purchase price) with currency have more than 12 digits in total, 9 before and 3 after the decimal point. Could someone help me to understand what data type to choose with examples?
if we have an integer number e.g. 85192 we choose int?
for currency we choose the decimal, right? then what have I done wrong that I keep getting errors? 0 records imported.
if we have a combination of numbers and letters or just letters then we choose varchar? and varchаr1 equals 1 character, eg. apple32 = 7 characters, therefore vаrchar7?
turning to decimal, 12,464.87 in total 7 digits, 5 before and 2 after the decimal point, hence mysql decimаl7,2 should be enough, right? or would it be better to put decimаl10,3 with a margin so to say.
excel
mysql
data
$1,000.00 contains two characters that cannot be part of a numeric literal: the dollar sign and the comma that is used as a thousands separator.
Find a way to change '$1,000.00' to '1000.00' in the input file. Then, the load will succeed.
Alternatively, create an intermediate table where product_price is a VARCHAR(32), load into that, and then:
INSERT INTO target_table
SELECT
other_col1
,other_col2
, ....
,CAST(REPLACE(REPLACE(product_price,',',''),'$','') AS DECIMAL(15,2)
,other_col_n
,...
FROM staging_table;
You don't need an intermediate table. When doing LOAD DATA, put and columns into #variables; then use a SET to convert as needed:
LOAD DATA
...
col1, col2, #price, ...,
SET price = CAST(REPLACE(REPLACE(product_price,',',''),'$','') AS DECIMAL(15,2))
Dates need to be like this: "2022-07-25 22:02:22". Either change what Excel is delivering, or use STR_TO_DATE(...) in the SET.
I have number in DB column and i want to format this with money,decimal data types.
my query is like below,
SELECT CONVERT(VARCHAR,CAST(CAST(CAST(HouseholdCoverage AS DECIMAL(10,1))/ 12 AS DECIMAL(10,1))
AS money),1) AS HouseholdGoodsInsuredAmount FROM Table
HouseholdCoverage = 1175012
My query returns the value like below,
97,917.70
but i want result as
97,917.7 (only 1 number after decimal places).
If i cast the value as float i am getting error.
I know i can use parsename to separate the values after decimal place and add it again with few modifications. But that is not good i feel.
As well as i know this has to be done in C# application, but this query is already written in sql server 2008, i just want modify it according to requirements.
I just figured it out myself after doing some extensive research.. but dont know performance will be good or bad
SELECT LEFT(CONVERT(VARCHAR,CAST(CAST(CAST(HouseholdCoverage AS DECIMAL(10,1))/ 12 AS DECIMAL(10,1))AS money),1),
len(CONVERT(VARCHAR,CAST(CAST(CAST(HouseholdCoverage AS DECIMAL(10,1))/ 12 AS DECIMAL(10,1))AS money),1))-1)
AS HouseholdGoodsInsuredAmount FROM Table
Now the result is as i expected with 1 decimal places after decimal point,
97,917.7
Just keep is simple. Divide your number by a decimal number and cast the answer as DECIMAL(10,1)
DECLARE #HouseholdCoverage BIGINT = 1175012
SELECT CAST(#HouseholdCoverage/128.0 AS DECIMAL(10,1))
DECIMAL(10,1) will make sure that there will be only 1 number after precision(.)
I have set my MySQL field table data type to the decimal because from what I have read, I would be able to store the price with commas/dots in the decimal data type fields... The problem is that whenever I store any data with the comma or dot, MySQL is rounding it automatically up or down. Eg. When I'm executing the following query:
UPDATE table SET field = 114.21 WHERE id = 1;
Then field is set, but the value is rounded to 114, instead of displaying the data I set in the query (114.21) - is there any solution for that? Or I should just use other data type?
AFAIK the dot is the standard notation for decimal values. Using Commas may trigger SQL parse errors or may go unnoticed if the syntactical context allows for a comma to be there.
How did you define the precision of the DECIMAL column?
If it is DECIMAL(10, 2) it will have a total of 10 numbers of which 2 are decimal values (with 2 decimal rounding meaning that 10.215 is saved as 10.22 and 10.214 becomes 10.21).
If it is DECIMAL(10) it will not have any decimal values and be rounded to an integer.
If you use FLOAT or DOUBLE PRECISION you don't have to specify the number of decimal values but it has its own flaws.
As Mihai mentioned you need to define the proper precision for the decimal type, e.g. DECIMAL(10,2) for two decimal places.
When inserting a decimal value mySQL will round.
From the docs:
For inserts into a DECIMAL or integer column, the target is an exact data type, so rounding uses “round half away from zero,” regardless of whether the value to be inserted is exact or approximate.
See http://dev.mysql.com/doc/refman/5.0/en/precision-math-rounding.html for details.
Well before I have also an issue regarding on what to use on my numbers with decimal points. But problem solved by using DOUBLE(10,2) as my DATATYPE, and it shows the exact number on the database when you save it. Hope it will help.
Mysql Table shows value 'b' in place of bit type of data why??
How to convert it again into its original format does anybody know this??
I want values as 0 or 1 in these columns.
Taken from Bit-Field Literals
Beginning with MySQL 5.0.3, bit-field values can be written using
b'value' or 0bvalue notation. value is a binary value written using
zeros and ones.
Bit values are returned as binary values. To display them in printable
form, add 0 or use a conversion function such as BIN(). High-order 0
bits are not displayed in the converted value.
I found out the solution.
just call value using sql query,
this query will return only 0 or 1 for bit value though mysql represents value as 'b'.
Need not to worry.
I tried it as "select flag * 4 from table where id = 1" and answer was 0 as 0*4=0.
I'm creating a DB that will hold products with several "height" columns (in meters, for ex 7.79 or 12,8). Never more than 2 digits before and 2 after the decimal point. What field type should I use for this?
If I use decimal(2,2) an try to insert 7.79 in phpmyadmin I get an error saying Warning: #1264 Out of range value for column 'working_height' at row 1
I'll be using this DB for searching, so I have to be able to run a query like "select all products where height is great than 7".
You're looking for decimal(4,2) - in general, decimal(m,n) means m total digits, and n to the right of the decimal point. Docs here.
So a decimal(2,2) can store two total digits, both to the right of the decimal point. This explains the error that you are seeing.
People will say to use decimal(s, d) but how about storing the values as integers, in centimeters instead of meters? Easier to compare (no precision loss).
Just my two cents.
Try DECIMAL(4,2) instead
Refer to: MySQL Numeric Types