SQL Substring function with IF statement

Posted on 2011-10-20
Last Modified: 2012-05-12
I have the following statement that is throwing an error:

IF SUBSTRING(field1, 2, 1) = '-' THEN field1 = '0' + field1 END IF
WHERE     DO = 'TEST' AND Revision = 'BASELINE'

What I am trying to accomplish is to check if the second character in the field 'field1' is a hyphen '-' then if it is I would like to add a '0' to the beginning of field1
Question by:zintech
    LVL 23

    Accepted Solution

    UPDATE YourTableName
    SET field1 = '0' + field1
    WHERE SUBSTRING(field1, 2, 1) = '-'
    	AND DO = 'TEST' AND Revision = 'BASELINE'

    Open in new window

    LVL 10

    Expert Comment

    For MS SQL you should use the CASE statement.  For your code it would be something like:

    SELECT myfield = CASE SUBSTRING(field1, 2, 1) WHERE '-' THEN '0' & field1 ELSE field1 END

    I haven't used CASE must, so someone else may be able to post more accurate code.  For reference you can look at MSDN:

    Expert Comment

    For MS SQL Server, if you are trying to update a column in a table, then here us the syntax for two appoaches:

    Approach #1:
    UPDATE <table name>
    SET field1 =
            WHEN SUBSTRING(field1, 2, 1) = '-' THEN field1 = '0' + field1
            ELSE field1
    WHERE     DO = 'TEST' AND Revision = 'BASELINE'

    I included this approach to illustrate how to use the CASE construct, but it has the disadvantage of causing every record in the table to be updated, regardless of whether SUBSTRING(field1, 2, 1) = '-'.

    Approach #2:
    UPDATE <table name>
    SET field1 = '0' + field1
    WHERE     DO = 'TEST'
    AND          Revision = 'BASELINE'
    AND          SUBSTRING(field1, 2, 1) = '-'

    This approach will perform better because only the records that must be changed are updated.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    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.

    728 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

    19 Experts available now in Live!

    Get 1:1 Help Now