63

How do you make a field in a sql select statement all upper or lower case?

Example:

select firstname from Person

How do I make firstname always return upper case and likewise always return lower case?

2
  • What happens when you move to a database (or other repository - XML?) that does not support case changing? Consider doing this in code or performing case-insensitive operations. Dec 4, 2008 at 17:15
  • Thanks for the comment ajmastrean. I would fully agree with your statement and I rarely do any casing in SQL, which is why I forget how to do this from time to time when I do have a need it. Dec 4, 2008 at 17:26

5 Answers 5

101
SELECT UPPER(firstname) FROM Person

SELECT LOWER(firstname) FROM Person
1
  • 8
    Is this part of the SQL standard?
    – micapam
    Jun 4, 2014 at 5:57
16

LCASE or UCASE respectively.

Example:

SELECT UCASE(MyColumn) AS Upper, LCASE(MyColumn) AS Lower
FROM MyTable
5
  • 1
    UCASE for lower and LCase for Upper? Dec 4, 2008 at 17:10
  • @Joshua - LCASE & UCASE are inherent within the SQL language itself not related to any specific implementation of SQL within a RDBMS. And I fixed the UCASE/LCASE for Lower/Upper thing. Dec 4, 2008 at 17:12
  • Thank you. I gave you a up vote because you are right about ISO SQL vs an implementation. Dec 4, 2008 at 17:22
  • 3
    LCASE/UCASE seem not to be known in Oracle (11.2.0.4.0), while LOWER/UPPER work Jun 6, 2014 at 8:23
  • LCASE does not exist in Postgres! Not sure if oracle and postgres are just out of spec, but this cannot be depended on for cross-database functionality. like Andre said, use LOWER/UPPER. They're supported on MySQL, MsSql and Postgres
    – Alkanshel
    Sep 30, 2015 at 23:48
5

SQL SERVER 2005:

print upper('hello');
print lower('HELLO');
1
  • Perfect. Thank you Cirieno! I actually had this answer but wanted it to be documented on stack overflow since I only use this rarely I always forget it. :) Dec 4, 2008 at 17:07
2

You can use LOWER function and UPPER function. Like

SELECT LOWER('THIS IS TEST STRING')

Result:

this is test string

And

SELECT UPPER('this is test string')

result:

THIS IS TEST STRING
0

You can do:

SELECT lower(FIRST NAME) ABC
FROM PERSON

NOTE: ABC is used if you want to change the name of the column

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.