How to remove some characters before space in T-SQL?

I'm trying, without luck, to come up with a way to transfer

Bruno Miguel Alexander in B. Miguel Alexander

and

Bruno Alexandre in B. Alexander

only in SQL, so I can do this part of a large query in a storage procedure

can anyone help me? Any function you may already have?

Great importance.

+3
source share
2 answers

Take the 1st character + all of space. 8000 - avoid LEN calls otherwise

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

Try a substring using CharIndex to find a 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

Output signal

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

(4 row(s) affected)
+3

Source: https://habr.com/ru/post/1778194/


All Articles