Click here to Skip to main content
15,881,635 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Dear All,

I have written the following Query that works fine with access database:

Update MyTable Set [Column New]=IIf(Right(Trim([Column New]),1)=';',Mid([Column New],1,Len([Column New])-1),[Column New])


But, when I execute the same in SQL Server 2008, It says:

'Trim' is not a recognized built-in function name.


How Do I write the same in SQL Server ?

Any help or suggestion would be greatly appreciated.



Regards,
Raj
Posted
Comments
Raj.rcr 13-Sep-11 2:54am    
Thank you Abhinav and Simon... The following is the query that is working fine :

UPDATE MyTable SET [Column New] = CASE WHEN RIGHT(LTRIM(RTRIM([Column New])),1) = ';' THEN SUBSTRING([Column New], 1, LEN([Column New])-1) ELSE [Column New] END

SQL Server does not have a TRIM function. It has LTRIM and RTRIM that you can use together. There is no MID or RIGHT as well. You need to use SUBSTRING instead[^].
 
Share this answer
 
Comments
Mehdi Gholam 12-Sep-11 2:44am    
My 5!
Abhinav S 12-Sep-11 2:47am    
Thank you Mehdi.
Raj.rcr 12-Sep-11 2:46am    
But there's a issue with IIf also... What can I use for IIf?
Abhinav S 12-Sep-11 2:48am    
SQL Server provides support for an IF...Else block.
As additional info, you could also use CASE.
Simon Bang Terkildsen 12-Sep-11 2:53am    
Or wait for SQL Server Denali as it adds support for IIF
As you've now found out T-SQL doesn't have a TRIM function, you can use RTRIM and LTRIM instead, e.g. RTRIM(LTRIM([Column New]))

you can also make your own TRIM function
SQL
CREATE FUNCTION dbo.TRIM(@string VARCHAR(MAX)) RETURNS VARCHAR(MAX)
BEGIN
    RETURN RTRIM(LTRIM(@string))
END
 
Share this answer
 
v2
Comments
Mehdi Gholam 12-Sep-11 2:47am    
My 5!
Simon Bang Terkildsen 12-Sep-11 2:49am    
Thank you, Mehdi
Raj.rcr 12-Sep-11 2:47am    
But there's an issue with IIf also... What can I use for IIf?
Abhinav S 12-Sep-11 2:49am    
5 for building your own function.
Pravin Patil, Mumbai 12-Sep-11 2:55am    
My 5 too for the function...

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