views:

92

answers:

3
+1  Q: 

Cast or Convert?

I have am getting an error when I try and alter a date column:

Arithmetic overflow error for type varchar, value = 20100601.000000.

I would like it to go from float to the datetime format of 20100601.

Since Alter doesn't work, how can I use cast or convert to change the datatype for every entry in the table?

+3  A: 

"20100601.000000" when cast to datetime from float means 20 million, 100 thousand, 601 days after 01 Jan 1900.

"20100601" as a string is 01 Jun 2010.

I'd use this to be 100% sure of conversions

SELECT CAST(CAST(CAST(20100601.000000 AS int) AS char(8)) AS datetime)
gbn
OK, but that doesn't answer my question about how to convert or cast it for every value in the column.
Daniel
I understand it now. Can I apply this code to the entire column?
Daniel
Won't this cause the loss of any time component if there is one?
Tom H.
Good point. Daniel will there ever be a decimal part and if so what format do you have that in?
Martin Smith
@Tom H: yes. Do we know if they are to be kept? @Daniel: You can apply this code the column.. See Martin Smith's comment for how to
gbn
I'd like for there to be no decimal point. I don't care about the time component, although I would like to get rid of the 00:00:00, etc.
Daniel
If you need the time component: DATEADD(ms, (@decimal_datetime%1)*86400000, CAST(CAST(CAST(@decimal_datetime AS INT) AS CHAR(8)) AS DATETIME)) (this is for MS SQL Server - your date functions may vary)
Tom H.
+2  A: 

I'm sure there must be a better way...

ALTER TABLE dbo.YourTable ADD
    NewColumn datetime NULL

UPDATE [YourTable]
   SET  [NewColumn] = 
        CAST(CAST(CAST(ROUND(YourOriginalColumn,0) as INT) AS char(8)) AS DATETIME)

Then if the idea is to replace the old column after you have sanity checked things, made a backup etc.

ALTER TABLE dbo.Cars
    DROP COLUMN YourOriginalColumn

EXECUTE sp_rename N'dbo.YourTable.NewColumn', N'YourOriginalColumn', 'COLUMN' 
Martin Smith
Nope. This is quite clear about what casts are needed. I came up with the same...
gbn
How do I do this for EVERY value in the column? Over 1,000,000 rows.
Daniel
I'd add a new column to the table of the correct datatype update it to the new value, sanity check it then drop the old column and rename the new if your RDBMS allows this. What RDBMS are you using actually - I've used ROUND which might not be either supported or actually required but as you are storing as floating point makes me feel a bit safer.
Martin Smith
After I create a new column, how do I run this cast as the new column entries?
Daniel
MySQL? Oracle? Access? SQL Server?
Martin Smith
SQL Server..........
Daniel
+2  A: 
UPDATE table_name
SET new_date_column=CAST(CAST(CAST( old_date_column AS INT) AS VARCHAR) AS DATETIME)

This will update the new column

GEShafer
Thanks a lot, this is exactly what I'm looking for. Running now..
Daniel
No problem. Glad I could help. Mark as Answer please?Thank You,Gale
GEShafer
This actually gave me a datetime format of 'Jun 1 2'How can we get it to show 20100601?
Daniel
That is actually dependent on your settings in SQL Server because the actual data is stored the same way. If you wanna display it that way after a Query you can set the date format. You will find a lot of helpful info about date formats along with how to use them here: http://msdn.microsoft.com/en-us/library/ms187819.aspx
GEShafer