Selecting part of a string?

  • Does anyone have a method of selecting the first xxx chars until a space is encountered?

    For example if I have a postcode 'GU14 7NJ', I want to be able to get 'GU14' as the result.

    I can't just select the first 4 chars as postcodes are different lengths i.e. W1, CR0

  • hi,

    try :-

    declare @postcode varchar(10), @area_code varchar(10)

    select @postcode = 'GU14 7NJ'

    select @area_code = substring(@postcode, 1, charindex(" ",@postcode))

    print @area_code

    HTH

    Paul

    Edited by - ripg1011 on 09/11/2003 08:25:41 AM

  • perfect - many thanks

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply