How to Trim in T-SQL

Save

T-SQL (Transact SQL) is the proprietary language used by Microsoft SQL Server. It is an extension of the SQL (Structured Query Language) used for creating, modifying and deleting data stored in relational databases. You can use T-SQL for procedural programming through stored procedures, triggers and views. Use "LTRIM ()" and "RTRIM ()" functions to trim or remove the leading and trailing spaces from a string in SQL Server, helping you to retrieve accurate data.

  • Select "Start" followed by "All Programs" and "Microsoft SQL Server." Click to open the folder, and then select "Microsoft SQL Server Management Studio."

  • Select the server type you want to connect to by clicking from the drop-down menu beside "Server type." Choose the name of server from the drop-down menu beside "Server name." Choose either "SQL Server Authentication" or "Windows Authentication" for "Authentication," and then click "Connect" to establish a connection with the server. It might take a few seconds to a few minutes depending on your type of connection and memory size.

  • Select the database from which you want to extract the data by selecting it from the "Databases" folder in "Object Explorer." Click "New Query" in the main menu to open the "Query Analyzer." Alternatively, you can right-click the database and choose "New Query" from the list of available options.

  • Use "LTRIM ()" function to remove the white spaces from the beginning of the string. The syntax for the function is:

    LTRIM (string)

    For example: "SELECT LTRIM (' SQL Server ') from servertables"

    Highlight the command, right-click and choose "Run" from the available options to execute the command. You will see the result as follows:

    SQL Server

  • Use "RTRIM ()" to remove the spaces after the string. The syntax of the string is:

    RTRIM (string)

    For example: "SELECT RTRIM ('SQL Server ') from servertables"

    Highlight the command by selecting it, and then click the "Run (!)" tab on the main menu. You will see the result as follows:

    SQL Server

References

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

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

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