Trim (MySQL)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Dit dus:

select trim(" blub ");                        # Leading & trailing spaties verwijderd
select trim(leading  "--" from "--blub--");   # Gegeven string wordt aan het begin verwijderd
select trim(trailing "--" from "--blub--");   # Gegeven string wordt aan het eind verwijderd
select trim(         "--" from "--blub--");   # Gegeven string wordt aan begin & eind verwijderd
select trim(both     "--" from "--blub--");   # Gegeven string wordt aan begin & eind verwijderd - 'both' is overbodig

Het keyword from kun je niet weglaten.

Alle velden van een tabel trimmen

Tjakka:

CREATE DEFINER=`root`@`localhost` PROCEDURE `trim_table`(
	in table_name_in tinytext
)
BEGIN
#
# Trip all columns of the given table
###############################################################################################################
#
###############################################################################################################
# Create table "fields_tmp" with all field names from the given table
###############################################################################################################
#
drop table if exists fields_tmp;

create temporary table fields_tmp
select column_name 
from information_schema.columns
where
	table_name=table_name_in
	and
	table_schema=database();


###############################################################################################################
# Turn column names into trim statements
###############################################################################################################
#
update fields_tmp
set
   column_name = concat
   (
      column_name,"=trim(' ' from ",column_name,")"
   );
 

###############################################################################################################
# Turn these trim statements into 1 ddl-string
###############################################################################################################
#
set @ddl_01=
(
   select concat
   (
      "update tmp set ",
      (select group_concat(column_name) from fields_tmp)
   )
   as ddl
);


###############################################################################################################
# Create additional ddl-strings
###############################################################################################################
#
set @ddl_02=replace(@ddl_01, "' '", "','");
set @ddl_03=replace(@ddl_01, "' '", "'.'");
set @ddl_04=replace(@ddl_01, "' '", "'-'");


###############################################################################################################
# Execute
###############################################################################################################
#
prepare stmt_01 from @ddl_01;
prepare stmt_02 from @ddl_02;
prepare stmt_03 from @ddl_03;
prepare stmt_04 from @ddl_04;

execute stmt_01;
execute stmt_02;
execute stmt_03;
execute stmt_04;

deallocate prepare stmt_01;
deallocate prepare stmt_02;
deallocate prepare stmt_03;
deallocate prepare stmt_04;

END

Zie ook