Use of the trim () function to delete the left and right characters in SQL statements

Source: Internet
Author: User
The article tells you how to delete the trim function, which is in SQL. It doesn't feel convenient in php. If you need it, please refer to the operation process.

The article tells you how to delete the trim function, which is in SQL. It doesn't feel convenient in php. If you need it, please refer to the operation process.

The Trim function in the program is known to all, but in SQL, only LTRIM is required. RTRIM deletes the Left and Right blank characters instead of the specified characters, so we can write one by ourselves.

Requirements:

1. The front and back spaces can be deleted, for example, 'a'-> 'A'

2. The front and back characters can be deleted and are not affected by blank spaces, such as '; a'-> 'A'

3. After the front and back characters are deleted, the front and back spaces must be cleared, such as '; a'-> 'A'

4. You need to delete consecutive characters, such as '; a'-> 'A'

I think it is quite good that some others write on the Internet, but it does not seem to fully meet the requirements, so I wrote one by myself.

The code for creating a function is as follows:

The Code is as follows:

/******
Object: UserDefinedFunction [dbo]. [TRIM]
Script Date: 09:10:14
Author: EF
******/
If exists (SELECT * FROM sys. objects WHERE object_id = OBJECT_ID (n' [dbo]. [trim] ') AND type in (N 'fn', N 'if', N 'tf', N 'fs', N 'ft '))
Drop function [dbo]. [trim]
GO
Create function dbo. trim
(
@ Source VARCHAR (MAX ),
@ Char CHAR (1)
)
Returns varchar (MAX)
AS
BEGIN
DECLARE @ I int;
DECLARE @ returnString VARCHAR (MAX)
SET @ returnString = @ Source
-- Clear leading and trailing Spaces
SET @ returnString = LTRIM (RTRIM (@ returnString ))
-- Delete characters on the left
SET @ I = 0
WHILE @ I = 0
BEGIN
If left (@ returnString, 1) = @ Char
SET @ returnString = RIGHT (@ returnString, LEN (@ returnString)-1)
ELSE
SET @ I = 1
END
-- Delete characters on the right
SET @ I = 0
WHILE @ I = 0
BEGIN
If right (@ returnString, 1) = @ Char
SET @ returnString = LEFT (@ returnString, LEN (@ returnString)-1)
ELSE
SET @ I = 1
END
-- Clear leading and trailing Spaces
SET @ returnString = LTRIM (RTRIM (@ returnString ))
RETURN @ returnString;
END

GO

-- Test

Dbo. trim ('asdfas; asdfasdfa ;',';')
Union all select dbo. trim ('; asdfas; asdfasdfa ;',';')
Union all select dbo. trim ('; asdfas; asdfasdfa ;',';')
Union all select dbo. trim ('; asdfas; asdfasdfa ;',';')

-- Result

-------------------------------------------------------------------------

Asdfas; asdfasdfa
Asdfas; asdfasdfa
Asdfas; asdfasdfa
Asdfas; asdfasdfa

(Four rows affected)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.