hello. A question please. I am new on Sqlserver. Could anyone say me how must I format an integer to string. example
I have:
0001 (integer field of 4 positions) and I'd want to see it "0001" (string)
Thanks...
It seems like there is a better way of doing this, but one way is something like:
select right('000'+convert(varchar(4), 1), 4) as [4-digit integer]
-- 4-digit integer
--
-- 0001
|||
Thanks. The problem is that I gave an example for the first field. (0001). But It could be 0010...
|||
What am I missing?
select right('000'+convert(varchar(4), 10), 4) as [4-digit integer]
-- 4-digit integer
--
-- 0010
|||
Waldrop wrote: |
|
What am I missing? select right('000'+convert(varchar(4), 10), 4) as [4-digit integer] -- 4-digit integer -- -- 0010
|
Nothing. That works fine.
SELECT right('0000' + cast([your_column] as varchar(4)),4)
FROM your_table
|||
I'm NEW !!!! Sorry !!!!. Another question please. I must put your statement in a string var. How can I do the '000' in my string var ?
Thanks...
|||
declare @.myString varchar(4)
set @.myString = right('000'+convert(varchar(4), 7), 4)
set @.myString = right('0000' + cast([your_column] as varchar(4)),4)
set @.myString = '0007'
|||
I'm so sorry. I don't understand. I forgot to say that the statement goes in an store procedure. My problem is how do I put the (') in a string var.
Thanks...
|||
An example of adding quote characters into the string variable:
declare @.myString varchar(6)
declare @.quoteChar char(1)
set @.myString = '''0007''' -- Loads '0007' (including quotes) into @.myString variable
set @.quoteChar = char(39) -- Loads a quote char ' into @.quoteChar variable
set @.quoteChar = '''' -- Loads a quote char ' into @.quoteChar variable
select @.myString as [@.myString],
@.quoteChar as [@.quoteChar]
-- - Output: -
-- @.myString @.quoteChar
-- -
-- '0007' '
declare @.anotherString varchar(4)
set @.anotherString = '0007' -- Loads '0007' (without quotes) into @.anotherString variable
set @.myString = char(39) + @.anotherString + '''' -- concatenates quote characters around '0007'
select @.anotherString as [@.anotherString],
@.myString as [@.myString]
-- - Output: -
-- @.anotherString @.myString
-- --
-- 0007 '0007'
|||Thanks. It works !!!!!