如何在T-SQL中删除空格前的特定字符?

3
我正试图想出一种方法来将“Bruno Miguel Alexandre”转换为“B. Miguel Alexandre”,并将“Bruno Alexandre”转换为“B. Alexandre”,这只需要在SQL中进行,以便我可以将其作为存储过程中的一部分。请问有人能提供任何帮助吗?你们可能已经有任何函数吗?非常感谢。
2个回答

6

取第一个字符和空格后的所有内容。8000是为了避免调用LEN函数。

LEFT(MyValue, 1) + '.' + SUBSTRING(MyValue, CHARINDEX(' ', MyValue), 8000)

3
尝试使用CharIndex查找空格的子字符串
with MyTable as
(
            SELECT 'Bruno Miguel Alexandre' as FullName
    UNION   SELECT 'Miguel Bruno Alexandre'
    UNION   SELECT 'Bruno Alexandre'
    UNION   SELECT 'Bruno Miguel'
)
SELECT 
    SubString (FullName, 1, 1) 
    + '.' 
    + SubString (FullName, CHARINDEX (' ', FullName, 1), 8000)
FROM MyTable

输出结果为:
------------------------
B. Alexandre
B. Miguel
B. Miguel Alexandre
M. Bruno Alexandre

(4 row(s) affected)

你知道的,看到 SUBSTRING 中的 LEN 让我感到不爽...;-) - gbn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接