kazazza
Member
Registered: 5th Oct 09
Location: Herne Bay, Kent
User status: Offline
|
Hey guys,
I recently started learning a bit of SQL for work, so today I thought I'd try something new.
I want to show 2 columns and only one row
In the first column I want there to be a random number between say 1 and 500. I can do this like this:
DECLARE @random int
SET @random = cast(rand() * 500 AS int)
select @random as number
In the second column I want it to say HIGH if the random number is >= 250 else LOW. How might one go about doing this? I'm guessing a CASE statement? Im using Server 2012 btw.
Much appreciation to anyone who might be able to help
|
kazazza
Member
Registered: 5th Oct 09
Location: Herne Bay, Kent
User status: Offline
|
Ok, almost done it, just need to eliminate one of the selects:
DECLARE @random int
DECLARE @true varchar(10)
DECLARE @false varchar(10)
SET @random = cast(rand() * 1000000 AS int)
SET @true = 'True'
SET @false = 'False'
select @random
select
case
when @random >= '500000' then @true
else @false
end
|
kazazza
Member
Registered: 5th Oct 09
Location: Herne Bay, Kent
User status: Offline
|
Ok, dont worry guys, done it - I really shouldnt have asked, it was easier than I though it would be:
DECLARE @random int
DECLARE @high varchar(10)
DECLARE @low varchar(10)
SET @random = cast(rand() * 1000000 AS int)
SET @high = 'High'
SET @low = 'Low'
SELECT @random,
CASE
WHEN @random >= '500000' THEN @high
ELSE @low
END
|
Gary
Premium Member
Registered: 22nd Nov 06
Location: West Yorkshire
User status: Offline
|
Alll by myself, don't wanna be, all by myself, anymore.
|
Russ
Member
Registered: 14th Mar 04
Location: Armchair
User status: Offline
|
DECLARE @random int
DECLARE @high varchar(10)
DECLARE @low varchar(10)
SET @random = cast(rand() * 1000000 AS int)
SET @high = 'High'
SET @low = 'Low'
SELECT @random,
CASE
WHEN @random >= '500000' THEN @high
ELSE @low
END
Should do the trick
|