Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 inserting pattern? please be nice! :)

Author  Topic 

pinkswansays
Starting Member

4 Posts

Posted - 2011-12-13 : 11:16:49
I am TOTALLY new to SQL ... meaning most of the posts I have seen so far are over my head :/ So please be nice and explain as you would to a monkey ...

I need to insert data into a column that has a pattern but is not a simple succession. The pattern is somewhat like this:

01-01A
01-01B
01-02A
01-02B
02-01A
02-01B...

I am using SQL Server, and I know the basics of insert into, delete, etc.

Thank you!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 11:23:31
can you explain the business rules using which you will generate the above pattern?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

lappin
Posting Yak Master

182 Posts

Posted - 2011-12-13 : 12:09:33
Found a good page showing how to build a custom id:
http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server
The idea is to create a column which gets an auto number assigned by sql server (identity column)
then create a calculated column which applies a function to the number to return a custom value.
this calculated column is what you use for your "patterned number".

Here is my attempt to modify the function used to generate your pattern. It is close but not there yet. I'm sure some-one else would know how to finish it.



CREATE function CustomerNumber(@i int)
returns char(6)
as
begin
return (
char(@i / 50 % 10 + 48) +
char(@i / 5 % 10 + 48)+
'-'+
char(@i / 25 % 10 + 48) +
char(@i / 2.5 % 10 + 48)+
char(@i / 26 % 260 + 65)
)
end


select x, dbo.CustomerNumber(x)
from
(
select 1 as x union all
select 2 as x union all
select 3 as x union all
select 4 as x union all
select 5 as x union all
select 6 as x union all
select 7 as x union all
select 8 as x union all
select 9 as x union all
select 10 as x union all
select 11 as x union all
select 12 as x union all
select 13 as x union all
select 14 as x union all
select 25 union all
select 26 union all
select 27 union all
select 51 union all
select 52
) x
Go to Top of Page

pinkswansays
Starting Member

4 Posts

Posted - 2011-12-13 : 12:10:07
quote:
Originally posted by visakh16

can you explain the business rules using which you will generate the above pattern?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





I am not sure what you mean ... do you mean what is the pattern I am actually trying to put in?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 12:11:08
yes..exactly.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pinkswansays
Starting Member

4 Posts

Posted - 2011-12-13 : 12:18:31
@lappin

Thank you! That is what I was looking for.

The sequence I posted is not actually what I am using in the db, so don't worry about that. I think I will be able to figure it out from the tut so thank you!
Go to Top of Page

pinkswansays
Starting Member

4 Posts

Posted - 2011-12-13 : 12:23:06
The pattern is:

01-01V
01-01A
01-01D
01-02V
01-02A
01-02D
...
01-17V
01-17A
01-17D (this pattern goes up to 17, then restarts as so:)
02-01V
02-01A
02-01D
02-02V
02-02A
02-02D
...and so forth to 17 and then starts again with 03-01V
Go to Top of Page
   

- Advertisement -