October 16, 2007 at 5:37 am
Hi All,
How can you convert a positive number into a negative. I have tried *-1 but this doesn't seem to work.
Thanks
October 16, 2007 at 5:44 am
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
October 16, 2007 at 7:09 am
* -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.
October 16, 2007 at 7:17 am
David Burrows (10/16/2007)
* -1 should workwhat 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. :hehe:
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[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgOctober 16, 2007 at 7:39 am
and if you have doubts, just put it between brackets.
SELECT @myvariable,@myvariable *(-1)
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- 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
October 16, 2007 at 7:54 am
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
October 16, 2007 at 8:07 am
indeed.
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- 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
October 18, 2007 at 8:27 am
Ramesh!
Your answer is excellent!
Hats off!!!
Ram
October 18, 2007 at 8:49 am
October 18, 2007 at 9:26 am
Ram,
Its just the basic stuff, but anyhow thanks for appreciating....:):)
Adam,
Where R U?:hehe: Have you got what you wanted or you've found some other way around?
--Ramesh
October 19, 2007 at 1:29 pm
Ramesh (10/16/2007)
DECLARE @InitialNumber INTDECLARE @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....
is there any advantage doing it that way verses
DECLARE @InitialNumber INT
DECLARE @OutputNumber INT
SET @InitialNumber = 9
SET @OutputNumber = -abs(@InitialNumber)
SELECT @InitialNumber AS Initial, @OutputNumber AS Final
the abs gives the absolute value of a number, which is ALWAYS positive, so then you just multiply it by -1 the execution times are almost exactly the same(7376 ms case vs 7200 ms abs) with a run of 100000 times each
October 19, 2007 at 1:56 pm
D'oh!
Here I was all smug 😎 about: -1*sign(@a)*@a
when yours: (-1)*abs(@a)
is even more elegant. 🙂
Kindest Regards,
--SF
(SELECT COALESCE(Phlogiston,Caloric,DarkMatter) as Construct FROM Science.dbo.Theory)
October 19, 2007 at 2:47 pm
Keep in mind that many of these solutions handle negative numbers differently, so until the OP comes back to comment, there's no telling which method will work for him. He might not have any negative numbers to begin with, but if he does, then we definitely need to know whether his return values should all be negative, or just toggle the sign.
October 21, 2007 at 5:25 am
David Easley (10/19/2007)
Ramesh (10/16/2007)
DECLARE @InitialNumber INTDECLARE @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....
is there any advantage doing it that way verses
DECLARE @InitialNumber INT
DECLARE @OutputNumber INT
SET @InitialNumber = 9
SET @OutputNumber = -abs(@InitialNumber)
SELECT @InitialNumber AS Initial, @OutputNumber AS Final
the abs gives the absolute value of a number, which is ALWAYS positive, so then you just multiply it by -1 the execution times are almost exactly the same(7376 ms case vs 7200 ms abs) with a run of 100000 times each
The only difference i can think of is that it looks simple to understand on first look itself...:hehe::hehe:
--Ramesh
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply