How to Trim in MS SQL


Most programming languages have a trim function, which removes any blank spaces before and after a variable's text. Microsoft (MS) SQL does not have a trim function, but it has rtrim and ltrim functions. These functions remove spaces from the right and left side of the variable text. You use these functions in place of other languages' trim function.

  • Click the Windows "Start" button and select "All Programs." Click "SQL Server," then click "SQL Server Management Studio." This opens the MS SQL application for your database server.

  • Click the "Programmability" icon, then click "Stored Procedures." Right-click the stored procedure you want to edit and select "Modify."

  • Add the rtrim and ltrim functions to the variables you want to trim. You can embed both functions. For instance, the following code inserts a name into the customer table and removes any trailing or prefix blank spaces:

    insert into customer (name) values (ltrim (rtrim (' Joe ')))

    In this example, the name "Joe" has spaces trimmed, so the value inserted into the database is "Joe" and not " Joe ."

  • Press "F5" to execute the code and save the stored procedure changes. Close the opened SQL editor to close the stored procedure code.

Related Searches


Promoted By Zergnet


Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!