Query

  • Dear All

    I have one scnario. One column i am having Following records

    1a

    2a

    2b

    ...

    .

    .

    .

    .

    10a

    10b

    But i want following output through Query

    1

    2

    2

    .

    .

    .

    .

    10

    10

    .

  • Not much information to go on.

    Is it only a and b in the column, or are there other characters present?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • You can use some of the string functions like CHARINDEX, PATINDEX, LEFT, SUBSTRING, etc.

    Try to find the first non numeric character and extract the substring from the beginning to this position.

    SELECT

    T.c1,

    CASE WHEN R.c1 > 1 THEN SUBSTRING(T.c1, 1, R.c1 - 1) ELSE T.c1 END AS c2

    FROM

    (VALUES ('1a'), ('10a'), ('110a'), ('220')) AS T(c1)

    CROSS APPLY

    (VALUES (PATINDEX('%[^0-9]%', T.c1))) AS R(c1);

  • You'll have to play around with the String Functions to get what you want.

    Maybe...

    SELECT REVERSE(SUBSTRING(REVERSE('1a'), 2, LEN('1a')));

    SQL and Strings... not exactly a match made in heaven.

  • hunchback (9/24/2013)


    You can use some of the string functions like CHARINDEX, PATINDEX, LEFT, SUBSTRING, etc.

    Try to find the first non numeric character and extract the substring from the beginning to this position.

    SELECT

    T.c1,

    CASE WHEN R.c1 > 1 THEN SUBSTRING(T.c1, 1, R.c1 - 1) ELSE T.c1 END AS c2

    FROM

    (VALUES ('1a'), ('10a'), ('110a'), ('220')) AS T(c1)

    CROSS APPLY

    (VALUES (PATINDEX('%[^0-9]%', T.c1))) AS R(c1);

    Mighty convoluted of you to use 2 table row constructors like that but I like it. 🙂

    Borrowing the one with your sample data, I'll offer another option that is described in the 4th link in my signature.

    SELECT a.c1, b.Item

    FROM

    (

    VALUES ('1a'), ('10a'), ('110a'), ('220')

    ) AS a(c1)

    CROSS APPLY dbo.PatternSplitCM(a.C1, '[0-9]') b

    WHERE [Matched]=1;

    Probably not as fast as hunchback's but that's because it's a more generalized tool.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • SELECT REPLACE('2b',SUBSTRING('2b',PATINDEX('%[^0-9]%','2b'),1), '')

  • SELECT LEFT('21b',len('21b')-1)

Viewing 7 posts - 1 through 6 (of 6 total)

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