Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 problem when converting bigint into nvarchar

Author  Topic 

marek_gd
Starting Member

6 Posts

Posted - 2014-12-04 : 10:08:49
Hi guys.

I have a problem when I am converting bigint number into nvarchar.

I want to generate a random bigint number between 1000000000000000 and 1900000000000000. Then I want to convert into nvarchar using cast or convert functions. Unfortunately none of these functions gives me a desired output.

When I run the following statement:

declare @acct as bigint
select @acct =ROUND(((1000000000000000 - 1900000000000000 -1) * RAND() + 1900000000000000), 0)
print @acct

my output looks like: 1898492990346528 and that is fine.

But when I want to cast it into nvarchar(16) using:

declare @acct as nvarchar (16)
select @acct =cast(ROUND(((1000000000000000 - 1900000000000000 -1) * RAND() + 1900000000000000), 0) as nvarchar(16))
print @acct

my output looks like: 1.29673e+015.
I want to have my nvarchar output looks like: '1898492990346528' . I want to get rid of dot and 'e' notation.

I tried also:
SELECT CONVERT(nvarchar(16),ROUND(((1000000000000000 - 1900000000000000 -1) * RAND() + 1900000000000000), 0)) but also had the same issue.

Please help.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-04 : 10:39:31
cast( cast(((1000000000000000 - 1900000000000000 -1) * RAND() + 1900000000000000) as bigint) as nvarchar(16))

recall that RAND() returns a float and SQL will convert the "+ 1900000000000000" to a float to match. You need to force it to biging
Go to Top of Page
   

- Advertisement -