SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 trim off last AND
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1341 Posts

Posted - 07/06/2012 :  08:28:38  Show Profile  Reply with Quote
Hi,
How do I trim off the 'AND' if it is present at the end of the line?
Thanks

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 07/06/2012 :  08:33:58  Show Profile  Visit webfred's Homepage  Reply with Quote
declare @line varchar(255)
set @line = 'bla and bla and bla and'
select
reverse(stuff(reverse(@line),1,4,''))



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1341 Posts

Posted - 07/06/2012 :  08:58:20  Show Profile  Reply with Quote
But what if there is no 'AND'
The sql should know if there is or there is not an AND at the end of the line.
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 07/06/2012 :  09:00:39  Show Profile  Visit webfred's Homepage  Reply with Quote
good point:

declare @line varchar(255)
set @line = 'bla and bla and bla and'
select
case
when right(@line,4)=' and' then reverse(stuff(reverse(@line),1,4,''))
else @line
end



No, you're never too old to Yak'n'Roll if you're too young to die.

Edited by - webfred on 07/06/2012 09:01:39
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 07/06/2012 :  09:05:01  Show Profile  Visit webfred's Homepage  Reply with Quote
or this:
reverse(stuff(reverse(@line),1,case when right(@line,4)=' and' then 4 else 0 end,''))


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1341 Posts

Posted - 07/06/2012 :  09:47:45  Show Profile  Reply with Quote
quote:
Originally posted by webfred

good point:

declare @line varchar(255)
set @line = 'bla and bla and bla and'
select
case
when right(@line,4)=' and' then reverse(stuff(reverse(@line),1,4,''))
else @line
end



No, you're never too old to Yak'n'Roll if you're too young to die.


Many thanks
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000