简体   繁体   中英

How do I alter a column's datatype in SQL, but just for all rows after the column header?

I have a column of varchar(10) called TITLE. Except for the first row, which contains the column header, the rest of the column happens to be all integers so I wanted to change the datatype to int.

ALTER TABLE X
ALTER COLUMN TITLE int

I get an error when converting the first row, which is the column header: "Conversion failed when converting the varchar value 'TITLE' to data type int.

So, how do I convert the data type for all rows, except the column header?

The short answer is No , you cannot mix data types in a single SQL column. Data types need to be consistent for things like sorting, building indexes, etc.

You could possibly use another table to store various column headers, or another column in the same table.

Using a NoSQL solution such as MongoDB might be an approach, depending on the type of data you're storing. These solutions allow you to be a lot more flexible with the schema, which can even differ per document.

Nope sorry you cannot have multiple data types on the same column.

Data types

You don't. A column isn't a collection of independent variables that can each have their own type. Everything in a column has the same type. If you're trying to do this, then your schema isn't likely what it should be. If you post a little more detail, you can likely get some answers with an improved schema.

You can't mix'n'match data types within a column. You can use fuzzy data types like VarBinary or XML and interpret them as you please.

OTOH, you can use sp_addextendedproperty to store column titles and other extraneous bits of fluff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM