Unlike Java, Microsoft SQL Server 2008, 2012, 2014, in addition to fifty-fifty the latest version don't bring a built-in trim() function, which tin take both leading in addition to trailing infinite from the given String. But, SQL Server does bring 2 built-in functions LTRIM() in addition to RTRIM() to take leading in addition to trailing space. The LTRIM() function removes infinite from the left side of String thence you lot tin utilisation it to acquire rid of leading space, land RTRIM() removes white-space from the correct side of String thence you lot tin utilisation it to delete trailing space. You tin fifty-fifty combine these 2 methods to practise your ain TRIM() method inwards SQL SERVER e.g. LTRIM(RTRIM(column)) volition human activity every bit a TRIM() method because it removes both leading in addition to trailing space.
Here is a dyad of instance of LTRIM component division inwards Microsoft SQL Server database:
You tin run into that LTRIM() has removed leading space, while RTRIM() has removed trailing infinite inwards SQL Server. If you lot desire to take both leading in addition to trailing infinite inwards 1 short, you lot tin combine LTRIM, RTRIM similar shown below:
Here is the screenshot from my Microsoft SQL Server Management Studio to confirm that to a higher house SQL commands piece of job every bit expected:
Further Learning
The Complete SQL Bootcamp
Microsoft SQL for Beginners
courses)How to supplant NULL amongst empty String inwards SQL Server? (tutorial) How to detect the length of a String inwards SQL Server? (solution) How to separate String inwards SQL SERVER 2008? (answer) The divergence betwixt char, varchar, nchar in addition to nvarchar inwards SQL Server? (answer) How to bring together to a greater extent than than 2 tables inwards 1 SQL query? (solution) How to practise an Identity column inwards Microsoft SQL Server? (example) 5 tips land migrating from Oracle to SQL SERVER? (tips) How to detect the minute highest salary of an employee inwards SQL Server? (query) What is the divergence betwixt WHERE in addition to HAVING clause inwards SQL Server? (answer) How to detect duplicate records from a table? (solution) 5 Websites to larn SQL online for FREE? (resource) Top v Courses to larn Database in addition to SQL Online (courses)
How to utilisation LTRIM() in addition to RTRIM() inwards SQL Server
You tin utilisation LTRIM() in addition to RTRIM component division similar whatsoever other built-in function. You tin apply it to a value or a column inwards SQL query.Here is a dyad of instance of LTRIM component division inwards Microsoft SQL Server database:
// variable annunciation DECLARE @name VARCHAR(20) // assigning value to variable SELECT @name = ' Microsoft '; // other variable to shop result of trim operations DECLARE @withoutLeadingSpace VARCHAR(20) SELECT @withoutLeadingSpace = LTRIM(@name); DECLARE @withoutTrailingSpace VARCHAR(20) SELECT @withoutTrailingSpace = RTRIM(@name); // printing output SELECT @name as name, @withoutLeadingSpace as [LTRIM], @withoutTrailingSpace as [RTRIM] Output Microsoft Microsoft Microsoft
You tin run into that LTRIM() has removed leading space, while RTRIM() has removed trailing infinite inwards SQL Server. If you lot desire to take both leading in addition to trailing infinite inwards 1 short, you lot tin combine LTRIM, RTRIM similar shown below:
SELECT LTRIM(RTRIM(name))as Name from Employee
Here is the screenshot from my Microsoft SQL Server Management Studio to confirm that to a higher house SQL commands piece of job every bit expected:
Further Learning
The Complete SQL Bootcamp
Microsoft SQL for Beginners
courses)
Thanks for reading this SQL Server tutorial thence far. If you lot similar this article thence delight portion amongst your friends in addition to colleagues. If you lot bring whatsoever questions or feedback thence delight drib a note.