代码之家  ›  专栏  ›  技术社区  ›  William

使每个单词的首字母大写为Sqlite

  •  0
  • William  · 技术社区  · 8 年前

    我希望能够更新字符串大写中的每个单词。

    我可能拥有:

    the dog is brown
    

    我想要的是:

    The Dog Is Brown
    

    现在我在发布前搜索了这里,发现了这个问题: SQLite Updating first letter to be upper case

    使用提供的接受答案查询: UPPER(SUBSTR(field, 1, 1)) || SUBSTR(field, 2) 这会将第一个单词更新为大写,不是全部吗?

    是否有什么可以添加到这个中以使其适用于所有单词?

    谢谢

    3 回复  |  直到 8 年前
        1
  •  2
  •   Community Mohan Dere    5 年前

    您可以使用以下内容作为基础:-

    测试表创建

    DROP TABLE IF EXISTS sentences;
    CREATE TABLE IF NOT EXISTS sentences (sentence TEXT);
    

    加载一些测试数据

    INSERT INTO sentences
    VALUES 
        ('the dog is brown'),('pigs are pink'),('polar bears are white'),('orangutans are orange'),('zebras are black and white');
    

    表如下所示:-

    enter image description here

    查询

    SELECT 
        substr(
            replace(
                replace(
                    replace(
                        replace(
                            replace(
                                replace(
                                    replace(
                                        replace(
                                            replace(' '||sentence,' a',' A')
                                        ,' b',' B'),
                                    ' c',' C'),
                                ' d',' D'),
                            ' e',' E'),
                        ' f',' F'),
                    ' g',' G'),
                ' h',' H'),
                -- ..........
            ' t',' T')
            -- .............
        ,2) AS converted
    FROM sentences
    
    • 这是以a-h和t开头的单词
    • 它添加了一个空格,允许根据 ' '||sentence 然后在所有替换完成后删除空间 substr(........,2)

    后果

    enter image description here

        2
  •  1
  •   Moon Brendan    6 年前
    SELECT 
        substr(
            replace(
                replace(
                    replace(
                        replace(
                            replace(
                                replace(
                                    replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                        replace(
                                            replace(' '||text,' a',' A')
                                        ,' b',' B'),
                                    ' c',' C'),
                                ' d',' D'),
                            ' e',' E'),
                        ' f',' F'),
                    ' g',' G'),
                ' h',' H'),
                ' i',' I'),
                ' j',' J'),
                ' k',' K'),
                ' l',' L'),
                ' m',' M'),
                ' n',' N'),
                ' o',' O'),
                ' p',' P'),
                ' q',' Q'),
                ' r',' R'),
                ' s',' S'),
                ' t',' T'),
                ' u',' U'),
                ' v',' V'),
                ' w',' W'),
                ' x',' X'),
                ' y',' Y'),
                ' z',' Z'),2) AS converted
    FROM city_db.
    
        3
  •  1
  •   k_karseladze    5 年前
    str.split(' ').map((it, i) => {
      return (it[0].toUpperCase() + it.slice(1))
    }
    ).join(' ')
    
    推荐文章