How to use LOWER in Sql Server

05.11.2022

Intro

SQL Server provides the lower function to transform a string into all lower case letters. In this article, we will learn how to use LOWER in SQL Server.

The Syntax

The basic syntax of a LOWER is as follows:

SELECT LOWER(string);
  • string: The string or varchar you want to lower case.

Getting Setup

For this, we will be using docker. This is recommended for more than just using SQL Server. To find how to install docker go here: https://docs.docker.com/engine/install/

Now create a file called docker-compose.yml and add the following.

version: "3.9"
services:
  db:
    image: "mcr.microsoft.com/mssql/server"
    ports: 
      - 1433:1433
    environment:
        SA_PASSWORD: "Your_password123"
        ACCEPT_EULA: "Y"

Open a terminal and go to the folder the file is located. Then run the following.

docker-compose up

If you are looking for another good reference, you can check here: https://docs.docker.com/samples/aspnet-mssql-compose/.

Basic Example

The simple example is to lower case a string literal. Here is an example.

select lower('Hello World!') as lower_string;
lower_string
hello world!

Lower on a Table Column

We can also run lower on a column. Below we create an employee table.

CREATE TABLE employees (
    first_name VARCHAR (50) NOT NULL,
    last_name VARCHAR (50) NOT NULL
);

insert into employees (first_name, last_name) 
	values 
	('Keith', 'Holliday'),
	('Jon', 'Doe'),
	('Jane', 'Doe');

We can now run the lower function on the columns.

select 
  lower(first_name) as FirstName,
	lower(last_name) as LastName
from employees e;
FirstName LastName
keith holliday
jon doe
jane doe