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»»

Convert positive number to a negative Expand / Collapse
Author
Message
Posted Tuesday, October 16, 2007 5:37 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, April 29, 2014 6:47 AM
Points: 319, Visits: 783
Hi All,

How can you convert a positive number into a negative. I have tried *-1 but this doesn't seem to work.

Thanks
Post #411207
Posted Tuesday, October 16, 2007 5:44 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 16, 2014 5:52 AM
Points: 2,551, Visits: 2,594
DECLARE @InitialNumber INT
DECLARE @OutputNumber INT

SET @InitialNumber = 9
SET @OutputNumber = ( CASE WHEN @InitialNumber > 0 THEN -@InitialNumber ELSE @InitialNumber END )

SELECT @InitialNumber AS Initial, @OutputNumber AS Final


Works like a charm....


--Ramesh

Post #411212
Posted Tuesday, October 16, 2007 7:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:01 AM
Points: 6,954, Visits: 7,077
* -1 should work

what is the datatype and what makes you think it did not work

btw

preceding a column or variable with - will achieve the same, eg

DECLARE @myvariable int
SET @myvariable = 1
SELECT @myvariable,-@myvariable



Far away is close at hand in the images of elsewhere.

Anon.

Post #411267
Posted Tuesday, October 16, 2007 7:17 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, October 5, 2014 10:21 AM
Points: 2,717, Visits: 3,855
David Burrows (10/16/2007)
* -1 should work

what is the datatype and what makes you think it did not work

btw

preceding a column or variable with - will achieve the same, eg

DECLARE @myvariable int
SET @myvariable = 1
SELECT @myvariable,-@myvariable


I think David means ...

DECLARE @myvariable int
SET @myvariable = 1
SELECT @myvariable,@myvariable * -1


But BOTH work. It must be a another issue, but I don't think it's a dat-type issue.


EDIT: Sorry Guys, I replied WAY to quick, and mis-read David's post


______________________________________________________________________

Personal Motto: Why push the envelope when you can just open it?

If you follow the direction given HERE you'll likely increase the number and quality of responses you get to your question.

Jason L. Selburg
Post #411272
Posted Tuesday, October 16, 2007 7:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:35 AM
Points: 6,733, Visits: 8,485
and if you have doubts, just put it between brackets.

SELECT @myvariable,@myvariable *(-1)


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #411287
Posted Tuesday, October 16, 2007 7:54 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, April 14, 2014 6:47 AM
Points: 967, Visits: 450
I don't think braces would really matter. It should work even without braces. But as a safety you can try it with braces.

Prasad Bhogadi
www.inforaise.com
Post #411302
Posted Tuesday, October 16, 2007 8:07 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:35 AM
Points: 6,733, Visits: 8,485
indeed.

Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #411309
Posted Wednesday, October 17, 2007 9:40 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, January 6, 2012 2:39 PM
Points: 954, Visits: 683
DECLARE @a INT
SELECT @a = 1

SELECT @a = -@a

SELECT @a

SELECT @a=-@a

SELECT @a

Note that @a*-1 doesn't change it unless you set it to itself.. Is that why you thought it didn't work?
Post #411804
Posted Thursday, October 18, 2007 8:27 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, March 11, 2013 12:32 PM
Points: 86, Visits: 64
Ramesh!

Your answer is excellent!

Hats off!!!

Ram
Post #412239
Posted Thursday, October 18, 2007 8:49 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, December 19, 2012 8:27 AM
Points: 359, Visits: 350
Here's another way to always endup with a negative number (this works whether you start with a positive or a negative number):

DECLARE @a INT
SET @a = 1
SET @a = (-1)*sign(@a)*@a
Select @a



Kindest Regards,

--SF

(SELECT COALESCE(Phlogiston,Caloric,DarkMatter) as Construct FROM Science.dbo.Theory)
Post #412257
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse