In SQL Server how to convert string uppercase part lowercase based on tab: ?

Asked

Viewed 4,407 times

5

I need to make a script to convert all user records into a table in this format (single field):

INFORMATICA\desenvolvedor  
ADMINISTRACAO\contador

That is, before the uppercase bar and after the minuscule bar. How to do this in SQL Server?

Currently the records are like this (single field):

informatica\desenvolvedor ou  
INFORMATICA\DESENVOLVEDOR

2 answers

8


I don’t know if it’s the most elegant solution, but take your case:

select Upper(Substring(t.texto, 0, CHARINDEX ( '\' ,t.texto) ) ) 
       + '\' + 
       lower(Substring(t.texto, CHARINDEX ( '\' ,t.texto) + 1, LEN(t.texto)) )
from (
    select 'administrador\edgar' as texto
) as t
  • Thanks friend, based on your reply I created a function that just pass the string as parameter. I will post below.

0

Below is a function created from the Edgar response, to only pass the string as parameter.

CREATE FUNCTION FORMATAR_USUARIO(@STRING VARCHAR(1000)) RETURNS VARCHAR(1000)
BEGIN
    RETURN UPPER(SUBSTRING(@STRING, 0, CHARINDEX ( '\' ,@STRING) ) ) + '\' +    LOWER(SUBSTRING(@STRING, CHARINDEX ( '\' ,@STRING) + 1, LEN(@STRING)) )
END 

So, after creating the function, to use it is enough:

SELECT dbo.FORMATAR_USUARIO('uuuuu\oooooo')

Output:

UUUUU\oooooo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.