SQL Server - Converting Varchar to bigint and again bigint to varchar

Asked By Namdev Patil on 23-Apr-10 02:56 AM
Hi All, My requirement is that, i want convert my 5 character varchar type number into bigint and after processing again i want to convert it into 5 digit varchar. So please give me your advice to tackle with this requirement. Thanks in advance
Firoz replied to Namdev Patil on 23-Apr-10 03:06 AM
Hi Namdev, YOu know how to convert a varchar to big int then you ca try assign a varchar value in one variable and again use this varable for varchar where do you need. Hope this will help you. THanks.
Ramesh Swaminathan replied to Namdev Patil on 23-Apr-10 03:41 AM

You can use CONVERT function to convert a value from one type to another type in SQL Server. Try the following code.

SET @str1 = '12345'
-- To ocnver from VARCHAR to BIGINT
SET @bignum1= CONVERT(BIGINT, @str1)
--To covert again from  BIGINT to VARCHAR
SET @str1= CONVERT(VARCHAR(20), @bignum1)

Anoop S replied to Namdev Patil on 23-Apr-10 03:47 AM
try this one-> Varchar to bigint,

declare @big_int as nvarchar(max)
declare @chr as nvarchar(max)
set @big_int='10'
set @chr = 'Select Top '+@big_int+' fieldname from tablename'
select @big_int
Select @chr
exec (@chr)

you can do the reverse by changing the variable
Santhosh N replied to Namdev Patil on 23-Apr-10 04:39 AM
If you wanted this in sql server, you can use either cast or convert functions to accomplish this