Home > Software engineering >  First 3 letters in caps and remaining letters in small case in a name -SQL
First 3 letters in caps and remaining letters in small case in a name -SQL

Time:08-24

i have question, In a name (eg. Richard) first 3 letters should be in capital letter and remaining letters should be in lower case.

ANS: RIChard

can you help me to get the query for this?

CodePudding user response:

Microsoft SQL Server has UPPER() and LOWER() functions that could change the characters to upper case and lower case. for your demand, you need to use UPPER for your first 3 letters: you can use the left() or substring functions to get the first 3 letters. and for the remaining letters, you need to use the LOWER function. for splitting the remaining letters you need to use the right or substring functions plus Len() function to calculate the remained letter counts.

Select  UPPER(Left(Name,3))   LOWER(right(Name,len(Name)-3))

OR

Select  UPPER(substring(Name,1,3))   LOWER(substring(Name,4,len(Name)))

CodePudding user response:

You can do like this

SELECT UPPER(LEFT('richard',3)) LOWER(SUBSTRING('richard',4,LEN(richard')));

CodePudding user response:

What you could do is the following if you want to update it:

UPDATE employees SET First_name = CONCAT(UPPER(LEFT(First_name,3)), LOWER(SUBSTRING(First_name,4)))

You can test it here

If you want to only have it in a select you can use:

SELECT CONCAT(UPPER(LEFT(First_name,3)), LOWER(SUBSTRING(First_name,4))) as First_name FROM employees;

You can test it here.

What I'm doing in both cases is the following:

  1. Get the first 3 characters, and convert it to uppercase
  2. Get all the other characters, and convert it to lowercase
  3. Concatenate the two string together

CodePudding user response:

// Try this:

SELECT UPPER(LEFT('richard',3)) LOWER(SUBSTRING('richard',4));
  •  Tags:  
  • sql
  • Related