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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"
"In SQLSERVER the ORG_UNIT_CD has a datatype of varchar"
You're right Scott.
in this case, your solution is 100% correct.
in this case, your solution is 100% correct.
ASKER
Thanks Guys ! I am giving the points to Scott, your solution worked for me.
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_uni