Default Value On nVarchar Column

  • 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.

  • 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


  • 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