Query

  • I have a column from two records in the database that look like the following:

    PC 20020708

    PC 20020201,L 20020201,H 20020201

    Respectively I want them to look like:

    P 20020708, C 20020708

    P 20020201, C 20020201, L 20020201, H 200201

    What I can’t figure out is how to say the replace equivalent of what you could do in a like:

    Replace(strLinesDate, ‘PC ________’, newtext);

    Where it replaces PC, the space, and then the next 8 characters with the expanded text. I have a solution for getting the expanded text, just can’t figure out how to get the proper value to be replaced.

    Any ideas guys?

    The_SQL_DBA
    MCTS

    "Quality is never an accident; it is always the result of high intention, sincere effort, intelligent direction and skillful execution; it represents the wise choice of many alternatives."

  • Something like this?

    [font="Courier New"]SELECT CASE WHEN LEFT(strLinesDate, 2) = 'PC'

       THEN REPLACE(strLinesDate,

           LEFT(strLinesDate, 11),

           'P  ' + SUBSTRING(strLinesDate,4,8) + ', ' + 'C  ' + SUBSTRING(strLinesDate,4,8))

       ELSE strLinesDate END AS strLinesDate

    FROM (

    SELECT 'PC 20020708' AS strLinesDate UNION ALL

    SELECT 'PC 20020201, L  20020201, H  20020201'

    ) d[/font]

    Cheers

    ChrisM

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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