On a mySQL database, in a dataset all values are stored as varchar, show amounts of Euro and look like this: 'EUR 2.5E+2', 'EUR 56.95', 'EUR 1E+2'
Now, I want to make it useful again and convert this into decimal for further calculation, but can't figure out how to do it.
I have tried to follow the suggested solution to this question:
Casting Scientific Notation (from varchar -> numeric) in a view
However, I failed to replicate the solution as a query, view or stored procedure, much less adapt it to my problem.
I really hope someone can help me out here.
Thanks in advance!
Thank you very much Rick James, your solution works quite well.
However, I prefer to explicitly cast the substring as decimal:
CAST(SUBSTRING_INDEX(p.amount, ' ', -1) AS DECIMAL(5,2))
Now I can carry out calculations on the result (see comment below Rick's answer).
SELECT SUBSTRING_INDEX('EUR 2.5E+2', ' ', -1) + 0;
->
250
Caution: This assumes there is one space, and the numeric value is after the space.
Related
In a MySQL database, I have a string like 123456789. I want to add a decimal separator between every 3 digits, and thus turn it into 123.456.789.
How can I do this?
If you are using MySQL 8.0+, it is possible to use the REGEXP_REPLACE function:
SELECT REGEXP_REPLACE(YOUR_COLUMN, '([:digit:]{3})(?!$)', '$0.');
It adds a dot for each group of 3 digits, excepted for the last one.
Here are some examples:
123456789 returns 123.456.789
1234567891 returns 123.456.789.1
12345678910 returns 123.456.789.12
You can try it out on this DB Fiddle and you can change the regular expression to fit what you need.
However, this approach is probably not the best in terms of performance. If your query is called a lot of times, your database server will suffer since calculation is centralized on it. As #xNoJustice said, it is better to handle this string operation in the client part, where it will be divided between every client execution.
Use FORMAT() function with suitable locale.
fiddle, which shows all suitable locales.
This maybe an easy one but i couldn't get answer.
I need to select float value from table
example table :-
value
10.2
4.5
4.6
4.06
my query
SELECT * FROM table where value = '4.6'
returns empty result set
how can i solve this !
Generally, you should never check equality with floats (unless, potentially, you have the same object). Internally, it is represented with more precision, even if it isn't showing it to you by the time it outputs to the screen. This basic tenet holds true for computing in general.
There are a dozens of schemes for doing this, but here is a simple one, which should make sense:
SELECT * FROM table where value BETWEEN 4.599 AND 4.601
Use decimal instead of float.
A decimal(10,2) will have 2 and only 2 decimal places and can be compared in the same manner as integers.
Especially for monetairy values you should always use decimal, but anywhere where rounding errors are unwanted, decimal is a good choice.
See: http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html
Or
MySQL DECIMAL Data Type Characteristics
Today, I also came across the same situation and get resolved just by using FORMAT function of MySQL, It will return the results that exactly match your WHERE clause.
SELECT * FROM yourtable WHERE FORMAT(`col`,2) = FORMAT(value,2)
Explanation:
FORMAT('col name',precision of floating point number)
Hope it helps.
You can also try query
SELECT * FROM table WHERE value LIKE 4.6;
you write:
SELECT * FROM table where round(value, 1) = 4.6
I would like to extract the file extension from a field in MySQL that contains filenames. This means I need to find the final '.' character in the field and extract everything after that. The following code example partially works:
SELECT LCASE(RIGHT(filename, LENGTH(filename) - LOCATE('.', filename)))
FROM mytable;
except that it falls down for cases where the file name contains more than one '.', where it extracts too much. In most programming languages I'd expect to find a function that gives me a rightmost match, but I can't find any such thing for MySQL, nor can I find any discussion from people who have had the same problem and found a workaround.
There is the substring_index function - it does exactly what you are looking for:
SELECT substring_index(filename, '.', -1) FROM mytable
Edit:
See Martin's answer, using substring_index(), with a negative count parameter is a MUCH better approach!
I'm downvoting myself (actually that's not possible...), upvoting Martin's answer; ' wish I could pass the accepted answer to him... Maybe OP will do that.
Original answer:
The following may do the trick (ATN: length may be off by 1, also may want to deal with case of filename value without a dot character.
SELECT LCASE(RIGHT(filename, LOCATE('.', REVERSE(filename) ) ))
FROM mytable;
Beware however that this type of post-facto parsing can be quite expensive (read slow), and you may consider extracting the file extension to a separate column, at load time.
Somewhere in the midst of thousands of records I have a decimal value in a sql column that has an odd value.
Specifically, it has a decimal place of .002.
The actual amount could be pretty much anything like 238.002 or 543.002
So how can I write a query to find that?
How about something like this:
SELECT *
FROM myRows
WHERE (myVal - CAST(myVal AS INTEGER)) = 0.002;
Although, from the sound of it, it sort of smells like maybe you're storing your decimal values as FLOATs instead of NUMERIC or DECIMAL, which will cause errors of a whole different nature.
I'm looking to use SQL to format a number with commas in the thousands, but no decimal (so can't use Money) - any suggestions?
I'm using SQL Server 2005, but feel free to answer for others as well (like MySQL)
With TSQL you could cast to money and convert it will add the .00, but you could use replace or substring to remove.
replace(convert(varchar, cast(column as money), 1), '.00', '')
In SQL 2005 you could use a CLR function as well
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString FormatNumber(SqlInt32 number)
{
return number.Value.ToString("N0");
}
and call it as any other user-defined function
SELECT dbo.FormatNumber(value)
In MySQL, the FORMAT() function will do the trick.
In Oracle you can specify a format parameter to the to_char function:
TO_CHAR(1234, '9,999') --> 1,234
Any specific reason you want this done on the server side? Seems like it is a task better suited for the client/report.
Otherwise, you are storing a number as a string just so you can keep the formatting how you want it -- but you just lost the ability to do even basic arithmetic on it without having to reconvert it to a number.
If you're really determined to do it in SQL and have a justifiable reason for it, I guess my vote is on Scott's method: Value --> Money --> Varchar --> Trim off the decimal portion
-- Kevin Fairchild
For SQL Server, you could format the number as money and then delete the right-most three characters.
replace(convert (varchar, convert (money, 109999), 1), '.00','')