| Author |
Topic |
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 10:03:45
|
| Iam trying to add a column to an existing table that would be an "identifier". I called it "ReadingNumber" and selected Identity "Yes" and "Identity increment" as 1.When I add it, it just gives the rows random numbers instead of by the order they were inserted into the database by.....is there a way to autonumber the columns correctly? I have a COLUMN called Date and also one called Time that have the date and Time, but the format is char.Would I have to convert the date time columns into something SQL understands, sort them ASC or DESC and then do the Identity column add?Thanks for any helpEdited |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-08-25 : 10:25:45
|
If you need the identity values for existing rows in a specific order then you might need to do something like this:set nocount oncreate table myTable (strDate varchar(10) ,strTime varchar(10))create table myNewTable (ReadingNumber int identity primary key ,strDate varchar(10) ,strTime varchar(10) ,ConvertedDate datetime)goinsert myTableselect '9/1/2000', '10:52:27.0' unionselect '1/2/1999', '17:25:35.1' unionselect '8/20/2004', '1:15:22.0'select * from myTableinsert myNewTable (strDate ,strTime ,ConvertedDate)select strDate ,strTime ,ConvertedDate = convert(datetime, strDate + ' ' + strTime)from myTable order by convert(datetime, strDate + ' ' + strTime)godrop table myTablegoexec sp_rename myNewTable, myTablegoselect * from myTablegodrop table myTable Be One with the OptimizerTG |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 10:38:55
|
| Wow...Iam new to sql so please bear with me.From the looks of it, you are joining the date and time fields in another table then putting them back into the original table?I just want to sort and number the entrys by their date and time insert....is this the simplist way to do it?Thanks for your help |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-08-25 : 10:44:29
|
If your objective to assign identity values in a specific order then I think you'll need to use the alternate table approach. If all you want to do is return the combined values in a Select statement in order of date then all you need to do is:select strDate ,strTimefrom myTable order by convert(datetime, strDate + ' ' + strTime) Be One with the OptimizerTG |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 10:55:59
|
| OK - I tried the second method and get a "invalid syntax" at strDate....did you see my edit to my first post in this topic? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-08-25 : 11:00:48
|
| GB, post the code that generated the syntax error and we'll get it sorted out.by the way, here a link that one of the contributors to this site has put together that really helps get questions answered fast.http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxBe One with the OptimizerTG |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 11:04:34
|
| SELECT strDate, strTimeFROM Press_3ORDER BY CONVERT(DATETIME strDate + ' ' + strTime) |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-08-25 : 11:22:17
|
sorry, you'll also need to post an example of your data from Press_3 and the datatypes of the columns you are using. strDate and strTime are column names from my previous example. You'll need to use the column names from your table. (this is where that link above comes in handy Be One with the OptimizerTG |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-08-25 : 11:23:40
|
| On a related note:Since you're new to sql server you may be new to database develoment in general so let meexplain a few concepts. (just my opinions, if other readers dissagree I'm sure we'llhear from them)identity columns:identity columns are mainly used as surrogate keys who's only purpose is to uniquelyidentify a row. The value and sequence of the identity should be meaningless. Manydevelopers incorrectly use the identities to display things like row numbers in reportsor to order output. For either of these objectives there should be other columns inthe table to accomplish these tasks like a datetime value or a "lineNumber" column.Dates:Storing dates in sql server as any datatype other than datetime or smalldatetime wouldbe like storing numbers as: (one, two, three, four) They would sort alpabetically,you couldn't perform mathmatical operations on them, they would be useless.Some limitations on the way sql server allows for altering tables. The ALTER TABLE ALTER COLUMN command is very powerfull but won't allow you to add and remove identity the property of an existing column. For that reason, it's necessary to either drop and re-add the column or the entire table when you want to alter a column to add or remove the identity property.Sorry for (not asked for) opinions, I hope they help...Be One with the OptimizerTG |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 11:35:51
|
Any help is appreciated, thanks for taking the time! |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 12:16:53
|
| On dates and times ....Iam doing the inserts to this table from a product called wonderware.....and the strings are the only way I know of to do it...Is there a way to make sql date/time stamp the insert?ThanksGary |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 12:23:54
|
| Column names2 ReadingNumber bigint 8 00 [Date] char 8 00 [Time] char 10 10 BlankID int 4 10 ProgramNo smallint 2 10 ActualProgramNo smallint 2 10 LotNumber real 4 10 PressLoad bigint 8 10 ProgramHighPSISP int 4 10 ActualHighPSISP int 4 10 ProgramHighPSITimeSP int 4 10 ActualHighPSITimeSP int 4 10 ProgramRampSP int 4 10 ActualRampSP int 4 10 ProgramLowPSISP int 4 10 ActualLowPSISP int 4 10 ProgramLowPSITimeSP int 4 10 ActualLowPSITimeSP int 4 10 ProgramTempSP int 4 10 ActualTempSP int 4 10 ActualPSISP int 4 10 ActualPSI int 4 10 ActualTemp int 4 10 TotalPressTimeSP numeric 5 10 ActualPressTimeSP numeric 5 10 HighPSIOverride char 3 10 HighPSITimeOverride char 3 10 RampTimeOverride char 3 10 LowPSIOverride char 3 10 LowPSITimeOverride char 3 10 TempOverride char 3 11 PressProgramNoOverride char 3 1Sample data 2304 8/24/2005 15:10:10 2337 56 56 30927 2 2771 2771 300 300 60 60 1001 1001 60 60 285 285 2771 2746 280 420 420 No No No No No No No 2305 8/24/2005 15:14:10 2337 56 56 30927 2 2771 2771 300 300 60 60 1001 1001 60 60 285 285 2771 2769 286 420 420 No No No No No No No 2306 8/24/2005 15:03:10 2337 56 56 30927 1 2771 2771 300 300 60 60 1001 1001 60 60 285 285 2771 2777 284 420 420 No No No No No No No 2307 8/24/2005 15:03:40 2337 56 56 30927 1 2771 2771 300 300 60 60 1001 1001 60 60 285 285 2771 2766 285 420 420 No No No No No No No 2308 8/24/2005 15:15:10 2337 56 56 30927 2 2771 2771 300 300 60 60 1001 1001 60 60 285 285 1561 1956 286 420 420 No No No No No No No 2309 8/24/2005 15:16:10 2337 56 56 30927 2 2771 2771 300 300 60 60 1001 1001 60 60 285 285 1001 1338 286 420 420 No No No No No No No 2310 8/24/2005 15:04:10 2337 56 56 30927 1 2771 2771 300 300 60 60 1001 1001 60 60 285 285 2771 2770 286 420 420 No No No No No No No 2311 8/24/2005 15:05:10 2337 56 56 30927 1 2771 2771 300 300 60 60 1001 1001 60 60 285 285 2771 2770 286 420 420 No No No No No No No |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-08-25 : 13:09:39
|
| >>Is there a way to make sql date/time stamp the insert?You can insert the current datetime value usingthe getdate() function ie:insert myTable (myDateTimeColumn)values (getdate())see Books Online which is the Help that ships with sql server>>OK - I tried the second method and get a "invalid syntax" at strDateSince your column names are [Date] and [time](which by the way are really bad names for columns) you need:SELECT [Date], [Time]FROM Press_3ORDER BY CONVERT(DATETIME [Date] + ' ' + [Time])Be One with the OptimizerTG |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 13:19:21
|
| Why are they bad names? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-08-25 : 13:30:46
|
| Because they are totaly non-descriptive of what business concept they represent. Plus, they are probably key words. Bad object names include: (code,id,group,date,From,To)Good object names could be: (zipCode, CompanyID, InsuranceGroupDesc, CreatedDate, StartBytePosition, EndBytePosition)Be One with the OptimizerTG |
 |
|
|
GB
Starting Member
22 Posts |
Posted - 2005-08-25 : 13:34:49
|
Thanks |
 |
|
|
|