【问题标题】:Calculate word length with SQL用 SQL 计算字长
【发布时间】:2017-04-19 12:45:35
【问题描述】:

是否可以仅使用 SQL 计算每个单词的长度?比如下面这句话:

I have a length of x.

1  4   1  6     2  1

【问题讨论】:

标签: sql


【解决方案1】:

可以尝试自定义方法创建:

以下是可以提供帮助的示例代码:
Declare @products varchar(200) = 'I have a length of x.'
Declare @individual varchar(20) = null
Declare @lengthOfProducts varchar(20) = null

WHILE LEN(@products) > 0
BEGIN
    IF PATINDEX('% %', @products) > 0
    BEGIN
        SET @individual = SUBSTRING(@products,
                                    0,
                                    PATINDEX('% %', @products))
        SELECT @individual
        SET @lengthOfProducts = @lengthOfProducts + ' ' + LEN(@individual)

        SET @products = SUBSTRING(@products,
                                  LEN(@individual + ' ') + 1,
                                  LEN(@products))
    END
    ELSE
    BEGIN
        SET @individual = @products
        SET @products = NULL
        SELECT @lengthOfProducts
    END
END

【讨论】:

    【解决方案2】:

    LEN() 函数。您只需在 SQL 中使用它

    SELECT LEN(column_name) FROM table_name;
    

    这在 Oracle 中

    SELECT LENGTH(column_name) FROM table_name;
    

    致谢:SQL LEN() Function

    【讨论】:

    • 他们想要String中每个单词的长度。
    【解决方案3】:

    在 sql server 中,使用LEN(column_name)。 甲骨文使用LENGTH(column_name) 例如SELECT CustomerName,LEN(Address) as LengthOfAddress FROM Customers;

    【讨论】:

    • 他们想要String中每个单词的长度。
    • link。试试这个链接。
    猜你喜欢
    • 2014-02-21
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    相关资源
    最近更新 更多