Link to home
Start Free TrialLog in
Avatar of SquareOne
SquareOne

asked on

Replacing the ACCESS function 'Format' with SQL server equivalent

I am porting some Access sql to SQL Server. I am running into problems with code of the format “FORMAT(ORG_UNIT_CD,'0000') as orgunitcd” which is in the Select clause.

In SQLSERVER the ORG_UNIT_CD has a datatype of varchar and length 4.

In Access the datatype is Text and FieldSize is 4.

Can somebody suggest the equivalent for it?

Thanks,
Square One
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wdalton
wdalton

basically the solution of ScottPletcher is correct, but just 1 remark:
it will probably return an error if org_unit_id is a numeric datatype
So the correct solution would be : SELECT RIGHT('0000' + convert(varchar(4),org_unit_cd), 4)

Yes, it would return an error if it were INT/NUMERIC, but the original post stated this:

"In SQLSERVER the ORG_UNIT_CD has a datatype of varchar"
You're right Scott.
in this case, your solution is 100% correct.
Avatar of SquareOne

ASKER

Thanks Guys ! I am giving the points to Scott, your solution worked for me.