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.
| Author |
Topic |
|
Oliver Harris
Starting Member
6 Posts |
Posted - 2003-12-16 : 17:06:41
|
| I've been asked to create a new numbering system for an existing table. They want the records numbered by month, year and count of records in that month and year. So, for instance if there are 8 records created in Dec-03, the numbers for those items would be 12-03-001 - 12-03-008. I can generate the month and year portions, but how do I generate the count? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-12-16 : 18:01:24
|
| If your data already was corrected with this new format, then this would work for future INSERTs:INSERT INTO Table1 (Column1)SELECT CONVERT(VARCHAR(2), DATEPART(m, GETDATE())) + '-' + SUBSTRING(CONVERT(VARCHAR(4), DATEPART(yy, GETDATE())), 3, 2) + '-' + CONVERT(VARCHAR(3), COUNT(Column1) + 1)FROM Table1GROUP BY SUBSTRING(Column1, 1, 5)As far as fixing your data for this new format, I can only figure it out with a loop. Hopefully someone else can figure out a set-based method for you. Or is the data already "fixed"?Tara |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-12-16 : 18:03:08
|
| I would ask: what is the significance of the numbering? Why are you storing this in the table? what are you using it for? What is the primary key of this table, and what kind of data is stored in it?- Jeff |
 |
|
|
Oliver Harris
Starting Member
6 Posts |
Posted - 2003-12-16 : 18:27:19
|
quote: Originally posted by tduggan If your data already was corrected with this new format, then this would work for future INSERTs:INSERT INTO Table1 (Column1)SELECT CONVERT(VARCHAR(2), DATEPART(m, GETDATE())) + '-' + SUBSTRING(CONVERT(VARCHAR(4), DATEPART(yy, GETDATE())), 3, 2) + '-' + CONVERT(VARCHAR(3), COUNT(Column1) + 1)FROM Table1GROUP BY SUBSTRING(Column1, 1, 5)As far as fixing your data for this new format, I can only figure it out with a loop. Hopefully someone else can figure out a set-based method for you. Or is the data already "fixed"?Tara
Tara,Thanks for the reply. The data hasn't been fixed yet, that was what I was trying to do. I will keep your insert statement in mind though. Thanks.quote: Originally posted by jsmith8858 I would ask: what is the significance of the numbering? Why are you storing this in the table? what are you using it for? What is the primary key of this table, and what kind of data is stored in it?- Jeff
The numbering system is what the user asked for. Personnally I would have done it differently, but they think it will be easier to track using that number rather than using the automatically generated ID number (the primary key). The application is a web application similar to this forum software. A question is asked and other users provide answers. The person who made the request then decides when the question has been answered and closes the question. The table I'm working on is basically a table of threads. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-12-16 : 19:27:11
|
Here is the looping solution to "fix" the data:DECLARE @num INTDECLARE @WhichMonth INTDECLARE @WhichYear INTDECLARE @WhichOne VARCHAR(50)SET @num = 1WHILE (SELECT COUNT(*) FROM Table1 WHERE ColumnA NOT LIKE '%-%') > 0BEGIN SELECT @WhichOne = ColumnA FROM Table1 WHERE ColumnA NOT LIKE '%-%' AND ColumnB = (SELECT MIN(ColumnB) FROM Table1 WHERE ColumnA NOT LIKE '%-%') SELECT @WhichMonth = DATEPART(m, ColumnB), @WhichYear = DATEPART(yy, ColumnB) FROM Table1 WHERE ColumnA = @WhichOne UPDATE Table1 SET ColumnA = CASE WHEN DATEPART(m, ColumnB) < 10 THEN '0' + CONVERT(VARCHAR(2), DATEPART(m, ColumnB)) ELSE CONVERT(VARCHAR(2), DATEPART(m, ColumnB)) END + '-' + SUBSTRING(CONVERT(VARCHAR(4), DATEPART(yy, ColumnB)), 3, 2) + '-' + CASE WHEN @num < 10 THEN '00'+ CONVERT(VARCHAR(3), @num) WHEN @num < 100 THEN '0'+ CONVERT(VARCHAR(3), @num) ELSE CONVERT(VARCHAR(3), @num) END WHERE ColumnA = @WhichOne IF @WhichMonth = (SELECT DATEPART(m, MIN(ColumnB)) FROM Table1 WHERE ColumnA NOT LIKE '%-%') AND @WhichYear = (SELECT DATEPART(yy, MIN(ColumnB)) FROM Table1 WHERE ColumnA NOT LIKE '%-%') SET @num = @num + 1 ELSE SET @num = 1END If you've got thousands of rows in your table, this is going to be resource intensive and slow due to the looping.Tara |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-12-17 : 01:35:16
|
A little set-based processing, anyone?update aset orderid = cast(datepart(mm, a.orderdate) as varchar) + '-' + substring(cast(datepart(yy, a.orderdate) as varchar),3,2) + '-' + cast(((select count(*) from #orders b where datepart (yy, b.orderdate) = datepart(yy, a.orderdate) and datepart (m, b.orderdate) = datepart(m, a.orderdate) and b.orderdate < a.orderdate) + 1) as varchar)from #orders a OwaisWe make a living out of what we get, but we make a life out of what we give. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-12-17 : 12:15:33
|
| For Owais' solution, which btw is much better because it is set-based, you'll need to add another case statement to get the format that you need since it does not give 000 format for the last three characters. If you grab my second case statement and tweak it a bit for his solution, you'll be able to do this all in one statement without the looping.I knew it could be done the set-based way, I just don't know how to come up with those solutions yet.Tara |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-12-17 : 13:11:19
|
| The second case could also be replaced with this to give you a formatted three digit number:RIGHT('000' + CONVERT(varchar, @num), 3)OwaisWe make a living out of what we get, but we make a life out of what we give. |
 |
|
|
Oliver Harris
Starting Member
6 Posts |
Posted - 2003-12-18 : 19:11:28
|
| This is way over my head. Not sure how to translate that into my particular situation. I did manage to come up with a hack of my own to get it done though. Thanks for the help. |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2003-12-19 : 08:29:48
|
| I see you're up+running, but as a learning exercise......it's definitely worth the investment of your time to work out what's going on here.....it'll bring you 4 leaps forward in your understanding of the capabilities of SQL, rather than inching forward otherwise....Try running the code given....if necessary break it down into smaller parts so that you can understand each bit.You'll be pleasantly surprised at the outcome. |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-12-19 : 10:29:44
|
| Well said, Andrew...any time you spend on understanding SQL will not be wasted. If you can provide the table structure and some sample data in the form of DDL and DML statements, we'd be willing to show exactly how the above queries would fit into the context of your problem. OwaisWe make a living out of what we get, but we make a life out of what we give. |
 |
|
|
Oliver Harris
Starting Member
6 Posts |
Posted - 2003-12-19 : 10:45:32
|
| I agree it would definately be worth it. If you guys don't mind helping a newbie I'd love to learn. I'm afraid you already lost me on some of the terminology though. What are DDL and DML statements? |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2003-12-19 : 11:00:20
|
| DDL...data definition langguage....statements to create databases, tables, stored procedures, etc...DML...data manipulation language....regular SQL statements...select, insert, delete, etc... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-12-19 : 11:03:25
|
quote: Originally posted by Oliver Harris I agree it would definately be worth it. If you guys don't mind helping a newbie I'd love to learn. I'm afraid you already lost me on some of the terminology though. What are DDL and DML statements?
DDL: Data Definition LanguageCREATE TABLE myTable99 (Col1 int, Col2char(1), ect)DML: Data Manipilation LanguageINSERT INTO myTable99(Col1, col2, ectSELECT 1,'a',ectDELETE FROM myTable99UPDATE myTable99 SET Col1=SELECT...Sample Data:Use the INSERT DML above...but likeINSERT INTO myTable99(Col1, col2, ectSELECT 1,'a',ect UNION ALLSELECT 1,'a',ect UNION ALLSELECT 1,'a',ect UNION ALLSELECT 1,'a',ect UNION ALLSELECT 1,'a',ect UNION ALLSELECT 1,'a',ect UNION ALLSELECT 1,'a',ect UNION ALLSELECT 1,'a',ect UNION ALLSELECT 1,'a',ectTo give us a lot of data to work with..And then there DCL...which I forget... Data Control Language?Brett8-) |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Oliver Harris
Starting Member
6 Posts |
Posted - 2003-12-19 : 11:28:45
|
| Wow, thanks for all the responses. I haven't ever built a table with a SQL statement, I usually just build it in the enterprise manager. This statement would give you the pertinent fieldsCREATE TABLE tblRFIs ( RFIID INTEGER NOT NULL, RFI_Add_Date DATETIME NOT NULL, ControlNumber CHARACTER (9) ); There's more fields than that, but these are the ones that I'm concerned with. I don't know how t set the Identity or Identity Seed property, but those are set to Yes and 1 respectively for RFIID. Does this make sense? |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2003-12-19 : 11:39:25
|
| The above makes sense alright.....but now that you're beginning to get more than your toes wet....you should start to migrate away from EM...and get to learn and know SQL from the raw commands.The windows interface of EM is a reasonable shortcut for avoiding the raw commands of SQL....but it's no substitute for knowing what goes on under the hood. It'll be good for you in the long rung. |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-12-20 : 03:26:41
|
| Oliver, right click on the table in Enterprise Manager, and choose "All tasks -> Generate SQL Script" to generate the DDL for the table. It's a good way to learn how an existing table translates into DDL and how you can create tables using Query Analyzer. Unfortunately, there is no way to generate DML (for the sample data) statements from EM, but there are a few third party tools that could do the trick.OwaisWe make a living out of what we get, but we make a life out of what we give. |
 |
|
|
Oliver Harris
Starting Member
6 Posts |
Posted - 2003-12-22 : 11:33:58
|
quote: Originally posted by mohdowais Oliver, right click on the table in Enterprise Manager, and choose "All tasks -> Generate SQL Script" to generate the DDL for the table. It's a good way to learn how an existing table translates into DDL and how you can create tables using Query Analyzer. Unfortunately, there is no way to generate DML (for the sample data) statements from EM, but there are a few third party tools that could do the trick.OwaisWe make a living out of what we get, but we make a life out of what we give.
Owais,I wondered if there wasn't something like that. That answers my question about how to set the identity property. Here is what it produced.CREATE TABLE [dbo].[tblRFIs] ( [RFID] [int] IDENTITY (1, 1) NOT NULL , [RFI_Add_Date] [datetime] NULL , [RFI_Resp_Date] [datetime] NULL , [RFI_Req_Date] [datetime] NULL , [RFI_Close_Date] [datetime] NULL , [RFI_Classification] [int] NULL , [RFI_Precedence] [int] NULL , [RSID] [int] NULL , [SBID] [int] NULL , [RFI_Suggested_Resp] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [RFI_Title] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [PID_Init] [int] NULL , [PID_Resp] [int] NULL , [RFI_Web_Flag] [bit] NULL , [RFI_Document] [nvarchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [RFI_Alt_POC] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [RFI_Alt_Phone] [nvarchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [RFI_Alt_Email] [nvarchar] (75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [ControlNumber] [nchar] (9) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GO |
 |
|
|
|
|
|
|
|