Showing posts with label to_char. Show all posts
Showing posts with label to_char. Show all posts

Thursday, February 9, 2012

alternative function for to_char in sql server

Hi,
Is there any alternative function for to_char in sql server 2005(T-SQL) that
converts numeric to character in the specified format?
to_char(5,'09')
will result in 05 (in oracle)
to_char(10,'09')
will result in 10 (in oracle)
ThanksJP
No, there isn't
DECLARE @.c AS INT
SET @.c=9
SELECT CASE WHEN LEN(@.c)=1 THEN '0'+ CAST(@.c AS VARCHAR(2))
ELSE CAST(@.c AS VARCHAR(2)) END
"JP" <JP@.discussions.microsoft.com> wrote in message
news:83C4046F-70C8-463B-B533-3D3C181D96DA@.microsoft.com...
> Hi,
> Is there any alternative function for to_char in sql server 2005(T-SQL)
> that
> converts numeric to character in the specified format?
> to_char(5,'09')
> will result in 05 (in oracle)
> to_char(10,'09')
> will result in 10 (in oracle)
> Thanks
>|||You dn't have a direct function.
Maybe you can use something like this
declare @.a int ,@.no_of_digits int
set @.a = 10 -- value
set @.no_of_digits = 2 -- No of digits you want to have in the final string
select right(replicate('0',@.no_of_digits) + cast(@.a as
varchar(30)),@.no_of_digits)
Hope this helps.
--
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/