Click to See Complete Forum and Search --> : Format text


srinika
September 16th, 2005, 04:12 PM
Hi All,

I'm using MS SQL Server 2000
I need to get the results of a select statement after formatting it.

eg :
if the results of the following query is '1234' I need to see the results as AB00001234
if the results of the following query is '567' I need to see the results as AB00000567

Query : Select StundentID from Student

In other words, I need to know whether there is something like Format function in VB, so that the hypothetical Query would be

Select 'AB' + Format(StundentID, "0000000#") from Student

Thanks in advance
Srinika

olivthill
September 16th, 2005, 04:52 PM
SQL Server does not have a "Format" statement.
Conversions are done with CAST and CONVERT. Used in conjunction with REPLICATE, you can have:
Select 'AB' + REPLICATE('0', 7 - DATALENGTH(CAST(StudentID AS VARCHAR(7)))
from Student
For more information, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ea-ez_4aur.asp

srinika
September 20th, 2005, 03:41 PM
Thanks

It helped me 100%