Add minus symbol in sql query

  • As SQL is not my main job what might seem easy to many here has me stumped.:blush:

    I have written some SQL querying a invoice and invoice items tables that works fine. The Sales invoice records all have a "SI" prefix. The query also, by design, picks up the credit notes which have a "SC" prefix.

    The only issue I'm stuck on is, I need the values against the "SC" prefix items to have a minus - symbol in front of the value. The output is used by the accounts guys who put straight into Excel.

    Any ideas?

  • The - symbol usually denotes a negative value, so that's probably right. Hard to tell with the limited information.

    What in particular is the concern? You want it to strip off the negative on the numerics? Use ABS(<value>) to Absolute it as a positive. I'd personally be leery of doing this as you'll miss inversions, but if you're sure of your data go for it.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • If you want a negative value, then just multiply the output column by -1

    e.g. select col_name * (-1)

  • Thanks guys, but...

    Let me try and be more clear.

    The data is coming from a table where the monetary value is always "just" a value so the output looks like:

    Example:

    Columns.

    Inv/Credit Amount

    SI-100 10.00

    SI-200 12.75

    SI- 300 250.00

    SC-100 18.50

    Where in the Inv/Credit column the id is prefixed with a "SC" I know that's a credit note so in this report - written in BIRT - for the accounts guys, I need to leave the "SI" values "as is" but have a minus symbol where there is a "SC" so it outputs like this:

    Columns.

    Inv/Credit Amount

    SI-100 10.00

    SI-200 12.75

    SI- 300 250.00

    SC-100 -18.50

  • Malcolm,

    If your values will always be in that format and lenght, you can try something like this:

    select Left(ltrim(rtrim(YourFieldNameHere)), 6) + ' - ' + SUBSTRING(ltrim(rtrim(YourFieldNameHere)), 8, len(YourFieldNameHere)-7) as YourNewValue

    from Blah

    Just replace "YourFieldNameHere" with your actual field/column name and see it that works for you.

    Mark

  • malcolm.garbett (11/3/2012)


    Thanks guys, but...

    Let me try and be more clear.

    The data is coming from a table where the monetary value is always "just" a value so the output looks like:

    Example:

    Columns.

    Inv/Credit Amount

    SI-100 10.00

    SI-200 12.75

    SI- 300 250.00

    SC-100 18.50

    Where in the Inv/Credit column the id is prefixed with a "SC" I know that's a credit note so in this report - written in BIRT - for the accounts guys, I need to leave the "SI" values "as is" but have a minus symbol where there is a "SC" so it outputs like this:

    Columns.

    Inv/Credit Amount

    SI-100 10.00

    SI-200 12.75

    SI- 300 250.00

    SC-100 -18.50

    You may have to change the column names and, certainly, the table name, but the following will do it quite nicely.

    SELECT [Inv/Credit],

    Amount = CASE WHEN LIKE 'SC%' THEN Amount ELSE -Amount END

    FROM dbo.YourTable

    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (11/3/2012)


    malcolm.garbett (11/3/2012)


    Thanks guys, but...

    Let me try and be more clear.

    The data is coming from a table where the monetary value is always "just" a value so the output looks like:

    Example:

    Columns.

    Inv/Credit Amount

    SI-100 10.00

    SI-200 12.75

    SI- 300 250.00

    SC-100 18.50

    Where in the Inv/Credit column the id is prefixed with a "SC" I know that's a credit note so in this report - written in BIRT - for the accounts guys, I need to leave the "SI" values "as is" but have a minus symbol where there is a "SC" so it outputs like this:

    Columns.

    Inv/Credit Amount

    SI-100 10.00

    SI-200 12.75

    SI- 300 250.00

    SC-100 -18.50

    You may have to change the column names and, certainly, the table name, but the following will do it quite nicely.

    SELECT [Inv/Credit],

    Amount = CASE WHEN LIKE 'SC%' THEN Amount ELSE -Amount END

    FROM dbo.YourTable

    ;

    That's close, but I fear I have misled you...

    The SI/SC is not the value field, that's the identifier, and another field is the value.

    I tried this, based on your help:

    SELECT

    invoiceitems.nominalaccountid,

    traders.name,

    invoiceitems.partid,

    invoices.id,

    invoiceitems.itemnumber,

    invoices.taxdate,

    invoiceitems.homenettvalue, CASE WHEN invoices.id LIKE 'SI%' THEN invoiceitems.homenettvalue ELSE -invoiceitems.homenettvalue END,

    invoiceitems.hometaxvalue

    from dbo.table

    This adds a column - with no name - repeats the values from the invoiceitems.homenettvalue BUT adds the minus symbol when the invoices.id = 'SC% 'which is what I'm after, but not in a new column. So at the moment it now looks like this:

    nominalaccountidnamepartididitemnumberhomenettvalue(No column name)hometaxvalue

    1129-BALSHEETElektrameric Ltd294-34200SI-02021710.000.000.00

    1129-BALSHEETElektrameric Ltd294-35610SI-02021720.000.000.00

    1129-BALSHEETBryan Donkin RMG Gas Cont294-23090SI-020350714.0014.002.45

    1160-BALSHEETShenzhen Oska Measurement SystemsE8UPGRADEMISCSC-03058511619.8199-1619.81990.00

    1160-BALSHEETShenzhen Oska Measurement SystemsE8UPGRADEMISCSC-03059311619.8199-1619.81990.00

    1160-BALSHEETShenzhen Oska Measurement SystemsE8UPGRADEMISCSI-03059111619.81991619.81990.00

    2210-BALSHEETISIS Precision Engineering Ltd.SR000000SI-02989920.000.000.00

    2210-BALSHEETISIS Precision Engineering Ltd.SR000000SI-02989930.000.000.00

    2210-BALSHEETSpire Machining Ltd.MS0003INSI-0302471295.00295.0051.63

  • Nah... you didn't mislead. I forgot to include the column name being checked by the case statement. You picked up on that correct, it would appear.

    For your output, just alias the CASE statement with columnname = CASE .... and you should be all set.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (11/5/2012)


    Nah... you didn't mislead. I forgot to include the column name being checked by the case statement. You picked up on that correct, it would appear.

    For your output, just alias the CASE statement with columnname = CASE .... and you should be all set.

    Forgive me for being obtuse but...

    Does this mean there will always be two columns though? Or is there a way to have just one column with the minus prefix when the conditions of the CASE statement are met?

    (Really appreciate this help, by the way. 🙂 )

  • malcolm.garbett (11/5/2012)


    Jeff Moden (11/5/2012)


    Nah... you didn't mislead. I forgot to include the column name being checked by the case statement. You picked up on that correct, it would appear.

    For your output, just alias the CASE statement with columnname = CASE .... and you should be all set.

    Forgive me for being obtuse but...

    Does this mean there will always be two columns though? Or is there a way to have just one column with the minus prefix when the conditions of the CASE statement are met?

    (Really appreciate this help, by the way. 🙂 )

    You can remove the Amount column that doesn't have the CASE statement. The Case statement will do just as you asked. It'll return a negative amount when the INV/Credit column starts with "SC" and a positive amount for everything else.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I see it now. 🙂

    Many, many thanks.

Viewing 11 posts - 1 through 10 (of 10 total)

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