Thursday, December 18, 2014

search keyword in sql

declare @CityPostCode varchar(500)
set @CityPostCode='hghgh gtr rt gtr yy'
declare @substring varchar(15)

if @CityPostCode <> ''            
             
begin              
                       
WHILE (CHARINDEX(' ',@CityPostCode ,1)<>0)                          
BEGIN                          
 SET @substring = substring(@CityPostCode ,1,CHARINDEX(' ',@CityPostCode ,1)-1)                        
  -- Find Substring up to Separator                          
  if (isnull(@substring,' ') <> ' ')                                      
   begin                
    select @substring ,1
   end                
    SET @CityPostCode = substring(@CityPostCode ,Len(@substring)+2,Len(@CityPostCode ))                
  end              
               
end            

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Code is not working properly..... Correct code is as below-

    declare @CityPostCode varchar(500)
    set @CityPostCode='hghgh gtr rt gtr yy'
    declare @substring varchar(15)

    if @CityPostCode <> ''

    begin

    WHILE (CHARINDEX(' ',@CityPostCode ,1)<>0)
    BEGIN
    SET @substring = substring(@CityPostCode ,1,CHARINDEX(' ',@CityPostCode ,1)-1)
    -- Find Substring up to Separator
    if (isnull(@substring,' ') <> ' ')
    begin
    select @substring ,1
    end
    SET @CityPostCode = substring(@CityPostCode ,Len(@substring)+2,Len(@CityPostCode ))
    end
    if (isnull(@CityPostCode,' ') <> ' ')
    select @CityPostCode,1
    end

    ReplyDelete