February 6, 2009 at 9:06 am
Hi,
I'm trying to add a default value to a nVarchar column 'nvarchar(50)' that generates a random string of 5 numbers using the following select statement at a default value:
SELECT Convert(nvarchar(5),ROUND(RAND()*100000,0))
I've tried using that in the Default Value Or Bindings property and it errors when I try to add rows?
Any help greatly appreciated.
February 6, 2009 at 9:35 am
You just need to write the expression (like CONVERT( NVARCHAR(5), ROUND(RAND()*100000,0)) ) & not the entire statement. And also, this will only work if you are inserting one record at a time. To overcome this, you need to use NEWID() function
like this one....
REPLACE( STR( ABS( CHECKSUM( NEWID() ) ) % 10000, 5 ), ' ', '1' )
--Ramesh
February 6, 2009 at 10:09 am
Thanks. I'm only planning on performing one record at a time but I appreciate the tip. I've tried your suggestion and it works. Thanks again.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply