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

Thursday, March 29, 2012

Get all unused numbers in range

What I am trying to do is get all of the records in a table that are out of
sequence so I know which account numbers I can reuse. I have a range
of account numbers from 50100 to 70100. I need to know which account
numbers are not stored in the table (not currently used) so I can use.

For instance say I have the following data in table:

Account Name
--- ---
50100 Test1
50105 Test2
50106 Test4
..
..
..
I should see the results:
50101
50102
50103
50104

because 50101-50104 are available account numbers since not currently in
table.

Thanks."Jason" <NO-SPAM-xyz@.msn.com> wrote in message news:525Ka.43802$hI1.7823@.nwrddc01.gnilink.net...
> What I am trying to do is get all of the records in a table that are out of
> sequence so I know which account numbers I can reuse. I have a range
> of account numbers from 50100 to 70100. I need to know which account
> numbers are not stored in the table (not currently used) so I can use.
> For instance say I have the following data in table:
> Account Name
> --- ---
> 50100 Test1
> 50105 Test2
> 50106 Test4
> .
> .
> .
> I should see the results:
> 50101
> 50102
> 50103
> 50104
> because 50101-50104 are available account numbers since not currently in
> table.
> Thanks.

Here's a UDF that will fill in all missing integers between a
lower bound and an upper bound, both inclusive. By default,
the lower bound is 50100 and the upper bound is 70100.

CREATE TABLE Accounts
(
account_id INT NOT NULL PRIMARY KEY,
account_name VARCHAR(10) NOT NULL
)

CREATE TABLE Digits
(
d TINYINT NOT NULL PRIMARY KEY CHECK (d BETWEEN 0 AND 9)
)

INSERT INTO Digits (d)
SELECT 0
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 8
UNION ALL
SELECT 9

CREATE TABLE NonnegativeIntegers
(
n INT NOT NULL PRIMARY KEY CHECK (n >= 0)
)

INSERT INTO NonnegativeIntegers (n)
SELECT Ones.d + 10 * Tens.d + 100 * Hundreds.d +
1000 * Thousands.d + 10000 * TenThousands.d
FROM Digits AS Ones
CROSS JOIN
Digits AS Tens
CROSS JOIN
Digits AS Hundreds
CROSS JOIN
Digits AS Thousands
CROSS JOIN
Digits AS TenThousands

CREATE FUNCTION IntervalGaps
(@.lower INT = 50100, @.upper INT = 70100)
RETURNS @.missing TABLE (n INT NOT NULL PRIMARY KEY)
AS
BEGIN
IF @.lower > @.upper
RETURN
INSERT INTO @.missing (n)
SELECT @.lower + I.n
FROM (SELECT MIN(account_id)
FROM Accounts) AS A(least_id)
INNER JOIN
NonnegativeIntegers AS I
ON I.n < A.least_id - @.lower
INSERT INTO @.missing (n)
SELECT A1.account_id + I.n + 1
FROM Accounts AS A1
INNER JOIN
Accounts AS A2
ON A2.account_id > A1.account_id + 1 AND
NOT EXISTS (SELECT *
FROM Accounts
WHERE account_id > A1.account_id AND
account_id < A2.account_id)
INNER JOIN
NonnegativeIntegers AS I
ON I.n < A2.account_id - A1.account_id - 1
INSERT INTO @.missing (n)
SELECT A.greatest_id + I.n + 1
FROM (SELECT MAX(account_id)
FROM Accounts) AS A(greatest_id)
INNER JOIN
NonnegativeIntegers AS I
ON I.n < @.upper - A.greatest_id
RETURN
END

-- Sample data
INSERT INTO Accounts (account_id, account_name)
SELECT 50102, 'test1'
UNION ALL
SELECT 50105, 'test2'
UNION ALL
SELECT 50106, 'test4'

SELECT n AS account_id
FROM IntervalGaps(DEFAULT, DEFAULT)

account_id
50100
50101
50103
50104
50107
50108
...
70100

Regards,
jag|||SET NOCOUNT ON

declare @.lowwer as int
declare @.upper as int

declare @.cur as int

set @.lowwer = 50100
set @.upper = 50200

set @.cur = @.lowwer

IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#missing]'))
Drop table #missing
--temp table to hold list of missing
create table #missing (
Missing int
--,NotMissing int
)

IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#Account]'))
Drop table #Account
-- temp table to hold your range
create table #Account(
Account_number int
)

insert into #Account (Account_number)
select Account from [your table]
where inv_number like '[0-9][0-9][0-9][0-9][0-9][0-9]'
and inv_number between @.lowwer and @.upper
-- and any other filter you think would be handy

While( @.cur between @.lowwer and @.upper)
Begin --while

if not exists (select Account_number from #Account
where inv_number = @.cur)
Begin --if
insert into #missing (Missing) values (@.cur)
End --if
--else
--Begin --else
--insert into #missing (NotMissing) values (@.cur)
--End --else

set @.cur = @.cur + 1

End --while
-- return the results
select * from #missing
--clean up
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#missing]'))
Drop table #missing

IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#Inv]'))
Drop table #Inv

Friday, March 23, 2012

Generating Unique Check Digits

Hi all,
I've implemented a small routine that generates "ID" numbers in hexadecimal
format. Does anyone know of any routines to generate a unique check digit
for a hexadecimal number? I've found several ways to do it in Base 10, but
not so much for hex. I really like the UCC mod 10(3) methods, but the check
digits are fairly linear in progression when I try to apply it to hex.
Here's an example of the types of ID's I'll be generating:
B951-D300-1E73-C1xx
Note that the dashes are just to "prettify it", and can be ignored for my
purposes. "xx" represents a placeholder for the 2 hexadecimal check digits.
B951D3001E73C1xx
Thanks. Any help is appreciated.Michael,
Do you need the check digit for more than
catching transcription or typing errors? Is there
a reason "fairly linear" in progression is not acceptable?
Steve Kass
Drew University
Michael C# wrote:

>Hi all,
>I've implemented a small routine that generates "ID" numbers in hexadecimal
>format. Does anyone know of any routines to generate a unique check digit
>for a hexadecimal number? I've found several ways to do it in Base 10, but
>not so much for hex. I really like the UCC mod 10(3) methods, but the chec
k
>digits are fairly linear in progression when I try to apply it to hex.
>Here's an example of the types of ID's I'll be generating:
>B951-D300-1E73-C1xx
>Note that the dashes are just to "prettify it", and can be ignored for my
>purposes. "xx" represents a placeholder for the 2 hexadecimal check digits
.
>B951D3001E73C1xx
>Thanks. Any help is appreciated.
>
>|||Like you mentioned, catching transcription errors is part of it; I also
don't want people to be able to reverse engineer and guess the previous ID
code; for instance, if the code ends with -C1B6, I don't want them to be
able to take a wild guess at -C0B5 being the previous ID.
Thanks
"Steve Kass" <skass@.drew.edu> wrote in message
news:OYxYyK7SFHA.3696@.TK2MSFTNGP15.phx.gbl...
> Michael,
> Do you need the check digit for more than
> catching transcription or typing errors? Is there
> a reason "fairly linear" in progression is not acceptable?
> Steve Kass
> Drew University
>
> Michael C# wrote:
>sql

Wednesday, March 21, 2012

Generating Record number for a result set?

Hi gurus;
any body would like to tell how to generate row numbers with a qurery?
thanks in advance
Ansari
Hi,
see the below link:-
http://www.sqlteam.com/item.asp?ItemID=1491
Thanks
Hari
MCDBA
"M.M Ansari" <mudasar_ansari@.hotmail.com> wrote in message
news:#zj5Kp5cEHA.1000@.TK2MSFTNGP12.phx.gbl...
> Hi gurus;
> any body would like to tell how to generate row numbers with a qurery?
> thanks in advance
> Ansari
>
|||... and along the same lines:
http://www.databasejournal.com/featu...0894_3373861_1
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Hari Prasad wrote:
> Hi,
> see the below link:-
> http://www.sqlteam.com/item.asp?ItemID=1491
>
> Thanks
> Hari
> MCDBA
> "M.M Ansari" <mudasar_ansari@.hotmail.com> wrote in message
> news:#zj5Kp5cEHA.1000@.TK2MSFTNGP12.phx.gbl...
>
>
sql

Generating Record number for a result set?

Hi gurus;
any body would like to tell how to generate row numbers with a qurery?
thanks in advance
AnsariHi,
see the below link:-
http://www.sqlteam.com/item.asp?ItemID=1491
Thanks
Hari
MCDBA
"M.M Ansari" <mudasar_ansari@.hotmail.com> wrote in message
news:#zj5Kp5cEHA.1000@.TK2MSFTNGP12.phx.gbl...
> Hi gurus;
> any body would like to tell how to generate row numbers with a qurery?
> thanks in advance
> Ansari
>|||... and along the same lines:
http://www.databasejournal.com/features/mssql/article.php/10894_3373861_1
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Hari Prasad wrote:
> Hi,
> see the below link:-
> http://www.sqlteam.com/item.asp?ItemID=1491
>
> Thanks
> Hari
> MCDBA
> "M.M Ansari" <mudasar_ansari@.hotmail.com> wrote in message
> news:#zj5Kp5cEHA.1000@.TK2MSFTNGP12.phx.gbl...
>>Hi gurus;
>>any body would like to tell how to generate row numbers with a qurery?
>>thanks in advance
>>Ansari
>>
>
>

Generating Record number for a result set?

Hi gurus;
any body would like to tell how to generate row numbers with a qurery?
thanks in advance
AnsariHi,
see the below link:-
http://www.sqlteam.com/item.asp?ItemID=1491
Thanks
Hari
MCDBA
"M.M Ansari" <mudasar_ansari@.hotmail.com> wrote in message
news:#zj5Kp5cEHA.1000@.TK2MSFTNGP12.phx.gbl...
> Hi gurus;
> any body would like to tell how to generate row numbers with a qurery?
> thanks in advance
> Ansari
>|||... and along the same lines:
http://www.databasejournal.com/feat...10894_3373861_1
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Hari Prasad wrote:
> Hi,
> see the below link:-
> http://www.sqlteam.com/item.asp?ItemID=1491
>
> Thanks
> Hari
> MCDBA
> "M.M Ansari" <mudasar_ansari@.hotmail.com> wrote in message
> news:#zj5Kp5cEHA.1000@.TK2MSFTNGP12.phx.gbl...
>
>
>

Generating numbers in SQL

hi,

I am developing a ASP.NET application using SQL Server as my database.
I want to create a number that is unique.

The number will have 8 digits.

first 2 digits correspond to year. ex:04 or 03

the next 6 digits start with 000001 and it should get added for each new entry of data.

i am not able to generate number in the way i said. I am relatively new to SQL. so any suggestions as how to go about solving the problem?. Are there any samples/codes available for this.

Any help would be highly appreciated.

thanks,
-sriramWhat are you doing this for? Uniqueness for records in a table? You can use an Identity Column for that...what is the business problem you are trying to solve?|||thanks for your reply.

My client wants to do it in the way i explained before. Is there a way??

-sriram|||You could do it a few ways, the incrementing number being the point of divergence. To get the current month you simply use the Month() function with GetDate()

ex:
SELECT Month(GetDate())

There are a number of ways to build the second part...

One would be to create an identity column and after the insert read the SCOPE_IDENTITY(), combine it with the Month(GetDate()) to get your number. The problem with this is that it relies on work after the insert which could be done through a stored proc or a trigger.

Another approach would be to select the top value from the column, trimming the month prefix and the creating your value by incrementing that number.

Do you need to stuff with zeros? Does the month need to be 1 or 01; does the second number need to be led by zeros?|||hi,

thanks for replying.

The second number needs to be led with zeros.

thanks,

-sriram|||You can use the SQL-Server Function Replicate() for this:


DECLARE @.MaxLength AS int
SET @.MaxLength = 6
DECLARE @.INT AS int

SET @.INT = 1
PRINT REPLICATE('0', @.MaxLength - LEN(LTRIM(STR(@.INT))) ) + LTRIM(STR(@.INT))
-- >> prints 000001

SET @.INT = 1234
PRINT REPLICATE('0', @.MaxLength - LEN(LTRIM(STR(@.INT))) ) + LTRIM(STR(@.INT))
-- >> prints 001234

|||Here is some code that will help; I tried not to use UDFs because I dont know what version of SQL you are using. Regardless this will show you how to parse the old value out and generate a new one.

-- Assumes that the fixed length is 10

Declare @.OldValuechar(10)
Set@.OldValue= '0100000365'

Declare @.OldNumberint
Set@.OldNumber= Cast(Substring(@.OldValue,3, LEN(@.OldValue)-2) as int)

Declare @.MonthNamevarchar(10)
Set@.MonthName= DateName(M, Cast(Left(@.OldValue,2) + '/01/2000' as datetime))

-- Show the old values
select @.OldValue
select@.OldNumber
select @.MonthName

Declare @.MonthPrefixchar(2)
Set@.MonthPrefix= (Select Case WHEN Month(GetDate()) >= 10 THEN Cast(Month(GetDate()) as char(2)) ELSE '0' + Cast(Month(GetDate()) as char(1)) END)

Declare @.NewNumbervarchar(8)
Set@.NewNumber= Cast(@.OldNumber + 1 as varchar)

Declare @.Prefix varchar(7)
Set@.Prefix= ''

While Len(@.Prefix) < (7 - len(@.NewNumber))
Set @.Prefix = '0' + @.Prefix

Declare @.NewValuechar(10)
Set@.NewValue= @.MonthPrefix + @.Prefix + Cast(@.NewNumber as char)

-- Show the New Value
Select @.NewNumber
Select@.NewValue|||thanks,

i think it makes sense.

it should work..

anyway thanks for your reply.

-sriramsql

Generating membership number

Hi all,
I have a question about generating membership numbers on the fly when someone registers to my website.

Rather than using the auto increment field as a membership number, I would rather keep it as just as the ID for the record and I would like to have a seperate membership number that looks something similar to this...

SR357324J

This will then stay with them for the lifetime of their membership and be on their printed loyalty card.

My questions are...
1) Is there a 'good practice' for membership number format and generation?

2) If this was used as a unique field, is there a degradation in performance when looking up records due to it being alphanumeric.

I may be well off base here, however these are my thoughts so far and your opinion/help is greatly appreciated.

Thanks for your contribution.I'm not sure about #1, but as for #2: You can still use the ID to retrieve records, even if you display the manually generated membership #.

If, however, you create your clustered index on the manually generated membership # field, you can make that field the clustered index (though this may hurt performance as relates to joins with other tables).|||I'd say let the db autogenerate an ID for you and you can append the 'SR' later..you could get into locking problems and duplicate ID's sooner or later..

hth

Monday, March 19, 2012

generating auto sequential numbers

I am using sybase aSE12.5. I have a table with only one column with
datatype char(2). when i query this table to select all the records, i
should get these records in the ascending order and they should be numbered
, i.e, the o/p should look something like this
column_name
-- ---
1 AB
2 AC
3 AD
and so on.

I cannot add an extra column and i need this to be done in a single query.

--
Message posted via http://www.sqlmonster.comPraveen D'Souza via SQLMonster.com (forum@.nospam.SQLMonster.com) writes:
> I am using sybase aSE12.5. I have a table with only one column with
> datatype char(2). when i query this table to select all the records, i
> should get these records in the ascending order and they should be
> numbered , i.e, the o/p should look something like this
> column_name
> -- ---
> 1 AB
> 2 AC
> 3 AD
> and so on.
> I cannot add an extra column and i need this to be done in a single query.

If you are using Sybase, you should be posting to
comp.databases.sybase, or whatever that groups goes by at SQLMonster.

However, this solution should work on Sybase as well:

SELECT col, (SELECT COUNT(*) FROM tbl b WHERE b.col >= a.col)
FROM tbl a
ORDER BY col

For large amount of data this may not be very performant, though.
SQL 2005, currently in beta, offers a ROW_NUMBER() function to
handle this a little more effeciently. I don't know if Sybase offers
something similar.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||In a tiered architecture display is done in the front end and not the
database.

Monday, March 12, 2012

Generating a series of numbers or dates?

I want to generate a resultset that is just a series of numbers in
ascending order or perhaps a series of dates.. What I mean is, is
there a way to generate a temporary table of dates given an input of a
start date and an end date.. This table would then contain an entry
for each date in ascending order from the start date to and including
the end date..

Or perhaps with numbers, given a start of 5 and and end of 7
the resulting table would be

5
6
7

Would appreciate any help with this one.. Thanks
ChrisThe usual technique is just to keep these as permanent tables in your
database and then SELECT numbers and dates out of them as required.

Dates:

CREATE TABLE Calendar
(caldate DATETIME NOT NULL PRIMARY KEY)

INSERT INTO Calendar (caldate) VALUES ('20000101')

WHILE (SELECT MAX(caldate) FROM Calendar)<'20101231'
INSERT INTO Calendar (caldate)
SELECT DATEADD(D,DATEDIFF(D,'19991231',caldate),
(SELECT MAX(caldate) FROM Calendar))
FROM Calendar

Numbers:

CREATE TABLE Numbers
(num INTEGER PRIMARY KEY)

INSERT INTO Numbers VALUES (1)

WHILE (SELECT MAX(num) FROM Numbers)<8192
INSERT INTO Numbers
SELECT num+(SELECT MAX(num) FROM Numbers)
FROM Numbers

--
David Portas
----
Please reply only to the newsgroup
--|||Refer to:
http://www.bizdatasolutions.com/tsql/tblnumbers.asp

--
-- Anith
( Please reply to newsgroups only )

Wednesday, March 7, 2012

Generate Sequential Numbers - T-SQL

Hi
i have a table with a field called response id. i would like to update this
field with sequential numbers starting from 1000.
can someone please advise me how i go about this.
any help would be appreciated.
thanks heaps in advance
Kind Regards
RidhimaTested on NORTHWIND:
alter table ORDERS
add RESPONSEID int
GO
update ORDERS
set RESPONSEID = RowNumber + 999
from ORDERS inner join
(select ROW_NUMBER() OVER (order by OrderID) as RowNumber, ORDERID from
ORDERS) as TMP on
TMP.ORDERID = ORDERS.ORDERID
--
Alain Quesnel
alainsansspam@.logiquel.com
www.logiquel.com
"Ridhima" <Ridhima@.discussions.microsoft.com> wrote in message
news:65055160-3C5A-4278-B453-C4DA6344731D@.microsoft.com...
> Hi
> i have a table with a field called response id. i would like to update
> this
> field with sequential numbers starting from 1000.
> can someone please advise me how i go about this.
> any help would be appreciated.
> thanks heaps in advance
> Kind Regards
> Ridhima|||On Nov 15, 9:14 pm, Ridhima <Ridh...@.discussions.microsoft.com> wrote:
> Hi
> i have a table with a field called response id. i would like to update this
> field with sequential numbers starting from 1000.
> can someone please advise me how i go about this.
> any help would be appreciated.
> thanks heaps in advance
> Kind Regards
> Ridhima
Another way to control this moving forward is to select Design for the
table in SSMS and select the ResponseID field. In the Column
Properties section, select Identity Specification >> (Is Identity) and
set it to Yes. Set Identity Increment to 1 and then set Identity Seed
to 1000 (which is the starting integer value of the ResponseID field).
Hope this helps.
Regards,
Enrique Martinez
Sr. Software Consultant|||That was my first thought too. But when I tested it, it created "random"
values in the ResponseID field.
--
Alain Quesnel
alainsansspam@.logiquel.com
www.logiquel.com
"EMartinez" <emartinez.pr1@.gmail.com> wrote in message
news:29bb8aeb-5839-42b5-8e27-57978af3f160@.v4g2000hsf.googlegroups.com...
> On Nov 15, 9:14 pm, Ridhima <Ridh...@.discussions.microsoft.com> wrote:
>> Hi
>> i have a table with a field called response id. i would like to update
>> this
>> field with sequential numbers starting from 1000.
>> can someone please advise me how i go about this.
>> any help would be appreciated.
>> thanks heaps in advance
>> Kind Regards
>> Ridhima
>
> Another way to control this moving forward is to select Design for the
> table in SSMS and select the ResponseID field. In the Column
> Properties section, select Identity Specification >> (Is Identity) and
> set it to Yes. Set Identity Increment to 1 and then set Identity Seed
> to 1000 (which is the starting integer value of the ResponseID field).
> Hope this helps.
> Regards,
> Enrique Martinez
> Sr. Software Consultant|||declare @.secuence int
set @.secuence = 999
update table
set @.secuence = response_id = @.secuence + 1
PS.
It does work ! Have faith !
"Ridhima" <Ridhima@.discussions.microsoft.com> escribió en el mensaje
news:65055160-3C5A-4278-B453-C4DA6344731D@.microsoft.com...
> Hi
> i have a table with a field called response id. i would like to update
> this
> field with sequential numbers starting from 1000.
> can someone please advise me how i go about this.
> any help would be appreciated.
> thanks heaps in advance
> Kind Regards
> Ridhima

Sunday, February 26, 2012

Generate Random Numbers

Hi, how do I use the Rand Function to generate a Random integer value between 1 and 20. Any help will be appreciated.

Thanks

What about this one:

SELECT

CEILING(RAND()*20) AS myrandIntegerBetween0And20

Generate Multiple Tables from Column data

I have a large table with 30000+ rows. Most of the rows can be group by Column 1.

Say you had 10000 people and numbers 1 through 3. Every person would be linked to 1, 2 & 3. hence 30000 rows.

What i need to do is create a table for each number. i.e. Table 1 where column 1 is 1 (then it lists 10000 rows)

Table 2 where column 1 is 2 (another 10000 rows) and so on.

I need to create just under 300 tables. Is there a quicker way of doing that than this 300 times?

I'll try and put it into an example of what i'm looking for

Original table

Column 1 Column 2 Column 3 etc...

-

1 a b .....

2 a b .....

1 c d .....

2 c d .....

AND THEN TURN IT INTO

Table name '1'

Column 1 Column 2 Column 3 etc...

-

1 a b .....

1 c d .....

Table name '2'

Column 1 Column 2 Column 3 etc...

-

2 a b .....

2 c d .....

Using one piece of code and the data in Column 1 as the table names?

Thanks

it worked like this

Select *into 1 from Original Table where column1 = 1

Select *into 2 from Original Table where column1 = 2

All i did was copy the column names into excell and duplicate the rest and run seperate select queries.

Friday, February 24, 2012

generate Identity field on existing table

Hi, I have the lovely task of overhauling some of our SQL-based systems. I've found many tables that don't have unique identifying numbers that really should have them. I've searched around and people keep mentioning the Identity field as being similar to Autonumber in Access. The only examples I could find involved setting up a new table... but I need to add (and populate) an identity column to an existing database table. Does anyone know the command for this?

Example... my table is called PACountyTown. It currently has 3 columns: County, Town, and Area. I wish to call the identity-ish field RecordID.

Thanks in advance!do you mean programmatically ? you can always add it in the design view...

hth|||If you add a new column and set its type to INT and set Identity to true, it will auto populate based on the Identity Seed and the Identity Increment.|||Thanks for the replies. I figured it out right after my first posting. DOH! :)