Equivalent to an Excel function

Hello guys,

I am somewhat new to SQL. I am trying to recreate a simple excel function is SQL

The function is:
=text(cell,"000000000")

All it does is add zeros to the front of the number. I know I can concatenate and put it there but this is sort of like a format.....I would like to make SQL do the same thing...

I know this is super simple for you guys but like I said....I'm green...sorry

Thanks

Without knowing your field one way is if the field is string:
SELECT RIGHT('000'+ISNULL(field,''),3)

or
SELECT REPLACE(STR(n,3),' ','0')

1 Like
CREATE FUNCTION dbo.Lpad_Zeros
(
    @value varchar(100)
)
RETURNS varchar(100)
AS
BEGIN
RETURN (
    SELECT RIGHT('000000000' + @value, 9)
)
END
1 Like

Wow you guys are awesome...I'm hope to be as good....Thanks!

Scott is the master here! :slight_smile:

1 Like