Avatar of jspc
jspcFlag for Australia

asked on 

SQL Script (Uppercase & Propercase)

Hello,

Does anyone know of a script I can run in SQL Studio Manager which will either:

•      Upper case
•      Proper case

my Master Records, rather than sending this data to Excel to perform the function and importing back?

Thanks.
Microsoft SQL Server

Avatar of undefined
Last Comment
PortletPaul
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of jspc
jspc
Flag of Australia image

ASKER

Is there one for Proper?
Avatar of PortletPaul
PortletPaul
Flag of Australia image

i will get back on that

only had a moment to comment via my phone
Avatar of PortletPaul
PortletPaul
Flag of Australia image

There isn't an built-in way to achieve "proper case" (or "Camel Case" or "CamelCaps")

The best way to achieve it is to use a user defined function and there are many references to these available.

Here is one from Scott Pletcher (see this previous question)
CREATE FUNCTION dbo.Proper_Case (
    @string varchar(2000)
    )
RETURNS varchar(2000)
AS
--Sample use: SELECT dbo.Proper_Case('aBC mike;a alkj/as 12k 43')
BEGIN

DECLARE @previousByte int
DECLARE @byte int

SET @string = LOWER(@string)
IF LEFT(@string, 1) LIKE '[a-z]'
    SET @string = STUFF(@string, 1, 1, UPPER(LEFT(@string, 1)))
SET @previousByte = 2

WHILE 1 = 1
BEGIN
    SET @byte = PATINDEX('%[^a-z0-9][a-z]%', SUBSTRING(@string, @previousByte, 2000))
    --PRINT 'Byte = ' + CAST(@byte AS varchar(10))
    IF @byte = 0
        BREAK
    SET @string = STUFF(@string, @previousByte + @byte, 1, 
        UPPER(SUBSTRING(@string, @previousByte + @byte, 1)))
    SET @previousByte = @previousByte + @byte + 1
    --PRINT 'PrevByte = ' + CAST(@previousByte AS varchar(10))
END --WHIILE

RETURN @string

END --FUNCTION

GO

Open in new window

For the following data
CREATE TABLE Table1
    ([Column1] varchar(60))
;
    
INSERT INTO Table1
    ([Column1])
VALUES
    ('patrick o''malley'),
    ('wolfgang von ribben tropp'),
    ('buckley & nunn'),
    ('archibald smyth-percival')
;

Open in new window

This query produced the following result:
SELECT
   dbo.Proper_Case(Column1)
FROM Table1
;


Patrick O'Malley
Wolfgang Von Ribben Tropp
Buckley & Nunn
Archibald Smyth-Percival

Open in new window

Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo