Avatar of Direct One
Direct One
Flag for United States of America asked on

MS SQL Statement to make amount field all Zero's when the field is null

I have this statement to pad amounts with zero's and change the dollar amount from negative to positive.

When a check is voided, it is a null amount for the dist_amt.  How do we populate just zero's when the amount is null included in this statement?  Also, if there is a better way to write this entire statement, please advise as well.  Thanks in advance.

RIGHT ('000000000000' + CONVERT (varchar, REPLACE(CONVERT (varchar, dbo.APDISFIL_SQL.dist_amt), '.', '') * - 1), 12)
Microsoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
Lowfatspread

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jorgedeoliveiraborges

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
HainKurt

there is an issue with code above...
I am assuming dbo.APDISFIL_SQL.dist_amt is float or some other numeric format
then why you are converting it to char and multiply by -1?

maybe you want this

RIGHT ('000000000000' + REPLACE(CONVERT (varchar, abs(isnull(dbo.APDISFIL_SQL.dist_amt,0))), '.', ''), 12)

first, set to 0 if it is null, then get abs value(remove negative), then convert it to varchar, replace ., pad with 0 to make it 12 char...

HainKurt

maybe my post is not correct, what is your data type for dbo.APDISFIL_SQL.dist_amt
give some samples, positive, negative, null etc...
Direct One

ASKER
worked great. thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Lowfatspread

more like

RIGHT ('000000000000' + REPLACE(CONVERT(varchar(13), coalesce(-(dbo.APDISFIL_SQL.dist_amt),0)), '.', ''), 12)

Open in new window