SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


patindex problem


patindex problem

Author
Message
kutti45
kutti45
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 34
i have table column in sql server like this

dat1/dat2/dat3/dat4/1/2009
dat1/dat2/dat34/dat4/2/2009
dat1/dat2/dat/sd34/dat4/3/2009
ssf/sdf/sdf/sf/sf/sf/4/2009
ssf/sdf/sdf/sf/sf/sf/10/2009
ssf/sdf/sdf/sf/sdf/sf/sf/44444444/2009
ssf/sdf/sdf/sf//sdf sdf sf/sf/111/2009
ssf/sdf/sdf/sf/sf/sd sf sdf /1798/2009


i want the number alone in that column like this

1
2
3
4
10
44444444
111
1798


please give me the solution
arun.sas
arun.sas
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3077 Visits: 3493
declare @result varchar(50)
set @result = 'ssf/sdf/sdf/sf/sf/sf/10/2009'

select replace(@result,'/2009','')
/*ssf/sdf/sdf/sf/sf/sf/10*/
select reverse(replace(@result,'/2009',''))
/*01/fs/fs/fs/fds/fds/fss*/
select patindex('%/%',reverse(replace(@result,'/2009','')))
/*3*/
select left(reverse(replace(@result,'/2009','')),(patindex('%/%',reverse(replace(@result,'/2009',''))))-1)
/*01*/
select reverse (left(reverse(replace(@result,'/2009','')),(patindex('%/%',reverse(replace(@result,'/2009',''))))-1))
/*10*/


fcastillo 24841
fcastillo 24841
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 4
I think its a little bit late, but if helps someone .
You can put some Replace in the same sentence like this

declare @Table As Table (col nvarchar(50))
INSERT into @table(col)
Select 'dat1/dat2/dat3/dat4/1/2009'
UNION ALL
Select 'dat1/dat2/dat34/dat4/2/2009'
UNION ALL
Select 'dat1/dat2/dat/sd34/dat4/3/2009'
UNION ALL
Select 'ssf/sdf/sdf/sf/sf/sf/4/2009'
UNION ALL
Select 'ssf/sdf/sdf/sf/sf/sf/10/2009'
UNION ALL
Select 'ssf/sdf/sdf/sf/sdf/sf/sf/44444444/2009'
UNION ALL
Select 'ssf/sdf/sdf/sf//sdf sdf sf/sf/111/2009'
UNION ALL
Select 'ssf/sdf/sdf/sf/sf/sd sf sdf /1798/2009'

select LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RePLACE(REPLACE(REPLACE(REPLACE(
col,'dat','') ,'sdf',''),'ssf',''),'2009',''),'/',''),'sf',''),'sd',''),'12344',''),'1234',''))) from @Table
scott.pletcher
scott.pletcher
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1164 Visits: 473
If the string you want is always the next-to-last one and is always delimited by '/', this should do it:


SELECT
SUBSTRING(data, LEN(data) - CHARINDEX('/', REVERSE(data), CHARINDEX('/', REVERSE(data)) + 1) + 2,
CHARINDEX('/', REVERSE(data), CHARINDEX('/', REVERSE(data)) + 1) - CHARINDEX('/', REVERSE(data)) - 1)
FROM (
SELECT 'dat1/dat2/dat3/dat4/1/2009' AS data UNION ALL
SELECT 'dat1/dat2/dat34/dat4/2/2009' UNION ALL
SELECT 'dat1/dat2/dat/sd34/dat4/3/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sf/4/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sf/10/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sdf/sf/sf/44444444/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf//sdf sdf sf/sf/111/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sd sf sdf /1798/2009'
) AS test_data



Scott Pletcher, SQL Server MVP 2008-2010
R.P.Rozema
R.P.Rozema
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2898 Visits: 1717
Using a common table expression you can also do neat tricks. I've interpreted your task as "return the next-to-last word from each of the /-separated list of words". In these string manipulations you have to be very careful with the substring() function: passing it a negative value for the index will result in very-hard-to-debug runtime errors. I think this should deal correctly with all sorts of input:

declare @tbl table (
data nvarchar(max) null
);

insert @tbl(data)
SELECT 'dat1/dat2/dat3/dat4/1/2009' AS data UNION ALL
SELECT 'dat1/dat2/dat34/dat4/2/2009' UNION ALL
SELECT 'dat1/dat2/dat/sd34/dat4/3/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sf/4/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sf/10/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sdf/sf/sf/44444444/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf//sdf sdf sf/sf/111/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sd sf sdf /1798/2009' UNION ALL
SELECT '2009' union all
select null;


with cteWords as (
select
1 as nr,
reverse(case when charindex('/', t.data) > 0
then left(t.data, charindex('/', t.data) - 1)
else t.data
end) as word,
case when charindex('/', t.data) > 0
then RIGHT(t.data, len(t.data) - charindex('/', t.data))
else CONVERT(nvarchar(max),null)
end as remainder
from @tbl tbl
cross apply (
select REVERSE(tbl.data) as data
) t

union all

select
c.nr + 1 as nr,
reverse(case when charindex('/', c.remainder) > 0
then left(c.remainder, charindex('/', c.remainder) - 1)
else c.remainder
end) as word,
case when charindex('/', c.remainder) > 0
then right(c.remainder, len(c.remainder) - charindex('/', c.remainder))
else null
end as remainder
from cteWords c
where not c.remainder is null
)
select word
from cteWords
where nr = 2;





Posting Data Etiquette - Jeff Moden
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
scott.pletcher
scott.pletcher
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1164 Visits: 473
You do need at least two slashes in all data for my previous code to work. If you may not have 2 slashes, you can do this:



SELECT
CASE WHEN data NOT LIKE '%/%/%' THEN '' ELSE
SUBSTRING(data, LEN(data) - CHARINDEX('/', REVERSE(data), CHARINDEX('/', REVERSE(data)) + 1) + 2,
CHARINDEX('/', REVERSE(data), CHARINDEX('/', REVERSE(data)) + 1) - CHARINDEX('/', REVERSE(data)) - 1) END
FROM (
SELECT 'dat1/dat2/dat3/dat4/1/2009' AS data UNION ALL
SELECT 'dat1/dat2/dat34/dat4/2/2009' UNION ALL
SELECT 'dat1/dat2/dat/sd34/dat4/3/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sf/4/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sf/10/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sdf/sf/sf/44444444/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf//sdf sdf sf/sf/111/2009' UNION ALL
SELECT 'ssf/sdf/sdf/sf/sf/sd sf sdf /1798/2009' UNION ALL
SELECT '' UNION ALL
SELECT '2009'
) AS test_data



Scott Pletcher, SQL Server MVP 2008-2010
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search