Update all the upper case values to the lower case, using SQL query. You can use this for large updates in the table also.
Create database
Here, we have created a database named Test_DB and you can create it with a different name also.
After creating the database, there is a need to create a table in which we put some records.
Create a test table
I have created a table with TestTable name, having two columns named ID and EmailAddress, where ID is the primary key.
- USE [Test_DB]
- GO
-
- /****** Object: Table [dbo].[TestTable] Script Date: 7/4/2016 7:13:39 PM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[TestTable](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [EmailAddress] [varchar](50) NULL
- ) ON [PRIMARY]
-
- GO
-
- SET ANSI_PADDING OFF
- GO
The image, given below, is the design of our table:
Now we have to insert some records in this table, and run the below script :
Insert value in table - insert into TestTable(EmailAddress) values('upe**.**@***.com')
- insert into TestTable(EmailAddress) values('Upe**.**@***.com')
- insert into TestTable(EmailAddress) values('uPe**.**@***.com')
- insert into TestTable(EmailAddress) values('UPe**.**@***.com')
- insert into TestTable(EmailAddress) values('SPe**.**@***.com')
- insert into TestTable(EmailAddress) values('SPEe**.**@***.com')
- insert into TestTable(EmailAddress) values('DSF**.**@***.com')
- insert into TestTable(EmailAddress) values('FF**.**@***.com')
- insert into TestTable(EmailAddress) values('FGF**.**@***.com')
- insert into TestTable(EmailAddress) values('FF**.**@***.com')
- insert into TestTable(EmailAddress) values('UTYU**.**@***.com')
- insert into TestTable(EmailAddress) values('TYUYTU**.**@***.com')
- insert into TestTable(EmailAddress) values('RAHUL**.**@***.com')
- insert into TestTable(EmailAddress) values('RS**.**@***.com')
- insert into TestTable(EmailAddress) values('rtt**.**@***.com')
- insert into TestTable(EmailAddress) values('AWE**.**@***.com')
- insert into TestTable(EmailAddress) values('RED**.**@***.com')
Run the script, given above.
See below screen Now, we have to check the inserted record, using select query.
We found the retrieved record from the command, given below:
In the table, we see some records are in the upper case and some are in the lower case.
We need to update all the upper case values to the lower case. For this, we need first to know about the exact value for the upper case , which is to be update in the lower case.
How do we know if the records are in the upper case or in the lower case?
We can find these records with many queries but I am using some here:
- Select * from test_up where ASCII(left(EmailAddress, 1)) between ASCII('A')
and ASCII('Z')
- Select * from Testtable where EmailAddress!=upper(emailaddress)COLLATE Latin1_General_CS_AS
- Using function in SQL
Create a function,
- create function dbo.fnIsStringInAllUppercase(@input nvarchar(max)) returns bit
-
- as
-
- begin
-
- if (ISNUMERIC(@input) = 0 AND RTRIM(LTRIM(@input)) > '' AND @input = UPPER(@input COLLATE Latin1_General_CS_AS))
- return 1;
-
- return 0;
- end
Now check value using Function :
in the select query,
- SELECT *
- FROM Testtable
- WHERE dbo.fnIsStringAllUppercase(EmailAddress) = 1
From the code, given above, you can see your data, which is required to be updated.
Use the query, given below:
- BEGIN TRANSACTION
- GO
-
- UPDATE [dbo].[test_up]
- SET EmailAddress = lower(EmailAddress)
- WHERE id in(select id from test_up where EmailAddress!=lower(emailaddress)COLLATE Latin1_General_CS_AS )
- GO
COMMIT TRANSACTION,
Now, we can see the table records:
All the upper case values will convert in the lower case values.