开发者

How can I remove certain chars before the space in T-SQL?

I'm trying witho开发者_StackOverflow社区ut any luck to come up with a method to pass

Bruno Miguel Alexandre into B. Miguel Alexandre

and

Bruno Alexandre into B. Alexandre

just in SQL so I can make this part of a big query in a Store Procedure

can anyone provide me with any help? Any function that you guys might already have?

Much appreciated.


Take 1st character + everything from the space. The 8000 is to avoid LEN calls otherwise

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


Try a substring with a CharIndex to find the space

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

The output is

------------------------
B. Alexandre
B. Miguel
B. Miguel Alexandre
M. Bruno Alexandre

(4 row(s) affected)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜