Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:49:57+00:00 2026-05-27T02:49:57+00:00

Possible Duplicate: Creating Nondeterministic functions in SQL Server using RAND() I need to generate

  • 0

Possible Duplicate:
Creating Nondeterministic functions in SQL Server using RAND()

I need to generate some random nvarchar in a function. So I create this function :

CREATE FUNCTION [dbo].[sfp_GenerateRandomString]    
    (@length int) -- Length of string   
RETURNS NVARCHAR
AS
BEGIN
DECLARE @random NVARCHAR(25) = ''

while @length > 0   
begin
    set @random = @random + nchar((cast(rand()*1000 as int)%26)+97)
    set @length = @length-1
end
return @random      
END

But it doesn’t work because the RAND() function is forbidden in SQL function who have to be DETERMINISTIC. And RAND() is NONDETERMINISTIC.

So my question is how to generate random number in this case?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:49:58+00:00Added an answer on May 27, 2026 at 2:49 am

    You could use a SQL CLR function if you really needed it in a function. Or if you only need it in one place, just put it in the stored procedure.

    Personally I would opt towards not doing this sort of work inside SQL Server at all, if it could be helped. This is application logic, not database logic. Although without knowing the back story, I don’t know what other constraints you may have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: How to execute sql statements from a C program? I'm creating a
Possible Duplicate: is there any other way of creating an object without using “new”
Possible Duplicate: Javascript: is using 'var' to declare variables optional? When creating variables in
Possible Duplicate: Creating, opening and printing a word file from C++ Hi, I need
Possible Duplicate: String vs StringBuilder Hi, I'm creating a json string. I have some
Possible Duplicate: Generating Random Numbers in Objective-C Hi I am creating an app where
Possible Duplicate: Prevent Direct Access To File Called By ajax Function I'm creating a
Possible Duplicate: T-SQL Pivot? Possibility of creating table columns from row values I've been
Possible Duplicate: How do you send email from a Java app using Gmail? How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.