corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Saturdays SQL Server Task


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Saturdays SQL Server Task
kazazza
Member

Registered: 5th Oct 09
Location: Herne Bay, Kent
User status: Offline
3rd Nov 12 at 12:31   View User's Profile U2U Member Reply With Quote

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
3rd Nov 12 at 13:06   View User's Profile U2U Member Reply With Quote

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
3rd Nov 12 at 13:09   View User's Profile U2U Member Reply With Quote

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

Avatar

Registered: 22nd Nov 06
Location: West Yorkshire
User status: Offline
4th Nov 12 at 12:10   View Garage View User's Profile U2U Member Reply With Quote

Alll by myself, don't wanna be, all by myself, anymore.


Russ
Member

Registered: 14th Mar 04
Location: Armchair
User status: Offline
4th Nov 12 at 13:46   View User's Profile U2U Member Reply With Quote

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

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
SQL Server Bart Geek Day 1 849
1st Feb 07 at 16:44
by Samls
 
SQL Server Bart Geek Day 3 1080
14th Mar 07 at 22:58
by James
 
SQL server Dan Lewis Geek Day 1 299
30th Jan 08 at 17:15
by Dan Lewis
 
System Center Configuration Manager 2007 Pablo Geek Day 13 361
6th Feb 08 at 14:39
by Pablo
 
MySQL Connection Problem Doug Geek Day 16 1136
30th Mar 08 at 19:22
by Doug
 

Corsa Sport » Message Board » Off Day » Geek Day » Saturdays SQL Server Task 29 database queries in 0.0111911 seconds