sql create a function - mysql

Could someone please help me with this one?
So I need to write a user input function in which I need to concatenate two strings. When outputted, there must be a space between the two strings, note there is not a space in the two strings when inputting them. Test functions with the following, String 1: Spring, String 2: Break!
This is my solution:
create function concatenate(X CHAR,Y CHAR)
Returns CHAR(50)
Return concat(X, ' ', Y);
select concatenate('Spring','Break')
However, the problem is that sql only returns the first letter of each word, which is "S B". But I want it to be "Spring Break"
Any ideas on this one? Helps are very appreciated

Supply a length for the input parameters as well:
create function concatenate(X CHAR(24),Y CHAR(24))
Returns CHAR(50)
Return concat(X, ' ', Y);
select concatenate('Spring','Break')

You need to define the size when you declare the argument.
create function con(X char(50), Y char(50))
returns char(100)

You have to specify the size of CHAR(), otherwise it will use the default CHAR(1), and you can't get want you want.
eg:
create function hello(x char(10),y char(10))
returns char(30) deterministic
return concat(x,' ',y)`
select hello('Hello','World');
Hello World

Related

MYSQL: Validate Input with Regular Expressions REGEX

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***
So my Solution until now is this (User 'Nick' helped me):
CREATE FUNCTION validate_number(testnumber VARCHAR(7))
RETURNS INT
DETERMINISTIC
RETURN testnumber REGEXP '^[[:alnum:]]{2}[[:alpha:]]?[[:digit:]]{1,4}$';
And this approach works for most cases.
But when i enter a value that exceeds the possible amount of elements (max elements = 7) i get no result.
example:
validate_number('00A00002345')
=> no result.
Do you guys have an idea what the problem is?
Thank you very much in advance.
you are actually pointing out the solution of the problem :)
just change VARCHAR(7) to something bigger VARCHAR(2000)
When I run your function, I get the error:
select validate_number('00A00002345')
Data too long for column 'testnumber' at row 1
You can add a length to the varchar.
CREATE FUNCTION validate_number (
in_testnumber VARCHAR(32000)
)
Or, use text:
CREATE FUNCTION validate_number (
in_testnumber text
)
RETURNS INT
DETERMINISTIC
BEGIN
RETURN (in_testnumber REGEXP '^[[:alnum:]]{2}[[:alpha:]]?[[:digit:]]{1,4}$');
END;

MySQL function doesn't work with unicode strings

I have a MySQL function which does some string replacement in a specified string. I simplyfied this function to return it's argument.
CREATE FUNCTION replace_details(
id INT UNSIGNED,
message VARCHAR(1000))
RETURNS VARCHAR(1000) LANGUAGE SQL
BEGIN
RETURN message;
END;
;;
This function is called from before insert trigger, so the inserted string is modified. Everything works as expected while I'm using english text, but as soon as I'm trying to insert something else (for example russian) it makes my string unreadable. Actually I get only ? sign in a database. If I'm using any other function in trigger it works fine
For example:
SET NEW.message = CONCAT(NEW.message, ' test');
but as soon as I'm using my custom function which actually does nothing, but returns what it got, inserted string becomes unreadable.
I guess I need to somehow specify that my function works with unicode, but how?
MySQL server version: 5.7.1-m11
I found it.
CREATE FUNCTION replace_details(
id INT UNSIGNED,
message VARCHAR(1000) CHARACTER SET UTF8)
RETURNS VARCHAR(1000) CHARACTER SET UTF8 LANGUAGE SQL
BEGIN
RETURN message;
END;
;;
works!

How do I create a QUOTENAME function in mySQL

I would like to create a QUOTENAME() function in mySQL like the one that exists in M$ SQL Server.
This is what it does:
QUOTENAME returns a Unicode string with the delimiters added to make
the input string a valid identifier. The QUOTENAME function uses this
syntax:
QUOTENAME ( 'string' [ , 'delimiter' ] )
You pass QUOTENAME a string to be delimited and a one-character string
to use as the delimiter. The delimiter can be a square bracket or a
single or double quotation mark.
Is this even possible?
You could start with something like this - building off of eggyal's comment
DROP FUNCTION IF EXISTS QUOTENAME;
CREATE FUNCTION QUOTENAME (s varchar(50), d CHAR(1))
RETURNS VARCHAR(52)
RETURN CONCAT (d, REPLACE(s, d, CONCAT(d,d)), d);
And then add your special cases and additional functionality.

Function with return values

I have written a function which takes the Name as input and returns Suffix.
I want to return the position of suffix in a name with a function.
How can i do that.
Could any one please help me doing it.
there is no need to create special function fot this. it already exists in t-sql.
its name is PATINDEX
Example:
declare #pat varchar(128)
set #pat = '_suf'
select login, Patindex('%'+#pat, login) as suffix_index from clients

number_format() with MySQL

hey i need a way to get a formated number from my column decimal(23,2) NOT NULL DEFAULT '0.00'
in php i could use this function number_format('1111.00', 2, ',', '.');
it would return 1.111,00 (in Germany we use , to define decimal numbers)
how would i do this in mysql? with string replaces?
http://blogs.mysql.com/peterg/2009/04/
In Mysql 6.1 you will be able to do FORMAT(X,D [,locale_name] )
As in
SELECT format(1234567,2,’de_DE’);
For now this ability does not exist, though you MAY be able to set your locale in your database my.ini check it out.
With performance penalty and if you need todo it only in SQL you can use the FORMAT function and 3 REPLACE :
After the format replace the . with another char for example #, then replace the , with a . and then the chararacter you choose by a , which lead you for your example to 1.111,00
SELECT REPLACE(REPLACE(REPLACE(FORMAT("1111.00", 2), ".", "#"), ",", "."), "#", ",")
You can use
SELECT round(123.4566,2) -> 123.46
FORMAT(X,D) 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.
SELECT FORMAT(12332.123456, 4);
-> '12,332.1235'
Antonio's answer
CONCAT(REPLACE(FORMAT(number,0),',','.'),',',SUBSTRING_INDEX(FORMAT(number,2),'.',-1))
is wrong; it may produce incorrect results!
For example, if "number" is 12345.67, the resulting string would be:
'12.346,67'
instead of
'12.345,67'
because FORMAT(number,0) rounds "number" up if fractional part is greater or equal than 0.5 (as it is in my example)!
What you COULD use is
CONCAT(REPLACE(FORMAT(FLOOR(number),0),',','.'),',',SUBSTRING_INDEX(FORMAT(number,2),'.',-1))
if your MySQL/MariaDB's FORMAT doesn't support "locale_name" (see MindStalker's post - Thx 4 that, pal). Note the FLOOR function I've added.
At least as far back as MySQL 5.5 you can use format:
SELECT FORMAT(123456789.123456789,2);
/* produces 123,456,789.12 */
SELECT FORMAT(123456789.123456789,2,'de_DE');
/*
produces 123.456.789,12
note the swapped . and , for the de_DE locale (German-Germany)
*/
From the MySQL docs:
https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_format
Available locales are listed elsewhere in the docs:
https://dev.mysql.com/doc/refman/5.5/en/locale-support.html
CREATE DEFINER=`yourfunctionname`#`%` FUNCTION `money`(
`betrag` DECIMAL(10,2)
)
RETURNS varchar(128) CHARSET latin1
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
return(
select replace(format(cast(betrag as char),2),',',"'") as betrag
)
will creating a MySql-Function with this Code:
select replace(format(cast(amount as char),2),',',"'") as amount_formated
You need this:
CONCAT(REPLACE(FORMAT(number,0),',','.'),',',SUBSTRING_INDEX(FORMAT(number,2),'.',-1))