Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MS SQL SERVER - STORED PROCEDURE


SELECT AIDNO,
(ALOANUMBER + AIDNO) AS LOAM
FROM VLOANS

how do you code (ALOANUMBER + AIDNO) AS LOAM to trim all spaces


Thanks
Posted

You just need to trim both pieces.

SQL
SELECT AIDNO, RTRIM(LTRIM(ALOANNUMBER)) + RTRIM(LTRIM(AIDNO)) AS LOAM
FROM VLOANS
 
Share this answer
 
Comments
Abhinav S 17-Apr-15 8:55am    
Yep of course.5.
Suvendu Shekhar Giri 17-Apr-15 8:59am    
You are faster :) +5
To trim spaces, you can make use of LTRIM() and RTRIM() functions
Check this complete article on trimming by my favorite, Pinal Dave.
SQL SERVER – 2008 – TRIM() Function – User Defined Function[^]

Example:
For individual field trimming-
SQL
SELECT AIDNO,
(LTRIM(RTRIM(ALOANUMBER))) + LTRIM(RTRIM(AIDNO))) AS LOAM
FROM VLOANS

For the concatenated result trimming-
SQL
SELECT AIDNO,
LTRIM(RTRIM((ALOANUMBER + AIDNO))) AS LOAM
FROM VLOANS

Hope, it helps :)

Note: For SQL Server 2012/2014, there is a function exists, TRIM() which does the same thing instead of using two functions.
 
Share this answer
 
v2
Comments
Abhinav S 17-Apr-15 8:55am    
Mera 5.
Suvendu Shekhar Giri 17-Apr-15 8:57am    
Thanks ! Gald that you liked it :)
ZurdoDev 17-Apr-15 9:04am    
Did they finally add TRIM()? I had not heard that but it's about time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900