Jonathan Escobedo
Jonathan Escobedo

Reputation: 4063

How can I use user defined functions?

I have a table Users, so some rows specially in field Full Name are in different upper/lower case, so i found this function:

CREATE function properCase(@texto varchar(8000)) returns varchar(8000) as   
begin   
    --declare @texto = 'hola'  
    set @texto = lower(@texto)   

    declare @i int   
    set @i = ascii('a')   

    while @i <= ascii('z')   
    begin   

        set @texto = replace(@texto, ' ' + char(@i), ' ' + char(@i-32))   
        set @i = @i + 1   
    end   

    set @texto = char(ascii(left(@texto, 1))-32) + right(@texto, len(@texto)-1)   

    return @texto   
end  

How can I use this function to update or select the "fullname" field from my user table?

Upvotes: 0

Views: 148

Answers (2)

Misko
Misko

Reputation: 2044

SELECT dbo.properCase(fullname) FROM [user]

and

UPDATE [user] SET fullname = dbo.properCase(fullname)

Upvotes: 2

Sean Bright
Sean Bright

Reputation: 120704

SELECT dbo.properCase(FullName) FROM [User]

and:

UPDATE [User] SET FullName = dbo.properCase(FullName)

Upvotes: 2

Related Questions