Sunday, February 19, 2012

format column data

Hi,

I am entering values from .Net app to a table in Sql 2k. The Identity field needs to be in 'ABC0000012' format (3 fixed letters and 7 digits - incremented by 1). My code MUST BE within a stored_proc. I am trying to format the column to have reqd. number of preceeding zeros. How can I get those ?

Pls help.
Sam

I am not sure exactly what you mean but I am going to take a guess.... Does this help?

Foo = "ABC" & SubStr(CStr(10000000 + id),2,7)

|||

Sara.. Thanks for guess.. here is what I am trying:

I have a constant prefix to which I need to apend value in the Identity column (should be 7 digits) -- so the total alpha numeric number will be 10 places:

examples:
say ID column value is 2 my final output should look like ABC0000002
say ID column value is 123 my final output should look like ABC0000123
say ID column value is 123456 my final output should look like ABC0123456

question is how to pad ID column value with preceeding zeroes?

Thanks, Sam

|||SELECT
'ABC' + right('0000000'+ convert(varchar,[id]), 7)
, othercolumns
FROM
yourTable

No comments:

Post a Comment