Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Add minus symbol in sql query Expand / Collapse
Author
Message
Posted Friday, November 2, 2012 2:48 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 7:58 AM
Points: 29, Visits: 81
As SQL is not my main job what might seem easy to many here has me stumped.

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?
Post #1380594
Posted Friday, November 2, 2012 2:57 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 11:44 AM
Points: 6,236, Visits: 7,380
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1380596
Posted Friday, November 2, 2012 3:03 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 10:33 AM
Points: 273, Visits: 851
If you want a negative value, then just multiply the output column by -1
e.g. select col_name * (-1)
Post #1380598
Posted Saturday, November 3, 2012 8:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 7:58 AM
Points: 29, Visits: 81
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



Post #1380712
Posted Saturday, November 3, 2012 8:58 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 5:24 AM
Points: 128, Visits: 490
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



Post #1380713
Posted Saturday, November 3, 2012 9:58 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:56 PM
Points: 36,958, Visits: 31,466
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1380714
Posted Monday, November 5, 2012 6:53 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 7:58 AM
Points: 29, Visits: 81
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
Post #1381056
Posted Monday, November 5, 2012 6:57 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:56 PM
Points: 36,958, Visits: 31,466
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1381059
Posted Monday, November 5, 2012 10:07 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 7:58 AM
Points: 29, Visits: 81
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. )
Post #1381186
Posted Monday, November 5, 2012 10:22 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:56 PM
Points: 36,958, Visits: 31,466
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1381195
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse