Monday, March 19, 2012

Formatting a field to spec in SQL

Can anyone tell me how to format a field to spec?

Example:

Select num_field from table = 12345

I want

Select Format(num_field,"0000000000") from table = 0000012345

or some equivalent. I know in Access you could do this using the Format function, but I cannot seem to find anything like this in SQL.

Thanks in advance,

Michael

Hi,

there is no format function in SQL Server, you will have to do it this way here:

LEFT('0000000000' + CONVERT(VARCHAR(10),num_field), 10)

That will give you the appropiate format.

HTH; Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

Hi Jen,

Thanks for the response, it works with one small exception, I need to use the RIGHT function not the LEFT. But all is good, and it works...THANKS!

Michael

|||You are sure right, my mistake :-)

No comments:

Post a Comment