Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: capitalize first letter only [duplicate]

Tags:

sql

sql-server

I need an SQL statement to capitalize the first letter of each word. The other characters have to be lower case.

The words can be like this:

wezembeek-oppem
roeselare
BRUGGE
louvain-la-neuve

Which would have to be:

Wezembeek-Oppem
Roeselare
Brugge
Louvain-La-Neuve

This should be with an UPDATE statement, I want to update the data of a column. Thank you very much for your answers in advance, I'm an SQL novice.

like image 501
samn Avatar asked Oct 02 '22 14:10

samn


People also ask

How do you capitalize the first letter in a SQL query?

Use the INITCAP() function to convert a string to a new string that capitalizes the first letter of every word. All other letters will be lowercase. This function takes one parameter as a string and changes the capitalization for each word as described.

How do you capitalize a letter in SQL?

If you want to display a string in uppercase, use the SQL UPPER() function. This function takes only one argument: the string column that you want to convert to uppercase.

How do you perform a case insensitive comparison in SQL?

To do a case-insensitive comparison, use the ILIKE keyword; e.g., column ILIKE 'aBc' and column ILIKE 'ABC' both return TRUE for 'abc' . In contrast, MySQL and MS SQL Server have case-insensitive behaviors by default. This means WHERE column = 'abc' returns TRUE for e.g., 'abc' , 'ABC' , or 'aBc' .


1 Answers

Are you asking for renaming column itself or capitalise the data inside column? If its data you've to change, then use this:

UPDATE [yourtable]
SET word=UPPER(LEFT(word,1))+LOWER(SUBSTRING(word,2,LEN(word)))

If you just wanted to change it only for displaying and do not need the actual data in table to change:

SELECT UPPER(LEFT(word,1))+LOWER(SUBSTRING(word,2,LEN(word))) FROM [yourtable]

Hope this helps.

EDIT: I realised about the '-' so here is my attempt to solve this problem in a function.

CREATE FUNCTION [dbo].[CapitalizeFirstLetter]
(
--string need to format
@string VARCHAR(200)--increase the variable size depending on your needs.
)
RETURNS VARCHAR(200)
AS

BEGIN
--Declare Variables
DECLARE @Index INT,
@ResultString VARCHAR(200)--result string size should equal to the @string variable size
--Initialize the variables
SET @Index = 1
SET @ResultString = ''
--Run the Loop until END of the string

WHILE (@Index <LEN(@string)+1)
BEGIN
IF (@Index = 1)--first letter of the string
BEGIN
--make the first letter capital
SET @ResultString =
@ResultString + UPPER(SUBSTRING(@string, @Index, 1))
SET @Index = @Index+ 1--increase the index
END

-- IF the previous character is space or '-' or next character is '-'

ELSE IF ((SUBSTRING(@string, @Index-1, 1) =' 'or SUBSTRING(@string, @Index-1, 1) ='-' or SUBSTRING(@string, @Index+1, 1) ='-') and @Index+1 <> LEN(@string))
BEGIN
--make the letter capital
SET
@ResultString = @ResultString + UPPER(SUBSTRING(@string,@Index, 1))
SET
@Index = @Index +1--increase the index
END
ELSE-- all others
BEGIN
-- make the letter simple
SET
@ResultString = @ResultString + LOWER(SUBSTRING(@string,@Index, 1))
SET
@Index = @Index +1--incerase the index
END
END--END of the loop

IF (@@ERROR
<> 0)-- any error occur return the sEND string
BEGIN
SET
@ResultString = @string
END
-- IF no error found return the new string
RETURN @ResultString
END

So then the code would be:

UPDATE [yourtable]
SET word=dbo.CapitalizeFirstLetter([STRING TO GO HERE])
like image 252
Scott Sellers Avatar answered Oct 12 '22 05:10

Scott Sellers