Nearest multiple of 12

  • I know this could be fairly straightforward but i am sort of stuck on it.

    I am writing a query to find the nearest multiple of 12.

    I tried to use the below code:

    select round ((x+11)/12,0)*12

    select CEILING (x/12)*12

    but they dont give the desired result.

    My aim is

    x result

    13 12 to be returned

    23 24 to be returned

    15 12 to be returned

    Can some one help?

  • Hi,

    try this

    declare @abc int

    select @abc = 125

    select @abc = 12*(round((@ABC/12),0) + (case when (cast(@ABC as int)%12)< 6.00 then 0 else 1 end))

    -- assume that nearest to 6

    select @abc

    ARUN SAS

  • Assuming x is an int

    select ((x+6)/12)*12

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply