Ge numbers before te first space  sql

Posted on 2011-05-04
Last Modified: 2012-08-14
Hello there,

How can I get the number before the first space?

123 Dr. Perez Perez
14567 Miami City

This should be the result


Thank you in advance
Question by:hmra
    LVL 142

    Accepted Solution

    select left(yourfield, charindex(' ', yourfield)-1 ) from yourtable
    LVL 16

    Expert Comment

    by:Imran Javed Zia
    Please try following

    declare @txt as varchar(100)
    set @txt = '123 Dr. Perez Perez'
    --set @txt = '14567 Miami City'
    select Substring(@txt, 1, CHARINDEX (' ', @txt)-1)

    Open in new window

    LVL 21

    Expert Comment

    by:Alpesh Patel
    Select SUBSTRING('123 Dr. Perez Perez',CHARINDEX(' ','123 Dr. Perez Perez')+1,len('123 Dr. Perez Perez'))
    LVL 1

    Expert Comment

    HI HRMA,
    you can do it with various ways.
    i think it can be done with three ways.
    1.  Using SUBSTRING function
    2.  Using LEFT Function
    3.  Using PARSENAME function

    However in PARSENAME function you have to take care of few things.
    solution is as mentioned below :

    DECLARE @Employee AS VARCHAR(100)
    SET @Employee = '123 Dr. Perez Perez'
    --      SET @Employee = '14567 Miami City'

    1.  Using SUBSTRING function
    SELECT SUBSTRING(@Employee, 1, CHARINDEX (' ', @Employee)-1)

    2.  Using LEFT Function

    SELECT LEFT(@Employee, CHARINDEX (' ', @Employee)-1)

    3.  Using PARSENAME function
    DECLARE @FullName        VARCHAR(100)
    SET @FullName = '14567 Miami City'

    PARSENAME(REPLACE(@FullName, ' ', '.'), 3) AS [Employee Code]
    ,PARSENAME(REPLACE(@FullName, ' ', '.'), 2) AS [FirstName]
    ,PARSENAME(REPLACE(@FullName, ' ', '.'), 1) AS [LastName]

    I think this will help you..
    LVL 32

    Expert Comment


    select case
             when charindex(' ', Field1) > 0 then
               left(Field1, charindex(' ', Field1)-1 )
             end Field1
    from Table1
    LVL 27

    Expert Comment

    your you doing this on the report or in the sql statement?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Written by Valentino Vranken. Introduction: The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source.  The data…
    Introduction Earlier I wrote an article about the new lookup functions ( that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now