Because it is not possible to convert a type smalldatetime
for int
. Imagine telling SQL "convert 01/01/2019 to an integer," how to do that? How should it convert? Divide 1 by 1 and then by 2019? He doesn’t know exactly what to do in this conversion.
To varchar
works because it is a text, so it is possible to store the data as it is, with everything that there is besides numbers (bars, two-points, etc).
What can be done is, change to varchar
, make a update
format correctly only with numbers, and then change again to int
Assuming your table is called "Test" and the column smaldatetime
if you call yourself "field", you could do so:
- Create a new int column
- Update this column by converting the date (I used Function
datediff
based on this other question: Convert smalldatetime to Unix timestamp
- Remove the old column
- Rename new to "field":
.
alter table teste
add campo_int int;
update teste
set campo_int = datediff(ss, '19700101', campo);
alter table teste
drop column campo;
You can see an example working here: sqlfiddle
if you have any questions about how to convert, tell me what format the date will be in full and I can help with sql
– Ricardo Pontual
@Ricardo Pontual the date will be in the format of Unix Timestamp. I am converting everything to Varchar and then to whole, if there is any way to make this conversion faster thank you.
– Levi