MySQL - 每个单词的首字母

时间:2016-08-31 11:12:35

标签: mysql

下面是MySQL函数,用于大写字符串中每个单词的第一个字母。

here is my code:
<section id="partner" class="use m-t-lg" style="margin-top:5px">
            <div class="container-fluid">
                <div class="Partner_portion">


                    <!-- Service Category Row-->

                        <!--First category-->

                    <div layout="row" layout-xs="column" layout-sm="column" layout-fill="true" class="service_apps layout-fill layout-xs-column layout-sm-column layout-row">

                        <!--Service Box 1-->
                        <!--Service Box 2-->
                        <div class="single_app col-md-4 flex-33 partner_content">
                            <div class="use_block">
                                <div class="use_blockcontent">
                                    <div class="use-block-text" id="#test">

                                        <h2>Rental Package</h2><br />
                                        <p class="textsmall">
                                            Cost effective way of starting ‘Vehicle Tracking’business with minimum upfront cost and time.
                                        </p>
                                        <br />
                                        <p style="color:#43C6DB;cursor:pointer">Read More</p>
                                    </div>
                                </div>

                            </div>
                        </div>

                        <!--Service Box 3-->
                        <div class="single_app col-md-4 flex-33 partner_imgsection" style="background-image:url('images/box.png')">
                            <div class="use_block">
                            </div>
                        </div>

                        <!--Service Box 3-->

                        <div class="single_app col-md-4 flex-33 partner_content" style="-moz-animation:ease-in-out;-moz-animation-delay:2s">
                            <div class="use_block">

                                <div class="use_blockcontent">
                                    <div class="use-block-text">

                                        <h2> Custom Package</h2><br />
                                        <p class="textsmall">
                                            Customized solution specific to a segment related transportation industry
                                        </p>
                                        <br />
                                        <p style="color:#43C6DB;cursor:pointer">Read More</p>
                                    </div>
                                </div>

                            </div>
                        </div>



                    </div>






                    <div layout="column" layout-align="center center" class="app_row layout-align-center-center layout-column">
                        <!--First category-->

                        <div layout="row" layout-xs="column" layout-sm="column" layout-fill="true" class="service_apps layout-fill layout-xs-column layout-sm-column layout-row">

                            <!--Service Box 1-->
                            <!--Service Box 2-->
                            <div class="single_app col-md-4 flex-33 partner_imgsection" style="background-image:url('images/pie-chart.png')">
                                <div class="use_block">
                                </div>
                            </div>

                            <!--Service Box 3-->
                            <div class="single_app col-md-4 flex-33 partner_content">
                                <div class="use_block">
                                    <div class="use_blockcontent">
                                        <div class="use-block-text">

                                            <p class="textmiddle">
                                                Take Your Business A Step Further<br />Host Your Own Tracking System
                                                <br>
                                            <p style="color:#43C6DB;cursor:pointer">Read More</p>
                                        </div>

                                    </div>

                                </div>
                            </div>

                            <!--Service Box 3-->


                            <div class="single_app col-md-4 flex-33 partner_imgsection" style="background-image:url('images/coins.png')">
                                <div class="use_block">







                                </div>
                            </div>



                        </div>


                    </div>





                    <div layout="column" layout-align="center center" class="app_row layout-align-center-center layout-column">
                        <!--First category-->

                        <div layout="row" layout-xs="column" layout-sm="column" layout-fill="true" class="service_apps layout-fill layout-xs-column layout-sm-column layout-row">

                            <!--Service Box 1-->
                            <!--Service Box 2-->
                            <div class="single_app col-md-4 flex-33 partner_content">
                                <div class="use_block">
                                    <div class="use_blockcontent">
                                        <div class="use-block-text">

                                            <h2> Marketing and Sales material</h2><br />
                                            <p class="textsmall">
                                                All the material that you need to make a sale is our responsibility.
                                            </p>
                                            <br />
                                            <p style="color:#43C6DB;cursor:pointer">Read More</p>
                                        </div>
                                    </div>

                                </div>
                            </div>

                            <!--Service Box 3-->
                            <div class="single_app col-md-4 flex-33 partner_imgsection" style="background-image:url('images/people.png');">
                                <div class="use_block">







                                </div>
                            </div>


                            <!--Service Box 3-->


                            <div class="single_app col-md-4 flex-33 partner_content">
                                <div class="use_block">

                                    <div class="use_blockcontent">
                                        <div class="use-block-text">

                                            <h2>Training and Support</h2><br />
                                            <p class="textsmall">
                                                We guide you with every step taken in a new direction and ensure that you benefit from our experience.
                                            </p>
                                            <br />
                                            <p style="color:#43C6DB;cursor:pointer">Read More</p>
                                        </div>
                                    </div>

                                </div>
                            </div>



                        </div>


                    </div>



                </div><!--partner portion closed-->
            </div><!--container closed-->
        </section><!--partner section closed-->

它适用于所有字符串。但是,如果我提供字符串&#34; M R E&#34;然后它给予&#34; M R e&#34;输出

plz建议。

1 个答案:

答案 0 :(得分:2)

First execute below query

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` FUNCTION `UC_FIRST`(oldWord VARCHAR(255)) RETURNS varchar(255) CHARSET latin1
RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),SUBSTRING(oldWord, 2))

Then execute below query

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` FUNCTION `UC_Words`(oldName VARCHAR(255), delim VARCHAR(1), trimSpaces BOOL) RETURNS varchar(255) CHARSET latin1
BEGIN
    SET @oldString := oldName;
    SET @newString := "";

    tokenLoop: LOOP
    IF trimSpaces THEN SET @oldString := TRIM(BOTH " " FROM @oldString); END IF;

    SET @splitPoint := LOCATE(delim, @oldString);

    IF @splitPoint = 0 THEN
      SET @newString := CONCAT(@newString, UC_FIRST(@oldString));
      LEAVE tokenLoop;
    END IF;

    SET @newString := CONCAT(@newString, UC_FIRST(SUBSTRING(@oldString, 1, @splitPoint)));
    SET @oldString := SUBSTRING(@oldString, @splitPoint+1);
    END LOOP tokenLoop;

    RETURN @newString;
END

Then after call function

SELECT UC_Words("this is for testing"," ", TRUE);

Output below

This Is For Testing