Showing posts with label numbers. Show all posts
Showing posts with label numbers. Show all posts

Friday, February 10, 2012

build random phone number.

I'm building a test database and I need to randomly create phone numbers in the format xxxxxxxxxx

I have 5000 contacts and need to generate fake phone numbers for them. anyone know how I can write an update query to do this?

ScAndalThe way I can think to do this is:

1. create a View that will return a random number (this is needed because the RAND() function cannot be called from another function (the one we are creating in step 2 below))


CREATE VIEW dbo.vRandNumber
AS
SELECT RAND() AS RandomNumber

2. write a GenerateRandomNumber UDF:

CREATE FUNCTION GenerateRandomNumber(@.Min int, @.Max int)
RETURNS float
AS
BEGIN
RETURN @.Min + (select RandomNumber from vRandNumber) * (@.Max-@.Min)
END

3. write a GenerateRandomPhoneNumber UDF:

CREATE FUNCTION dbo.GenerateRandomPhoneNumber ()
RETURNS varchar(10) AS
BEGIN

DECLARE @.myNumber int, @.myPhoneNumber varchar(9), @.X smallint

SELECT @.X = 0, @.myPhoneNumber =''
WHILE @.X < 10
BEGIN
SELECT @.myNumber=dbo.generaterandomnumber(0,9)
SELECT @.myPhoneNumber = @.myPhoneNumber + CAST(@.myNumber AS CHAR(1))
SELECT @.X = @.X + 1
END

RETURN @.myPhoneNumber

END


4. populate the fake phone numbers like this:

UPDATE contacts SET PhoneNumber = GenerateRandomPhoneNumber()

This is just my first thought. Someone else might come along and do it in 2 steps ;-)
Terri|||I can do it in 1 step! Assuming that table has a identity column you can use it as a seed for the rand() function:


update Contact
set PhoneNumber = cast(cast(rand(ContactID * 12345)*90000 as int) + 10000 as varchar)
+ cast(cast(rand(ContactID * 54321)*90000 as int) + 10000 as varchar)

I had to do 5 digits at a time to avoid the int size problem, and I had to multiply the identity column with a largeish number to make the phone numbers look random.

For some reason
rand(X) - rand(X + 1) = -1.8633e-005
for all values of X

Just noticed it. Seems wrong to me. Ah well. Just don't use this to randomize lotto numbers and you'll be fine.|||Here is a variation of Terri's:

--create a view to expose NEWID() for randomness
create view dbo.vwRandomNumGenerator
as
select top 10 n
from
(
select 0 n union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 9 union all
select 9
) d
order by newid()
go

--create udf
create function dbo.udfPhoneNumberGenerator ()
returns char(10)
AS
begin
declare @.phoneNumber char(10)
set @.phoneNumber = ''

Select @.phoneNumber = n + @.phoneNumber
from
(
select convert(char(1),n) n
from dbo.vwRandomNumGenerator

) d
return @.phoneNumber
end
go

--call our rnd phone generator inline
select dbo.udfPhoneNumberGenerator() RandomPhoneNumber

--drop view vwRandomNumGenerator
--drop function udfPhoneNumberGenerator

|||Thanks guys! That worked perfectly.

Great advice!

ScAndal

Build numbers

I am trying to restore a master database after a server rebuild and need to
get the build number back to 8.00.818. I have installed (or so I thought) t
he SP3a and the 8-11-03 patch to get it to that build number but when I chec
k the build number in Enter
prise Manager it still shows 8.00.194. Help?How are you getting the build number from Enterprise Manager? You should
connect to your SQL Server using Query Analyzer and run the following
command to get the SQL Server's build number:
SELECT @.@.VERSION
GO
May be you haven't installed the service pack on the client machines.
This might be helpful: http://vyaskn.tripod.com/sqlsps.htm
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Chad" <anonymous@.discussions.microsoft.com> wrote in message
news:819D85D5-070C-4150-87FB-B9A0C5A2B2FB@.microsoft.com...
I am trying to restore a master database after a server rebuild and need to
get the build number back to 8.00.818. I have installed (or so I thought)
the SP3a and the 8-11-03 patch to get it to that build number but when I
check the build number in Enterprise Manager it still shows 8.00.194. Help?|||I have done that as well to get the build number but under properties of the
sql instance name is where I was getting the information from Enterprise Ma
nager (is that wrong?). And after applying SP3a and the 8-11-03 patch it ha
sn't changed the build info
rmation. I have taken the defaults for installation of the SP and patch. I
Have completely rebuilt the machine and reinstalled SQL so there souldn't b
e anything strange with the installation (all default).|||I wouldn't use Enterprise Manager for this. As I posted earlier, query the
server directly. What do you get when you run SELECT @.@.VERSION?
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Chad" <anonymous@.discussions.microsoft.com> wrote in message
news:D18A75A7-4AC6-4917-9334-F056D3E0159F@.microsoft.com...
I have done that as well to get the build number but under properties of the
sql instance name is where I was getting the information from Enterprise
Manager (is that wrong?). And after applying SP3a and the 8-11-03 patch it
hasn't changed the build information. I have taken the defaults for
installation of the SP and patch. I Have completely rebuilt the machine and
reinstalled SQL so there souldn't be anything strange with the installation
(all default).|||I get the original build number of the system (8.00.194). The patches don't
seem to be applying or at least don't seem to be affecting the number at al
l and so the restore of the Master Database failes.|||Can't be right :-) I did this on various servers and I see updated build
numbers. You either applied the service pack to the wrong server, or you
haven't applied the service pack at all.
When you run the service pack exe first it extracts itself into a folder.
Many people mistake that and think they've applied the service pack, just by
extracting the service pack. Hope that's not the case with you. If it is,
you have to run the setup from the extracted folder.
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Chad" <anonymous@.discussions.microsoft.com> wrote in message
news:E97EEE10-091F-4DD8-99EB-B9513B38F1B5@.microsoft.com...
I get the original build number of the system (8.00.194). The patches don't
seem to be applying or at least don't seem to be affecting the number at all
and so the restore of the Master Database failes.|||Narayana Vyas Kondreddi wrote:

>Can't be right :-) I did this on various servers and I see updated build
>numbers. You either applied the service pack to the wrong server, or you
>haven't applied the service pack at all.
>When you run the service pack exe first it extracts itself into a folder.
>Many people mistake that and think they've applied the service pack, just b
y
>extracting the service pack. Hope that's not the case with you. If it is,
>you have to run the setup from the extracted folder.
>
Vyas,
Whether or not this is what happened, it never hurts to remind people
how easy it is not to install the update:
How *not* to install sp3a for Microsoft SQL Server 2000
1. Download the service pack file, SQL2KSP3.exe, from Microsoft
2. Run the service pack file SQL2KSP3.exe
3. Click OK when you see the message "the package has been successfully
delivered"
Guess what? The service pack has not been installed! The download page
says you just need to "Install Database Components SP3a (SQL2KSP3.exe),"
so what's going on?
Youi wouldn't know it from the download page, but you now have to go to
the location where you extracted the files and run setup (and if I
recall, there are many files containing the word setup).
SK

>--
>HTH,
>Vyas, MVP (SQL Server)
>http://vyaskn.tripod.com/
>Is .NET important for a database professional?
>http://vyaskn.tripod.com/poll.htm
>
>
>"Chad" <anonymous@.discussions.microsoft.com> wrote in message
>news:E97EEE10-091F-4DD8-99EB-B9513B38F1B5@.microsoft.com...
>I get the original build number of the system (8.00.194). The patches don'
t
>seem to be applying or at least don't seem to be affecting the number at al
l
>and so the restore of the Master Database failes.
>
>

Build numbers

I am trying to restore a master database after a server rebuild and need to get the build number back to 8.00.818. I have installed (or so I thought) the SP3a and the 8-11-03 patch to get it to that build number but when I check the build number in Enterprise Manager it still shows 8.00.194. Help?How are you getting the build number from Enterprise Manager? You should
connect to your SQL Server using Query Analyzer and run the following
command to get the SQL Server's build number:
SELECT @.@.VERSION
GO
May be you haven't installed the service pack on the client machines.
This might be helpful: http://vyaskn.tripod.com/sqlsps.htm
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Chad" <anonymous@.discussions.microsoft.com> wrote in message
news:819D85D5-070C-4150-87FB-B9A0C5A2B2FB@.microsoft.com...
I am trying to restore a master database after a server rebuild and need to
get the build number back to 8.00.818. I have installed (or so I thought)
the SP3a and the 8-11-03 patch to get it to that build number but when I
check the build number in Enterprise Manager it still shows 8.00.194. Help?|||I have done that as well to get the build number but under properties of the sql instance name is where I was getting the information from Enterprise Manager (is that wrong?). And after applying SP3a and the 8-11-03 patch it hasn't changed the build information. I have taken the defaults for installation of the SP and patch. I Have completely rebuilt the machine and reinstalled SQL so there souldn't be anything strange with the installation (all default).|||I wouldn't use Enterprise Manager for this. As I posted earlier, query the
server directly. What do you get when you run SELECT @.@.VERSION?
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Chad" <anonymous@.discussions.microsoft.com> wrote in message
news:D18A75A7-4AC6-4917-9334-F056D3E0159F@.microsoft.com...
I have done that as well to get the build number but under properties of the
sql instance name is where I was getting the information from Enterprise
Manager (is that wrong?). And after applying SP3a and the 8-11-03 patch it
hasn't changed the build information. I have taken the defaults for
installation of the SP and patch. I Have completely rebuilt the machine and
reinstalled SQL so there souldn't be anything strange with the installation
(all default).|||I get the original build number of the system (8.00.194). The patches don't seem to be applying or at least don't seem to be affecting the number at all and so the restore of the Master Database failes.|||Can't be right :-) I did this on various servers and I see updated build
numbers. You either applied the service pack to the wrong server, or you
haven't applied the service pack at all.
When you run the service pack exe first it extracts itself into a folder.
Many people mistake that and think they've applied the service pack, just by
extracting the service pack. Hope that's not the case with you. If it is,
you have to run the setup from the extracted folder.
--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm
"Chad" <anonymous@.discussions.microsoft.com> wrote in message
news:E97EEE10-091F-4DD8-99EB-B9513B38F1B5@.microsoft.com...
I get the original build number of the system (8.00.194). The patches don't
seem to be applying or at least don't seem to be affecting the number at all
and so the restore of the Master Database failes.|||Narayana Vyas Kondreddi wrote:
>Can't be right :-) I did this on various servers and I see updated build
>numbers. You either applied the service pack to the wrong server, or you
>haven't applied the service pack at all.
>When you run the service pack exe first it extracts itself into a folder.
>Many people mistake that and think they've applied the service pack, just by
>extracting the service pack. Hope that's not the case with you. If it is,
>you have to run the setup from the extracted folder.
>
Vyas,
Whether or not this is what happened, it never hurts to remind people
how easy it is not to install the update:
How *not* to install sp3a for Microsoft SQL Server 2000
1. Download the service pack file, SQL2KSP3.exe, from Microsoft
2. Run the service pack file SQL2KSP3.exe
3. Click OK when you see the message "the package has been successfully
delivered"
Guess what? The service pack has not been installed! The download page
says you just need to "Install Database Components SP3a (SQL2KSP3.exe),"
so what's going on?
Youi wouldn't know it from the download page, but you now have to go to
the location where you extracted the files and run setup (and if I
recall, there are many files containing the word setup).
SK
>--
>HTH,
>Vyas, MVP (SQL Server)
>http://vyaskn.tripod.com/
>Is .NET important for a database professional?
>http://vyaskn.tripod.com/poll.htm
>
>
>"Chad" <anonymous@.discussions.microsoft.com> wrote in message
>news:E97EEE10-091F-4DD8-99EB-B9513B38F1B5@.microsoft.com...
>I get the original build number of the system (8.00.194). The patches don't
>seem to be applying or at least don't seem to be affecting the number at all
>and so the restore of the Master Database failes.
>
>