Update Column In Database For A Table Having Upper Case Value

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.

  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object:  Database [Test_DB]    Script Date: 7/4/2016 7:14:32 AM ******/  
  5. CREATE DATABASE [Test_DB]  
  6.  CONTAINMENT = NONE  
  7.  ON  PRIMARY   
  8. NAME = N'Test_DB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Test_DB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9.  LOG ON   
  10. NAME = N'Test_DB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Test_DB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [Test_DB] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [Test_DB].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [Test_DB] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [Test_DB] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [Test_DB] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [Test_DB] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [Test_DB] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [Test_DB] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [Test_DB] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [Test_DB] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [Test_DB] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [Test_DB] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [Test_DB] SET CURSOR_DEFAULT  GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [Test_DB] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [Test_DB] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [Test_DB] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [Test_DB] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [Test_DB] SET  DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [Test_DB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [Test_DB] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [Test_DB] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [Test_DB] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [Test_DB] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [Test_DB] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [Test_DB] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [Test_DB] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [Test_DB] SET  MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [Test_DB] SET PAGE_VERIFY CHECKSUM    
  98. GO  
  99.   
  100. ALTER DATABASE [Test_DB] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [Test_DB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [Test_DB] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [Test_DB] SET  READ_WRITE   
  110. GO  
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.
  1. USE [Test_DB]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[TestTable]    Script Date: 7/4/2016 7:13:39 PM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[TestTable](  
  15.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  16.     [EmailAddress] [varchar](50) NULL  
  17. ON [PRIMARY]  
  18.   
  19. GO  
  20.   
  21. SET ANSI_PADDING OFF  
  22. GO  
The image, given below, is the design of our table:

design

Now we have to insert some records in this table, and run the below script :

Insert value in table
  1. insert into TestTable(EmailAddress) values('upe**.**@***.com')  
  2. insert into TestTable(EmailAddress) values('Upe**.**@***.com')  
  3. insert into TestTable(EmailAddress) values('uPe**.**@***.com')  
  4. insert into TestTable(EmailAddress) values('UPe**.**@***.com')  
  5. insert into TestTable(EmailAddress) values('SPe**.**@***.com')  
  6. insert into TestTable(EmailAddress) values('SPEe**.**@***.com')  
  7. insert into TestTable(EmailAddress) values('DSF**.**@***.com')  
  8. insert into TestTable(EmailAddress) values('FF**.**@***.com')  
  9. insert into TestTable(EmailAddress) values('FGF**.**@***.com')  
  10. insert into TestTable(EmailAddress) values('FF**.**@***.com')  
  11. insert into TestTable(EmailAddress) values('UTYU**.**@***.com')  
  12. insert into TestTable(EmailAddress) values('TYUYTU**.**@***.com')  
  13. insert into TestTable(EmailAddress) values('RAHUL**.**@***.com')  
  14. insert into TestTable(EmailAddress) values('RS**.**@***.com')  
  15. insert into TestTable(EmailAddress) values('rtt**.**@***.com')  
  16. insert into TestTable(EmailAddress) values('AWE**.**@***.com')  
  17. insert into TestTable(EmailAddress) values('RED**.**@***.com')  
Run the script, given above.

See below screen

output

Now, we have to check the inserted record, using select query.

We found the retrieved record from the command, given below:
  1. select * from Testtable  
output

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:  
  1. Select * from test_up where ASCII(left(EmailAddress, 1)) between ASCII('A')
    and ASCII('Z')

    output

  2. Select * from Testtable where EmailAddress!=upper(emailaddress)COLLATE Latin1_General_CS_AS

    output

  3. Using function in SQL

    Create a function,
    1. create function dbo.fnIsStringInAllUppercase(@input nvarchar(max)) returns bit  
    2.   
    3.     as  
    4.   
    5. begin  
    6.   
    7.     if (ISNUMERIC(@input) = 0 AND RTRIM(LTRIM(@input)) > '' AND @input = UPPER(@input COLLATE Latin1_General_CS_AS))  
    8.         return 1;  
    9.   
    10.     return 0;  
    11. end  
    Now check value using Function :

    in the select query,
    1. SELECT *  
    2. FROM   Testtable  
    3. 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:
    1. BEGIN TRANSACTION  
    2. GO  
    3.   
    4. UPDATE [dbo].[test_up]  
    5. SET    EmailAddress  = lower(EmailAddress)   
    6. WHERE id in(select id from test_up where EmailAddress!=lower(emailaddress)COLLATE Latin1_General_CS_AS )  
    7. GO  
    COMMIT TRANSACTION,

    TRANSACTION
    Now, we can see the table records:

    records

    All the upper case values will convert in the lower case values.


Similar Articles