none
extract data between 3 and 6th positions

    Question

  • Hi Gurus, 

    I want to extract data between 3rd and 6th positions. 

    I have data like this:

    Fld1

    5016789

    509876666

    5033284453

    O/p

    1678

    9876

    3328

    I can't think of a way to use SUBSTRING OR FINDSTRING because there's no common relation between these values. 

    Please help me with this.

    Thanks

    Wednesday, September 03, 2014 1:08 AM

Answers

  • Isnt this enough?

    SELECT SUBSTRING(Fld1,3,4) FROM Table
    or
    SELECT LEFT(STUFF(Fld1,1,2,''),4) FROM Table


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Wednesday, September 03, 2014 7:05 AM

All replies

  • declare @st varchar(20)='50167890' ---(T-SQL)


    select substring (@st,3+1,3)

    Last parameter in sub string function is a claculation 6-3 


    Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting: Large scale of database and data cleansing
    Remote DBA Services: Improves MS SQL Database Performance
    SQL Server Integration Services: Business Intelligence

    Wednesday, September 03, 2014 4:48 AM
  • Its more of same as what Uri said. But small change i did.

    declare @st varchar(20)='509876666' ---(T-SQL)

    select substring (@st,3,4)

    This will give you the data between 3rd and 6th position

    -----------Mark as Answered/Helpful if your problem is solved.----------------


    Wednesday, September 03, 2014 6:27 AM
  • Isnt this enough?

    SELECT SUBSTRING(Fld1,3,4) FROM Table
    or
    SELECT LEFT(STUFF(Fld1,1,2,''),4) FROM Table


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Wednesday, September 03, 2014 7:05 AM
  • Isnt this enough?

    SELECT SUBSTRING(Fld1,3,4) FROM Table
    or
    SELECT LEFT(STUFF(Fld1,1,2,''),4) FROM Table


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Ah..Thanks I was using SUBSTRING(fLd1,3,6) and I was getting incorrect results. 

    Now, it makes sense.

    Thanks everyone for your answers.

    Wednesday, September 03, 2014 1:06 PM