We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Floor and Ceil function

lysenko
lysenko asked
on
Medium Priority
1,255 Views
Last Modified: 2013-11-05
How to do this query in field like this:

Age
-----
6
17
29
33
46
50
76
81
90

When I enter parameter n=48, it should return:

Age
-----
46
50

where 46 is biggest integer smaller than or equal to n, and 50 is smallest integer greater than or equal to n

Thanks
Comment
Watch Question

Commented:
Hi,

How about this:

select t1.Age from table t1 where t1.Age = (select max(t2.Age) from table t2 where t2.Age<@n) or t1.Age = (select min(t3.Age) from table t3 where t3.Age>@n) order by t1.Age

/peter

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Chief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
Try like this:


/* section used for testing purposes */
DECLARE @tbl TABLE(Age INT)
INSERT INTO @tbl
SELECT 6 
UNION SELECT 17
UNION SELECT 29
UNION SELECT 33
UNION SELECT 46
UNION SELECT 50
UNION SELECT 76
UNION SELECT 81
UNION SELECT 90
 
DECLARE @n INT
SET @n = 48
/* end of testing setup */
 
-- using union
select max(age) As Age from @tbl where age < @n
union all
select min(age) As Age from @tbl where age > @n;

Open in new window

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
And I see in the tags you have Access, so my example which is based on SQL Server 2005 may not work but the concept of the query can be done in MS Access like this:


select max(age) As Age 
from your_table_name 
where age < [n]
union
select min(age) 
from your_table_name 
where age > [n];

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.