Creating funcion for short date in spanish - mysql

Hi guys this function gives me this error: "Something wrong in line 35." (RETURN diaCompleto;), I dont know whats wrong with the code below anyway can point me out in the correct direction, tyvm.
DELIMITER $$
CREATE FUNCTION DIA_FECHA(fecha date)
RETURNS VARCHAR(8)
BEGIN
DECLARE nomDia VARCHAR(6);
DECLARE dia VARCHAR(2);
DECLARE diaCompleto VARCHAR(8);
SET dia = LPAD(DAY(fecha),2,'0');
IF(DAYOFWEEK(fecha) = 1) THEN
SET nomDia = 'DOM - ';
IF(DAYOFWEEK(fecha) = 2) THEN
SET nomDia = 'LUN - ';
IF(DAYOFWEEK(fecha) = 3) THEN
SET nomDia = 'MAR - ';
IF(DAYOFWEEK(fecha) = 4) THEN
SET nomDia = 'MIE - ';
IF(DAYOFWEEK(fecha) = 5) THEN
SET nomDia = 'JUE - ';
IF(DAYOFWEEK(fecha) = 6) THEN
SET nomDia = 'VIE - ';
IF(DAYOFWEEK(fecha) = 7) THEN
SET nomDia = 'SAB - ';
SET diaCompleto = CONCAT(nomDia,dia);
RETURN diaCompleto;
END$$
DELIMITER ;

The reason for your syntax error is that you need to use END IF; after each IF/THEN. See examples in the manual: https://dev.mysql.com/doc/refman/8.0/en/if.html
I think you don't need to write this function at all. Instead, you could use MySQL's DATE_FORMAT() function. It supports locale-aware names for days of the week.
mysql> set lc_time_names = 'es_ES';
mysql> select date_format(curdate(), '%a - %d') as diaCompleto;
+-------------+
| diaCompleto |
+-------------+
| sáb - 19 |
+-------------+

Related

MySQL CONCAT DOUBLE values in trigger

UPDATE
The question needs clarity.
I'm converting converting milliseconds to hours minutes seconds to create a date independent timestamp.
Given time in milliseconds, I want the output to be a string in H:M:S
E.g.
9999999ms = 2.7777775 hrs
.7777775hrs * 60 = 46.66665 min
.66665min * 60 = 39.999sec
Desired output 02:46:39.999
This has nothing to do with TIMESTAMP. It is a simple calculation followed by a string concatenation.
I'm having a frustrating time with the CONCATENATION. IT only returns hours and it does not ROUND the returned value.
DECLARE v_timestamp VARCHAR(8);
DECLARE v_hours DOUBLE(5,3);
DECLARE v_minutes DOUBLE(5,3);
DECLARE v_seconds DOUBLE(5,3);
SET v_hours = (NEW.amount_viewed_ms)/(3600000);
SET v_minutes = (v_hours - FLOOR(v_hours)) * 60;
SET v_seconds = (v_minutes - FLOOR(v_minutes)) * 60;
SET v_timestamp = CONCAT(ROUND(v_hours), ":", ROUND(v_minutes), ":", ROUND(v_seconds));
SET NEW.timestamp = v_timestamp;
I could use another pair of eyes to help figure out where this is going wrong.
I really don't see your problem simply increasing a couple of field sizes seems to produce a desired result without an example of NEW.amount_viewed_ms it's not possible to say more
DROP PROCEDURE IF EXISTS P;
DELIMITER $$
CREATE PROCEDURE P()
BEGIN
DECLARE v_timestamp VARCHAR(20);
DECLARE v_hours double(20,3);
DECLARE v_minutes DOUBLE(5,3);
DECLARE v_seconds DOUBLE(5,3);
select (UNIX_TIMESTAMP(CONCAT(DATE(NOW()), ' ', CURTIME(3))))/(3600000);
SET v_hours = (UNIX_TIMESTAMP(CONCAT(DATE(NOW()), ' ', CURTIME(3))))/(3600000);
SET v_minutes = (v_hours - FLOOR(v_hours)) * 60;
SET v_seconds = (v_minutes - FLOOR(v_minutes)) * 60;
select v_hours,v_minutes,v_seconds, (UNIX_TIMESTAMP(CONCAT(DATE(NOW()), ' ', CURTIME(3))))/(3600000);
SET v_timestamp = CONCAT(ROUND(v_hours), ":", ROUND(v_minutes), ":", ROUND(v_seconds));
select v_timestamp;
END $$
DELIMITER ;
CALL P();
MariaDB [sandbox]> call p();
+------------------------------------------------------------------+
| (UNIX_TIMESTAMP(CONCAT(DATE(NOW()), ' ', CURTIME(3))))/(3600000) |
+------------------------------------------------------------------+
| 457.2335910 |
+------------------------------------------------------------------+
1 row in set (0.002 sec)
+---------+-----------+-----------+------------------------------------------------------------------+
| v_hours | v_minutes | v_seconds | (UNIX_TIMESTAMP(CONCAT(DATE(NOW()), ' ', CURTIME(3))))/(3600000) |
+---------+-----------+-----------+------------------------------------------------------------------+
| 457.234 | 14.040 | 2.400 | 457.2335910 |
+---------+-----------+-----------+------------------------------------------------------------------+
1 row in set (0.025 sec)
+-------------+
| v_timestamp |
+-------------+
| 457:14:2 |
+-------------+
1 row in set (0.032 sec)

Return value from DATE_SUB/DATE_ADD in stored function

I am trying to write a stored function that takes a string in proper ISO format (yyyy-mm-dd) and subract a certain number of weekdays from it. Based off of this question here I have tried both the accepted answer as well as one a few answers down that does things differently, however, both of them are just saying how to write the pure sql, and don't have an example of a function.
What I currently have is this:
delimiter //
CREATE DEFINER=`root`#`localhost` FUNCTION `WEEKDATE_SUB` (days TINYINT, date_val VARCHAR(16))
RETURNS DATE DETERMINISTIC
BEGIN
DECLARE SUBVAL INT;
DECLARE dow INT;
CASE
WHEN dow=1 THEN SET SUBVAL = (days +(FLOOR((days-0.5)/5)+1)*2 - 1);
WHEN dow=2 THEN SET SUBVAL = (days +(FLOOR((days-0.5)/5)+1)*2);
WHEN dow=3 THEN SET SUBVAL = (days-1 +(FLOOR(((days-1)-0.5)/5)+1)*2 + 1);
WHEN dow=4 THEN SET SUBVAL = (days-2 +(FLOOR(((days-2)-0.5)/5)+1)*2 + 2);
WHEN dow=5 THEN SET SUBVAL = (days-3 +(FLOOR(((days-3)-0.5)/5)+1)*2 + 3);
WHEN dow=6 THEN SET SUBVAL = (days-4 +(FLOOR(((days-4)-0.5)/5)+1)*2 + 4);
WHEN dow=7 THEN SET SUBVAL = (days-5 +(FLOOR(((days-5)-0.5)/5)+1)*2 + 5);
END CASE
RETURN DATE_SUB(date_val, INTERVAL SUBVAL DAY);
END;//
When I try to add it, I get a vague error (as mysql is fond of offering):
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'RETURN DATE_SUB(date_val, INTERVAL SUBVAL DAY);
END' at line 14
I have tried several variations on returning including trying to define a variable for Date sub and return that, but it's pretty much the same error.
Outside of a function, I know this works, so it seems like I should just be able to return that.
SELECT DATE_SUB("2016-01-01", INTERVAL 4 DAY);
The docs suggest you need to have END CASE, instead of END
Additionally, the stored function will probably execute a little faster if you use the CASE expr WHEN value2 THEN .... WHEN value2 THEN ... END CASE version, since it will not have to repeat the DAYOFWEEK function calls potentially 7 times.
you can also use something like this
delimiter //
CREATE DEFINER=`root`#`localhost` FUNCTION `WEEKDATE_SUB` (date_val VARCHAR(10), days TINYINT)
RETURNS VARCHAR(10) DETERMINISTIC
BEGIN
RETURN date_val - INTERVAL
FLOOR(days/5)*7 +
IF(DAYOFWEEK(date_val)-1 <= days - FLOOR(days/5)*5
, (days - FLOOR(days/5)*5)+2
, days - FLOOR(days/5)*5
) DAY;
END;//
samples
mysql> SELECT WEEKDATE_SUB('2017-02-06',1);
+------------------------------+
| WEEKDATE_SUB('2017-02-06',1) |
+------------------------------+
| 2017-02-03 |
+------------------------------+
1 row in set (0,00 sec)
mysql> SELECT WEEKDATE_SUB('2017-02-07',1);
+------------------------------+
| WEEKDATE_SUB('2017-02-07',1) |
+------------------------------+
| 2017-02-06 |
+------------------------------+
1 row in set (0,00 sec)
mysql> SELECT WEEKDATE_SUB('2017-02-07',2);
+------------------------------+
| WEEKDATE_SUB('2017-02-07',2) |
+------------------------------+
| 2017-02-03 |
+------------------------------+
1 row in set (0,00 sec)
mysql>
This is what I finally needed to get this working. Notice, the ; and the change to how I am using the CASE statement.
delimiter //
CREATE DEFINER=`root`#`localhost` FUNCTION `WEEKDATE_SUB` (date_val VARCHAR(10), days TINYINT)
RETURNS VARCHAR(10) DETERMINISTIC
BEGIN
DECLARE SUBVAL INT;
DECLARE dow INT;
SET dow = DAYOFWEEK(date_val);
CASE dow
WHEN 1 THEN SET SUBVAL = (days +(FLOOR((days-0.5)/5)+1)*2 - 1);
WHEN 2 THEN SET SUBVAL = (days +(FLOOR((days-0.5)/5)+1)*2);
WHEN 3 THEN SET SUBVAL = (days-1 +(FLOOR(((days-1)-0.5)/5)+1)*2 + 1);
WHEN 4 THEN SET SUBVAL = (days-2 +(FLOOR(((days-2)-0.5)/5)+1)*2 + 2);
WHEN 5 THEN SET SUBVAL = (days-3 +(FLOOR(((days-3)-0.5)/5)+1)*2 + 3);
WHEN 6 THEN SET SUBVAL = (days-4 +(FLOOR(((days-4)-0.5)/5)+1)*2 + 4);
WHEN 7 THEN SET SUBVAL = (days-5 +(FLOOR(((days-5)-0.5)/5)+1)*2 + 5);
ELSE SET SUBVAL = days;
END CASE;
RETURN DATE_SUB(date_val, INTERVAL SUBVAL DAY);
END;//

Convert CamelCase to camel_case in MySQL

Is there a way to convert CamelCase to camel_case using only mysql? I know I can do it in php but I wanna be able to do it in mysql, because I need to convert millions of strings.
Here's a solution:
delimiter $$
drop function if exists replaceCamelCase $$
create function replaceCamelCase( p_str TEXT ) returns text
BEGIN
declare v_pos int;
declare v_len int;
declare cnt int;
declare tmp text;
declare ret text;
set v_pos=1;
set v_len=1+char_length( p_str );
set ret = '';
set cnt = 0;
if p_str REGEXP('[^_]') then
while (v_pos<v_len)
do
set tmp = SUBSTR(p_str, v_pos, 1);
if tmp REGEXP BINARY '[A-Z]' = 1 then
if cnt > 0 then
set ret = concat(ret, '_');
end if;
set ret = concat(ret, lower(tmp));
set cnt = cnt + 1;
else
set ret = concat(ret, tmp);
end if;
set v_pos = v_pos + 1;
end while;
else
set ret = p_str;
end if;
RETURN ret;
end $$
User it like this:
SELECT replaceCamelCase(name) FROM `test` WHERE 1
Example input/output:
BlaTest
test_test
BaldieBal
TestStringCase
Output:
bla_test
test_test
baldie_bal
test_string_case
Knock yourself out - but see my comment above:
SELECT REPLACE('CamelCase', BINARY 'C','_c');
+---------------------------------------+
| REPLACE('CamelCase', BINARY 'C','_c') |
+---------------------------------------+
| _camel_case |
+---------------------------------------+
You can try following code :-
$result = strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $subject));
Converts:
HelloKittyOlolo
Declaration
CrabCoreForefer
TestTest
To:
hello_kitty_ololo
declaration
crab_core_forefer
test_test
It may help you.

MySQL convert Degree, Minutes, Seconds to Degree decimal

I have multiple rows of Degrees Minutes Seconds that I need to convert with a query.
36°19'11.46" N = 36.31985
95°36'02.22" W = 95.600617
Each row is going to be different. I've been stuck on this for two days. Is this even possible?
Nice lifehack: reverse problem solution (degree to DMS) using SEC_TO_TIME built-in MySQL function:
CREATE FUNCTION `geocoords`(lon double, lat double) RETURNS varchar(24) CHARSET cp1251
NO SQL
DETERMINISTIC
begin
declare alon double;
declare alat double;
declare slon varchar(12);
declare slat varchar(12);
set alon = abs(lon);
set alat = abs(lat);
set slon = TIME_FORMAT(SEC_TO_TIME(alon*3600), '%H°%i''%s"');
set slat = TIME_FORMAT(SEC_TO_TIME(alat*3600), '%H°%i''%s"');
if lon>0 then
set slon = concat(slon, 'E');
elseif lon<0 then
set slon = concat(slon, 'W');
end if;
if lat>0 then
set slat = concat(slat, 'N');
elseif lat<0 then
set slat = concat(slat, 'S');
end if;
return concat(slat, ' ', slon);
end
SELECT geocoords(30.550157546997, 50.344024658203)
50°20'38"N 30°33'01"E
The following should work:
SELECT D + M/60 + S/3600;
For example, in MySQL:
SELECT 36 + 19/60 + 11.46/3600;
returns: 36.319850
I ended up building this, and it worked flawlessly with what I needed. You will note that I added a C to the numbers, this is to flag them, so if it had already been converted it wouldn't continue to convert.
UPDATE
MasterTable
SET
MasterTable.Latitude_A = MasterTable.Latitude,
MasterTable.Longitude_A = MasterTable.Longitude
WHERE
ProjectID = 'ProjectAlpha'
and Sequence = '0'
and MasterTable.Latitude NOT LIKE '%C%'
and MasterTable.Longitude NOT LIKE '%C%';
TRUNCATE TABLE gpsconvert;
INSERT into gpsconvert(gpsconvert.`Account Number`,gpsconvert.Latitude,gpsconvert.Longitude)
SELECT
MasterTable.AccountNumber,
MasterTable.Latitude,
MasterTable.Longitude
FROM
MasterTable
WHERE
MasterTable.ProjectID = 'ProjectAlpha'
and MasterTable.Sequence = '0'
and MasterTable.Latitude NOT LIKE '%c%'
and MasterTable.Longitude NOT LIKE '%c%'
and MasterTable.Latitude <> ''
and MasterTable.Longitude <> '';
UPDATE
gpsconvert
SET
gpsconvert.LatDegree = LEFT(gpsconvert.Latitude,2),
gpsconvert.LatMinutes = SUBSTRING(gpsconvert.Latitude,-7,2),
gpsconvert.LatSeconds = SUBSTRING(gpsconvert.latitude,-5,5),
gpsconvert.LatDecimal = gpsconvert.LatDegree + (gpsconvert.LatMinutes/60) + (gpsconvert.LatSeconds/3600),
gpsconvert.LongDegree = LEFT(gpsconvert.Longitude,2),
gpsconvert.LongMinutes = SUBSTRING(gpsconvert.Longitude,-7,2),
gpsconvert.LongSeconds = SUBSTRING(gpsconvert.Longitude,-5,5),
gpsconvert.LongDecimal = gpsconvert.LongDegree + (gpsconvert.LongMinutes/60) + (gpsconvert.LongSeconds/3600);
UPDATE
MasterTable
INNER JOIN
gpsconvert on gpsconvert.`Account Number` = MasterTable.AccountNumber
SET
MasterTable.Latitude = CONCAT(gpsconvert.LatDecimal,'c'),
MasterTable.Longitude = CONCAT(gpsconvert.LongDecimal,'c')
WHERE
MasterTable.ProjectID = 'ProjectAlpha'
and MasterTable.Sequence = '0'
and MasterTable.AccountNumber = gpsconvert.`Account Number`
This converts strings in the form 21°48'6.384" to decimal
CREATE FUNCTION database.DMS_TO_DECIMAL(dms varchar(20))
RETURNS decimal(12,9)
BEGIN
DECLARE deg decimal(12,9);
DECLARE mins decimal(12,9);
DECLARE secs decimal(12,9);
SET deg = CAST( SUBSTRING_INDEX(dms, '°', 1) AS decimal(12,9));
SET mins = CAST( (SUBSTR(dms, POSITION('°' IN dms) + 1, POSITION("'" IN dms) - POSITION('°' IN dms) - 1)) AS decimal(12,9));
SET secs = CAST( (SUBSTR(dms, POSITION("'" IN dms) + 1, POSITION("\"" IN dms) - POSITION("'" IN dms) - 1)) AS decimal(12,9));
RETURN deg + mins/60 + secs/3600;
END
Add orientation (W/S):
DROP FUNCTION IF EXISTS `DmsToDecimal`;
DELIMITER $$
CREATE FUNCTION `DmsToDecimal`(`dms` VARCHAR(50))
RETURNS DECIMAL(12,9)
DETERMINISTIC
NO SQL
SQL SECURITY INVOKER
BEGIN
DECLARE deg decimal(12,9);
DECLARE mins decimal(12,9);
DECLARE secs decimal(12,9);
DECLARE sign integer;
SET dms = UPPER(TRIM(REPLACE(dms, ",", ".")));
SET deg = CAST( SUBSTRING_INDEX(dms, '°', 1) AS decimal(12,9));
SET mins = CAST( (SUBSTR(dms, POSITION('°' IN dms) + 1, POSITION("'" IN dms) - POSITION("°" IN dms) - 1)) AS decimal(12,9));
SET secs = CAST( (SUBSTR(dms, POSITION("'" IN dms) + 1, POSITION("""" IN dms) - POSITION("'" IN dms) - 1)) AS decimal(12,9));
SET sign = 1 - 2 * (RIGHT(dms, 1) = "W" OR RIGHT(dms, 1) = "S";
RETURN sign * (deg + mins / 60 + secs / 3600);
END$$
DELIMITER ;

Capitalize first letter. MySQL

Does any one know the equivalent to this TSQL in MySQL parlance?
I am trying to capitalize the first letter of each entry.
UPDATE tb_Company SET CompanyIndustry = UPPER(LEFT(CompanyIndustry, 1))
+ SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))
It's almost the same, you just have to change to use the CONCAT() function instead of the + operator :
UPDATE tb_Company
SET CompanyIndustry = CONCAT(UCASE(LEFT(CompanyIndustry, 1)),
SUBSTRING(CompanyIndustry, 2));
This would turn hello to Hello, wOrLd to WOrLd, BLABLA to BLABLA, etc. If you want to upper-case the first letter and lower-case the other, you just have to use LCASE function :
UPDATE tb_Company
SET CompanyIndustry = CONCAT(UCASE(LEFT(CompanyIndustry, 1)),
LCASE(SUBSTRING(CompanyIndustry, 2)));
Note that UPPER and UCASE do the same thing.
Vincents excellent answer for Uppercase First Letter works great for the first letter only capitalization of an entire column string..
BUT what if you want to Uppercase the First Letter of EVERY word in the strings of a table column?
eg: "Abbeville High School"
I hadn't found an answer to this in Stackoverflow. I had to cobble together a few answers I found in Google to provide a solid solution to the above example. Its not a native function but a user created function which MySQL version 5+ allows.
If you have Super/Admin user status on MySQL or have a local mysql installation on your own computer you can create a FUNCTION (like a stored procedure) which sits in your database and can be used in all future SQL query on any part of the db.
The function I created allows me to use this new function I called "UC_Words" just like the built in native functions of MySQL so that I can update a complete column like this:
UPDATE Table_name
SET column_name = UC_Words(column_name)
To insert the function code, I changed the MySQL standard delimiter(;) whilst creating the function, and then reset it back to normal after the function creation script. I also personally wanted the output to be in UTF8 CHARSET too.
Function creation =
DELIMITER ||
CREATE FUNCTION `UC_Words`( str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET utf8 DETERMINISTIC
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(255);
DECLARE i INT DEFAULT 1;
DECLARE bool INT DEFAULT 1;
DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!#;:?/';
SET s = LCASE( str );
WHILE i < LENGTH( str ) DO
BEGIN
SET c = SUBSTRING( s, i, 1 );
IF LOCATE( c, punct ) > 0 THEN
SET bool = 1;
ELSEIF bool=1 THEN
BEGIN
IF c >= 'a' AND c <= 'z' THEN
BEGIN
SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
SET bool = 0;
END;
ELSEIF c >= '0' AND c <= '9' THEN
SET bool = 0;
END IF;
END;
END IF;
SET i = i+1;
END;
END WHILE;
RETURN s;
END ||
DELIMITER ;
This works a treat outputting Uppercase first letters on multiple words within a string.
Assuming your MySQL login username has sufficient privileges - if not, and you cant set up a temporary DB on your personal machine to convert your tables, then ask your shared hosting provider if they will set this function for you.
You can use a combination of UCASE(), MID() and CONCAT():
SELECT CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS name FROM names;
mysql> SELECT schedule_type AS Schedule FROM ad_campaign limit 1;
+----------+
| Schedule |
+----------+
| ENDDATE |
+----------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT(UCASE(MID(schedule_type,1,1)),LCASE(MID(schedule_type,2))) AS Schedule FROM ad_campaign limit 1;
+----------+
| Schedule |
+----------+
| Enddate |
+----------+
1 row in set (0.00 sec)
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_mid
This is working nicely.
UPDATE state SET name = CONCAT(UCASE(LEFT(name, 1)), LCASE(SUBSTRING(name, 2)));
http://forge.mysql.com/tools/tool.php?id=201
If there are more than 1 word in the column, then this will not work as shown below.
The UDF mentioned above may help in such case.
mysql> select * from names;
+--------------+
| name |
+--------------+
| john abraham |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS name FROM names;
+--------------+
| name |
+--------------+
| John abraham |
+--------------+
1 row in set (0.00 sec)
Or maybe this one will help...
https://github.com/mysqludf/lib_mysqludf_str#str_ucwords
UPDATE tb_Company SET CompanyIndustry = UCASE(LEFT(CompanyIndustry, 1)) +
SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))
CREATE A FUNCTION:
CREATE DEFINER=`root`#`localhost` FUNCTION `UC_FIRST`(`oldWord` VARCHAR(255))
RETURNS varchar(255) CHARSET utf8
RETURN CONCAT( UCASE( LEFT(oldWord, 1)), LCASE(SUBSTRING(oldWord, 2)))
USE THE FUNCTION
UPDATE tbl_name SET col_name = UC_FIRST(col_name);
If anyone try to capitalize the every word separate by space...
CREATE FUNCTION response(name VARCHAR(40)) RETURNS VARCHAR(200) DETERMINISTIC
BEGIN
set #m='';
set #c=0;
set #l=1;
while #c <= char_length(name)-char_length(replace(name,' ','')) do
set #c = #c+1;
set #p = SUBSTRING_INDEX(name,' ',#c);
set #k = substring(name,#l,char_length(#p)-#l+1);
set #l = char_length(#k)+2;
set #m = concat(#m,ucase(left(#k,1)),lcase(substring(#k,2)),' ');
end while;
return trim(#m);
END;
CREATE PROCEDURE updateNames()
BEGIN
SELECT response(name) AS name FROM names;
END;
Result
+--------------+
| name |
+--------------+
| Abdul Karim |
+--------------+
This should work nicely:
UPDATE tb_Company SET CompanyIndustry =
CONCAT(UPPER(LEFT(CompanyIndustry, 1)), SUBSTRING(CompanyIndustry, 2))
UPDATE users
SET first_name = CONCAT(UCASE(LEFT(first_name, 1)),
LCASE(SUBSTRING(first_name, 2)))
,last_name = CONCAT(UCASE(LEFT(last_name, 1)),
LCASE(SUBSTRING(last_name, 2)));
select CONCAT(UCASE(LEFT('CHRIS', 1)),SUBSTRING(lower('CHRIS'),2));
Above statement can be used for first letter CAPS and rest as lower case.
The solution in PostgreSQL (as far as googling may lead to this page)
INITCAP(firstname || ' ' || lastname) AS fullname
Uso algo simples assim ;)
DELIMITER $$
DROP FUNCTION IF EXISTS `uc_frist` $$
CREATE FUNCTION `uc_frist` (str VARCHAR(200)) RETURNS varchar(200)
BEGIN
set str:= lcase(str);
set str:= CONCAT(UCASE(LEFT(str, 1)),SUBSTRING(str, 2));
set str:= REPLACE(str, ' a', ' A');
set str:= REPLACE(str, ' b', ' B');
set str:= REPLACE(str, ' c', ' C');
set str:= REPLACE(str, ' d', ' D');
set str:= REPLACE(str, ' e', ' E');
set str:= REPLACE(str, ' f', ' F');
set str:= REPLACE(str, ' g', ' G');
set str:= REPLACE(str, ' h', ' H');
set str:= REPLACE(str, ' i', ' I');
set str:= REPLACE(str, ' j', ' J');
set str:= REPLACE(str, ' k', ' K');
set str:= REPLACE(str, ' l', ' L');
set str:= REPLACE(str, ' m', ' M');
set str:= REPLACE(str, ' n', ' N');
set str:= REPLACE(str, ' o', ' O');
set str:= REPLACE(str, ' p', ' P');
set str:= REPLACE(str, ' q', ' Q');
set str:= REPLACE(str, ' r', ' R');
set str:= REPLACE(str, ' s', ' S');
set str:= REPLACE(str, ' t', ' T');
set str:= REPLACE(str, ' u', ' U');
set str:= REPLACE(str, ' v', ' V');
set str:= REPLACE(str, ' w', ' W');
set str:= REPLACE(str, ' x', ' X');
set str:= REPLACE(str, ' y', ' Y');
set str:= REPLACE(str, ' z', ' Z');
return str;
END $$
DELIMITER ;