Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a column named 'Address' of some database, in which Data is something like this...

-- 'D.No 5-34,5th Road,sanjeevanagar|Cityname|Pincode|state|Country'
-- 'dgfkj gdkh fghk|CityName|pincode|state|country'

i want to fetch CityName to use it as a parameter ...please suggest the query...thanx in advance
Posted
Updated 19-Mar-12 4:51am
v2

Here is the Function, Just copy, paste and enjoy..

create function [dbo].[SplitGETPos](
@String varchar(1000), 
@Delimiter char(1),
@POSITION INT) 
returns varchar(20)
as     
begin     
    declare @OUTPUT varchar(20);
    declare @idx int;     
    DECLARE @CHARIDX INT;
    DECLARE @INPUTSTR VARCHAR(1000);
    SET @INPUTSTR=@String + ',';
    SET @CHARIDX =0;
    SET @idx=0;
    while @idx < @POSITION   
    begin     
          SET @CHARIDX= CHARINDEX(@Delimiter, @INPUTSTR);
          SET @OUTPUT=SUBSTRING(@INPUTSTR, 1, @CHARIDX - 1)
          SET @INPUTSTR = SUBSTRING(@INPUTSTR,@CHARIDX +1,LEN(@INPUTSTR));
        SET @idx=@idx+1;
    end 
return  @OUTPUT
end


Thanks
--RA
 
Share this answer
 
Comments
DileepkumarReddy 19-Mar-12 11:50am    
got it...Thank u very much...
Rajesh Anuhya 19-Mar-12 22:48pm    
Vote & accept my answer, if it's helps you.
--RA(mobile post)
Here is my procedure for you, I have written this procedure long back. change it as function and use in your select statement

create PROCEDURE [dbo].[SplitGETPos](
@String varchar(8000),
@Delimiter char(1),
@POSITION INT,
@OUTPUT VARCHAR(100) OUTPUT)    
WITH
EXECUTE AS CALLER
as    
begin    
    declare @idx int;    
    DECLARE @CHARIDX INT;
    DECLARE @INPUTSTR VARCHAR(8000);
    SET @INPUTSTR=@String;
    SET @CHARIDX =0;
    SET @idx=0;
    while @idx < @POSITION  
    begin    
          SET @CHARIDX= CHARINDEX(@Delimiter, @INPUTSTR);
          SET @OUTPUT=SUBSTRING(@INPUTSTR, 1, @CHARIDX - 1)
          SET @INPUTSTR = SUBSTRING(@INPUTSTR,@CHARIDX +1,LEN(@INPUTSTR));
        SET @idx=@idx+1;
    end
return    
end


Thanks
--RA
 
Share this answer
 
v2
Comments
DileepkumarReddy 19-Mar-12 10:57am    
Thank you so much...i will check it out..
DileepkumarReddy 19-Mar-12 11:03am    
Hi, what is Position and Output parameters here...i dint understand...


what do i need to pass in them..Please explain
Rajesh Anuhya 19-Mar-12 11:06am    
Position mean:
========================
Rajesh|Ramesh|Rakesh
1 2 3
=========================
1,2,3 are the positions

I am returning the value in output parameters, just change this procedure to function(which returns the output value)
--RA
DileepkumarReddy 19-Mar-12 11:19am    
create function [dbo].[SplitGETPos](
@String varchar(8000),
@Delimiter char(1),
@POSITION INT,
@OUTPUT VARCHAR(100) OUTPUT)
--WITH
Returns varchar(100)
--EXECUTE AS CALLER
as
begin
declare @idx int;
DECLARE @CHARIDX INT;
DECLARE @INPUTSTR VARCHAR(8000);
SET @INPUTSTR=@String;
SET @CHARIDX =0;
SET @idx=0;
while @idx < @POSITION
begin
SET @CHARIDX= CHARINDEX(@Delimiter, @INPUTSTR);
SET @OUTPUT=SUBSTRING(@INPUTSTR, 1, @CHARIDX - 1)
SET @INPUTSTR = SUBSTRING(@INPUTSTR,@CHARIDX +1,LEN(@INPUTSTR));
SET @idx=@idx+1;
return @OUTPUT
end



Msg 181, Level 15, State 1, Procedure SplitGETPos, Line 5
Cannot use the OUTPUT option in a DECLARE or CREATE FUNCTION statement.
Msg 137, Level 15, State 1, Procedure SplitGETPos, Line 20
Must declare the scalar variable "@OUTPUT".
Msg 137, Level 15, State 2, Procedure SplitGETPos, Line 25
Must declare the scalar variable "@OUTPUT".



unable to create function...help me out
Rajesh Anuhya 19-Mar-12 11:23am    
See my next answer
--RA

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