Discussion Locked

"Format" a string in SQL Server query?

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

This is simple query from MS Access, how do i convert it to SQL Server 7.0 query? apparently SQL Server doesn't support the function called Format.

SELECT Format(myNo, '000000') as myNo From myTable

i've managed to come out with something which perform the same task in SQL Server, it looks like:

SELECT CONVERT(varchar(6), REPLICATE('0', 6 - LEN(myNo))) + CONVERT(varchar(6), myNo) AS myNo FROM myTable

is this the correct way of doing so?

btw, can i create a user function SQL Server? i would like to make it a general function so that i can use it in other tables.

thanx

5 replies

by ohjani

"myNo" is actually one of the columns from myTable. myNo is supposed to obtain from the database recordset.

by olivier_de-polignac

Here is the SQL statement you should use:

SELECT RIGHT(REPLICATE('0', 6) + CAST(myNo AS VARCHAR(6)), 6) FROM myTable

This will correctly format your number.

Regarding the User Function, only SQL Server 2000 has that feature, not SQL Server 7.0.

Hope this help.

Regards,

Olivier

This discussion is closed to new replies.