Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Replace last comman in a field with 'and'

Author  Topic 

offspring22
Starting Member

38 Posts

Posted - 2011-11-30 : 13:27:18
Hello,

I have a column with records such as:

interior above grade, wall thickness
wall thickness, below grade area, interior above grade
interior above grade
interior above grade, wall thickness

etc.

I need to replace the last comma in each (if it has one) with "and" so it reads more like proper grammar.

Any suggestions on how to do so?

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-11-30 : 13:44:13
declare @str varchar(100)= 'wall thickness, below grade area, interior above grade'


select REVERSE(STUFF(reverse(@str),PATINDEX('%,%',reverse(@str)) ,5,' dna '))


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

offspring22
Starting Member

38 Posts

Posted - 2011-11-30 : 14:53:28
Hi Jim,

The data is being created by the code:

replace
(replace
(replace
(replace
(replace
(replace
(replace
(replace
(replace
(replace(RegisteredSizeIncl,'ATTGR', ' attached garage')
,'BALTR', ' balcony/terrace')
,'BLWGR', ' below grade area')
,'DETGR', ' detached garage')
,'IAGR', ' interior above grade')
,'INDPK', ' indoor parking stall')
,'LAND', ' land')
,'OUTPK', ' outdoor parking stall')
,'STORE', ' storage area')
,'WT', ' wall thickness')

I was hoping for something where I could use that, and have it replace the last comma. I can't seem to get what you have provided to work.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-12-01 : 05:07:22
Try this


select reverse(stuff(reverse(registeredsizeincl),patindex('%,%',reverse(registeredsizeincl)) ,5,' dna ')) from
(
select replace
(replace
(replace
(replace
(replace
(replace
(replace
(replace
(replace
(replace(registeredsizeincl,'attgr', ' attached garage')
,'baltr', ' balcony/terrace')
,'blwgr', ' below grade area')
,'detgr', ' detached garage')
,'iagr', ' interior above grade')
,'indpk', ' indoor parking stall')
,'land', ' land')
,'outpk', ' outdoor parking stall')
,'store', ' storage area')
,'wt', ' wall thickness') as registeredsizeincl
from table
) as t

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-01 : 05:13:22
i didnt understand why you need so many replaces. better method would be to put these words against their short names in a mapping table and then use it for replacement

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -