Remove Characters from String

  • I have a set of data that gives me month name and year in this format:

    FiscalMonth

    July, 2008

    August, 2008

    September, 2008

    I need to select only the month name part of the string, so everything from the comma to the right needs to be excluded.

    I tried searching for a forum subject on this, but couldn't find anything. If there is another post with information on this, please let me know.

    Thank you.

    _______________________________
    [font="Tahoma"]Jody Claggett
    SQL Server Reporting Analyst
    [/font][/size]

  • To select just the month, that is data to the left of the first comma try this code to give you an idea of what you can do:

    DECLARE @FM as VARCHAR(10)

    DECLARE @Indata AS VARCHAR(20)

    SET @Indata = 'July, 2008'

    SET @FM = (SELECT(SUBSTRING(@Indata,1,(CHARINDEX(CHAR(44), @indata)-1))))

    PRINT 'For testing fiscal month is*' + @FM + '*'

    at this point the variable @FM contains what you want and you can then insert this into a column in a table, use the set statement in a function or use it as you wish.

    I included:

    PRINT 'For testing fiscal month is*' + @FM + '*'

    so that you can verify your results before using, of course in actual use you will not include the PRINT statement

    CHAR(44) is the ASCII comma.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Thank you, that worked!

    _______________________________
    [font="Tahoma"]Jody Claggett
    SQL Server Reporting Analyst
    [/font][/size]

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

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