Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add minus symbol in sql query


Add minus symbol in sql query

Author
Message
malcolm.garbett
malcolm.garbett
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 92
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?
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5715 Visits: 7660
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>Wink 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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
arnipetursson
arnipetursson
Old Hand
Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)Old Hand (339 reputation)

Group: General Forum Members
Points: 339 Visits: 1019
If you want a negative value, then just multiply the output column by -1
e.g. select col_name * (-1)
malcolm.garbett
malcolm.garbett
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 92
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
Mark Eckeard
Mark Eckeard
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 505
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



Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45520 Visits: 39948
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
malcolm.garbett
malcolm.garbett
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 92
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:

nominalaccountid name partid id itemnumber homenettvalue (No column name) hometaxvalue
1129-BALSHEET Elektrameric Ltd 294-34200 SI-020217 1 0.00 0.00 0.00
1129-BALSHEET Elektrameric Ltd 294-35610 SI-020217 2 0.00 0.00 0.00
1129-BALSHEET Bryan Donkin RMG Gas Cont 294-23090 SI-020350 7 14.00 14.00 2.45
1160-BALSHEET Shenzhen Oska Measurement Systems E8UPGRADEMISC SC-030585 1 1619.8199 -1619.8199 0.00
1160-BALSHEET Shenzhen Oska Measurement Systems E8UPGRADEMISC SC-030593 1 1619.8199 -1619.8199 0.00
1160-BALSHEET Shenzhen Oska Measurement Systems E8UPGRADEMISC SI-030591 1 1619.8199 1619.8199 0.00
2210-BALSHEET ISIS Precision Engineering Ltd. SR000000 SI-029899 2 0.00 0.00 0.00
2210-BALSHEET ISIS Precision Engineering Ltd. SR000000 SI-029899 3 0.00 0.00 0.00
2210-BALSHEET Spire Machining Ltd. MS0003IN SI-030247 1 295.00 295.00 51.63
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45520 Visits: 39948
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
malcolm.garbett
malcolm.garbett
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 92
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. :-) )
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45520 Visits: 39948
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search