如何在T-SQL空格之前删除某些字符?

时间:2010-12-02 21:10:24

标签: sql tsql sql-server-2008 function

我正在努力想出一个传递方法

Bruno Miguel Alexandre 进入 B. Miguel Alexandre

Bruno Alexandre 进入 B.亚历山大

只是在SQL中,所以我可以在Store Procedure

中进行大查询

任何人都可以向我提供任何帮助吗?你们可能已经拥有的任何功能吗?

非常感谢。

2 个答案:

答案 0 :(得分:6)

从空间中获取第一个角色+所有内容。 8000是为了避免LEN通话

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

答案 1 :(得分: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)