Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TRIM function on SQL Server 2014

IN SQLite I wrote :

UPDATE MYTABLE SET MYFIELD = TRIM(MYFIELD);

What to do to have this on SQL Server 2014 ?

like image 764
user3927897 Avatar asked Feb 22 '15 11:02

user3927897


People also ask

How do I TRIM in Microsoft SQL Server?

The syntax of trim is TRIM([ characters FROM] string) . If you just string without using characters FROM, then it will trim off the spaces on both sides of the string. If you use 'Characters' FROM, then it will look of the specific characters at the starting and end of the string and removes them.

What is the equivalent of TRIM in SQL Server?

Remarks. By default, the TRIM function removes the space character from both the start and the end of the string. This behavior is equivalent to LTRIM(RTRIM(@string)) .

When was TRIM added to SQL Server?

The TRIM function is used to remove trailing and leading spaces (char(32)) from a string of characters. This was introduced with SQL Server 2017.

How do I remove the first 3 characters in SQL?

To delete the first characters from the field we will use the following query: Syntax: SELECT SUBSTRING(string, 2, length(string));


2 Answers

UPDATE MYTABLE SET MYFIELD = LTRIM(RTRIM(MYFIELD));

However, field type must be varchar() and not text. Otherwise you get "Argument data type text is invalid for argument 1 of rtrim function"

like image 160
user763539 Avatar answered Oct 04 '22 01:10

user763539


You need functions LTRIM (to trim from left) and RTRIM (to trim from right):

UPDATE MYTABLE SET MYFIELD = LTRIM(RTRIM(MYFIELD));
like image 33
dotnetom Avatar answered Oct 04 '22 00:10

dotnetom