Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 834859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:44:12+00:00 2026-05-15T04:44:12+00:00

I’m looking for a way to do this … SELECT FirstName, LastName, Split(AddressBlock, ‘

  • 0

I’m looking for a way to do this …

SELECT FirstName, LastName, Split(AddressBlock, '  ', 1), Split(AddressBlock, ' ', 2), PostCode 
FROM Contacts

The arguments I want to pass are …

  1. The address
  2. The separator (current situation requires 2 spaces but this might be a comma or a space followed by a comma) or something else (it varies).
  3. The address part I want to return (i don’t always need all parts of the split result).

I seem to be able to find a few examples of splitting functions about the internet but they return a table containing the entire set of split parts.

My SQL skills aren’t that great so I need the answer to be ultra simple.
I’m always working with nvarchar data and the function needs to be reusable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:44:12+00:00Added an answer on May 15, 2026 at 4:44 am

    If you want a user-defined function to do this, this should work. Not that pretty, but…

    CREATE FUNCTION dbo.SplitStringPart (
        @input nvarchar(MAX),
        @separator nvarchar(10),
        @index int
    ) RETURNS nvarchar(MAX)
    BEGIN
    
    DECLARE @counter int,
            @position int,
            @oldposition int,
            @separatorlength int,
            @result nvarchar(MAX)
    
    SET @separatorlength = DATALENGTH(@separator) / 2
    IF @separatorlength = 0 RETURN NULL
    
    SET @result = NULL
    
    SET @counter = 1
    SET @position = -2
    
    WHILE (@counter <= @index)
    BEGIN
    
        SET @oldposition = @position
        SET @position = CHARINDEX(@separator, @input, @position + 1)
        IF @position = 0 AND @counter < @index
        BEGIN
            SET @oldposition = 0
            BREAK
        END
        SET @counter = @counter + 1
    
    END
    
    IF @oldposition = 0 AND @position = 0
        RETURN NULL
    ELSE IF @oldposition < 0
    BEGIN
        IF @position = 0 AND @index = 1
            SET @result = @input
        ELSE
            SET @result = SUBSTRING(@input, 0, @position)
    END
    ELSE IF @position <= 0
        SET @result = SUBSTRING(@input, @oldposition + @separatorlength, LEN(@input) - @oldposition - @separatorlength)
    ELSE
        SET @result = SUBSTRING(@input, @oldposition + @separatorlength, @position - @oldposition - @separatorlength)
    
    RETURN @result
    
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 465k
  • Answers 465k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, with (very) few exceptions, it's generally bad idea to… May 16, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer The IDENTITY_INSERT setting only works in the current SQL session,… May 16, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer It does depend on what kind of web development you're… May 16, 2026 at 1:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.