Sybase arithmetic overflow with run script to check database data and log usage

I run a script below which always return error as below

SELECT "Database Name" = CONVERT(char(20), db_name(D.dbid)),
"Data Size" =
WHEN U.segmap != 4 THEN U.size*(@@maxpagesize/1048576)
"Used Data" =
WHEN U.segmap != 4
THEN size -
curunreservedpgs(U.dbid, U.lstart, U.unreservedpgs)
"Data Full%" =
STR(100 *
(1 - 1.0 *
WHEN U.segmap != 4
THEN curunreservedpgs(U.dbid, U.lstart, U.unreservedpgs)
WHEN U.segmap != 4 THEN U.size
,9,1) + "%",
"Log Size" =
WHEN U.segmap in (4, 7) THEN U.size*@@maxpagesize/1048576
"Free Log" =
"Log Full%" =
STR(100 * (1 - 1.0 * lct_admin("logsegment_freepages",D.dbid)
/ SUM(CASE WHEN U.segmap in (4, 7) THEN U.size END)),8,1) + "%"
FROM master..sysdatabases D,
master..sysusages    U
WHERE U.dbid = D.dbid
AND (((D.dbid > 3) AND (D.dbid < 31513) OR D.dbid = 2)  AND (D.status != 256))
ORDER BY db_name(D.dbid)

Msg 3606, Level 16, State 4:
Server 'Sybase_test', Line 1:
Arithmetic overflow occurred.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joe WoodhousePrincipal ConsultantCommented:
Think this is your problem:

     WHEN U.segmap != 4 THEN U.size*(@@maxpagesize/1048576)

Notice that every other time you do a size calculation, you don't have those parentheses. Try replacing it with:

     WHEN U.segmap != 4 THEN U.size*@@maxpagesize/1048576

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Sybase Database

From novice to tech pro — start learning today.