Convert decimal to euro currency in MYSQL - mysql

What is the best way to select a decimal value from the mysql database and convert it to an euro currrency in the mysql selection.
When i select "format(multiplier * (coalesce(Invoice.rent, 0) + coalesce(Invoice.furnished, 0) + coalesce(Invoice.gwe, 0) + coalesce(Invoice.service, 0) + coalesce(Invoice.gas, 0) + coalesce(Invoice.electricity, 0) + coalesce(Invoice.water, 0) + coalesce(Invoice.heat, 0) + coalesce(Invoice.tax, 0) + coalesce(Invoice.internet_tv, 0)) - coalesce(sum(Payment.amount),0),2)"
It returns a number like this 1,000.25 i want it to be 1.000,25 what is the best way to do this in MYSQL select query?

The documentation reveals that FORMAT() has up to three arguments (emphasis mine):
FORMAT(X,D[,locale])
Formats the number X to a format like '#,###,###.##', rounded to D
decimal places, and returns the result as a string. If D is 0, the
result has no decimal point or fractional part.
The optional third parameter enables a locale to be specified to be
used for the result number's decimal point, thousands separator, and
grouping between separators. Permissible locale values are the same as
the legal values for the lc_time_names system variable (see Section
10.7, “MySQL Server Locale Support”). If no locale is specified, the default is 'en_US'.
That could be a starting point. (You apparently want the format provided by the de_DE locale.)
Demo

In database you cant change default decimal separator from . to , for reason described here insert-non-english-decimal-points-in-mysql.
You can use this function to format your output value:
CREATE FUNCTION f_change_decimal_separator(num_value VARCHAR(16)) RETURNS VARCHAR(16)
BEGIN
DECLARE char_value VARCHAR(16);
SET char_value = REPLACE(num_value, ".", ";");
SET char_value = REPLACE(char_value, ",", ".");
SET char_value = REPLACE(char_value, ";", ",");
RETURN char_value;
END;

Related

SQL: Substring until second character starting from right keeping left values

I have this filename AAAA_BBBBB_CC_HDDD_HGGG.csv and I'm trying to keep the values after the second underscore starting from the right.
So i want to keep any values just before _HDDD_HGGG.csv
This is my code:
SET #NFileN = REVERSE(SUBSTRING(REVERSE(#source_filename),1,CHARINDEX('_',REVERSE(#source_filename), CHARINDEX('_', REVERSE (#source_filename), 0) + 1)))
And this is the returned value:
(6 rows affected)
_HDDD_HGGG.csv
Instead of being AAAA_BBBBB_CC.
Does anyone has a clue for this?
You are taking a SUBSTRING from 1 till your CHARINDEX while your string is reversed. Either reverse your string again or use LEN to find the length of your string like so:
REVERSE(
SUBSTRING(
REVERSE(#source_filename),
CHARINDEX('_',
REVERSE(#source_filename),
CHARINDEX('_',
REVERSE (#source_filename),
0)+1)+1,
LEN(#source_filename)
)
)
p.s.: Added a second +1 to remove the "_" between CC and HDDD
p.p.s: CHARINDEX is a SQL Server function which I assume is what you are actually using. The MySQL equivalent would be POSITION, the equivalent for LEN would be LENGTH

MYSQL: Validate Input Values in Specific Format within a FUNCTION

i have one question with regard to MYSQL. I want to create a function that is able to check whether an Input is given in a specific format.
The output should be in the following shape:
***x x (a) n (n) (n) (n)
with :
x = letters and numbers
n = numbers
a = letters
brackets = optional Values***
my code is written below this.
CREATE FUNCTION validate_of_number(testnumber VARCHAR(7))
RETURNS INT
DETERMINISTIC
RETURN
CASE
WHEN
(SELECT * FROM flightexecution WHERE FlightNo REGEXP
'^[[:alnum:]+[:alnum:]] + [[:alpha:]|''] + [:digit:] +
[[:digit:]|''] + [[:digit:]|''] + [[:digit:]|'']') > 0
Then 1
Else 0
END;`
However, it does not work and i don´t know why. The Output is just a 'OK' without any further information.
I'm assuming based on your description of valid values and the context of your question that the values you are trying to validate look something like CX727 or QF1566 or BA1 etc (i.e. IATA flight designator codes). In that case, this function will give you the results you want:
CREATE FUNCTION validate_of_number(testnumber VARCHAR(7))
RETURNS INT
DETERMINISTIC
RETURN testnumber REGEXP '^[[:alnum:]]{2}[[:alpha:]]?[[:digit:]]{1,4}$';
Examples:
SELECT validate_of_number('A1B4352')
, validate_of_number('QF12')
, validate_of_number('CX727')
, validate_of_number('AB14352')
, validate_of_number('BA1')
, validate_of_number('1C42')
Output
1, 1, 1, 0, 1, 1
Demo on dbfiddle
In terms of using it with your table you might use a query like
SELECT * FROM flightexecution WHERE validate_of_number(FlightNo)

String to double or decimal

How to convert a string to a double or decimal? In Exact Online (REST API) I try to calculate with a decimal value in a string field. e.g items.netprice + items.notes. The field items.notes contains the decimal value.
Tried using cast and convert in combination with float and decimal.
I would use a solution like:
select case
when regexp_replace(c, '[^0-9.]', '', 1, 0, 'g') = c
then to_number(c)
else null
end
from ( select '123.45' c
from dual#datadictionary
union all
select '123invalid.45' c
from dual#datadictionary
)
The case with regexp_replace ensures that non-number are returned as null. You might want to change that to an error if deemed necessary.

How to format int to price format in SQL?

I select the price 1000000 and I need to format it to $1,000,000. How can I do that in SQL?
To format with commas, you can use CONVERT with a style of 1:
declare #money money = 1000000
select '$' + convert(varchar, #money, 1)
will produce $1,000,000.00
If you want to remove the last 3 characters:
select '$' + left(convert(varchar, #money, 1), charindex('.', convert(varchar, #money, 1)) - 1)
and if you want to round rather than truncate:
select '$' + left(convert(varchar, #money + $0.50, 1), charindex('.', convert(varchar, #money, 1)) - 1)
Creating Function:
CREATE FUNCTION [dbo].[f_FormatMoneyValue]
(
#MoneyValue money
)
RETURNS VARCHAR(50)
AS
BEGIN
RETURN cast(#MoneyValue as numeric(36,2))
END
Using in Select Query:
Select dbo.f_FormatMoneyValue(isnull(SalesPrice,0))SalesPrice from SalesOrder
Output:
100.00
Formatting Money Value with '$' sign:
CREATE FUNCTION [dbo].[f_FormatMoneyWithDollar]
(
#MoneyValue money
)
RETURNS VARCHAR(50)
AS
BEGIN
RETURN '$' + convert(varchar, #MoneyValue, 1)
END
Output:
$100.00
Note: The above sample is for the money field. You can modify this function according to your needs
Hope this helps you..! :D
SELECT FORMAT(price, 'C2', 'en-us')
The SQL Server money datatype is just decimal(10, 4). To my knowledge there is no datatype that will present the way you want.
Adding the dollar sign and commas is something that should belong in the application logic, but if you really must do it through a database object consider adding the dollar sign, and commas every three characters (after the decimal point). In other words, you'll have to convert the int to varchar and do string manipulation.
It depends, however, there's no simple way to do it in standard SQL specs(SQL-92, SQL-2003, etc.).
For PostgreSQL PL/pgSQL and Oracle PL/SQL, you can use to_char to format numbers:
select to_char(1234567.123, 'FM$999,999,999.99')
Which gives output:
$1,234,567.12
See: http://www.postgresql.org/docs/7/static/functions2976.htm

Adding the numbers of characters in a record (mysql)

I need to add the number of characters that there are in each of records.
SELECT
CHAR_LENGTH(fiedl1) +
CHAR_LENGTH(fiedl2) +
CHAR_LENGTH(fiedl3) +
CHAR_LENGTH(fiedl4) +
CHAR_LENGTH(fiedl5) +
CHAR_LENGTH(fiedl6)
FROM mytable;
The above code is not valid because if a field contains NULL values then it returns NULL.
You can use the IFNULL Control Flow Function.
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.
Your query should be something like this:
SELECT
IFNULL(CHAR_LENGTH(fiedl1), 0) +
IFNULL(CHAR_LENGTH(fiedl2), 0) +
IFNULL(CHAR_LENGTH(fiedl3), 0) +
IFNULL(CHAR_LENGTH(fiedl4), 0) +
IFNULL(CHAR_LENGTH(fiedl5), 0) +
IFNULL(CHAR_LENGTH(fiedl6), 0)
FROM mytable;
Let me know if you have any doubts.
Try using IFNULL, like so:
IFNULL(CHAR_LENGTH(fiedl1), 0) ...