SQL - create a string with bracket if not null

Posted on 2012-09-16
Last Modified: 2012-09-16
I like to create a string with bracket if not null, but return a blank string '' if null.

e.g. if the string is 'ABC',  I will like to get (ABC)
if the string is null, I will like to get an empty string ''

How will I do that?
Question by:tommym121
    LVL 25

    Accepted Solution

    use ISNULL to turn the null value into an empty string, concatenate the str with the brackets to get the bracketed string ... if the value the was null, concatenation will be null still.  e.g.
        select isnull('('+str+')',''), str
        from (select 'abc' as str union all select null)v

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    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…
    This video discusses moving either the default database or any database to a new volume.

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now