0

as the title states, when creating a table, when definining an variable + datatype like:

CREATE TABLE ExampleTable{
ID INTEGER,
NAME VARCHAR(200),
Integerandfloat 
}

Question: You can define a variable as integer or as float etc. however, is there a datatype that can hold both values, integer as well as a float number ?

1
  • 1
    A column in SQL is of one datatype, only NoSQL could accommodate more datatypes in the same column Commented Oct 22, 2020 at 9:31

2 Answers 2

1

Some databases support variant data types that can have an arbitrary type. For instance, SQL Server has sql_variant.

Most databases also allow you to create your own data type (using create type). However, the power of that functionality depends on the database.

For the choice between a float and an integer, there isn't much choice. An 8-byte floating point representation covers all 4-byte integers, so you can just use a float. However, float is generally not very useful in relational databases. Fixed-point representations (numeric/decimal) are more common and might also do what you want.

Sign up to request clarification or add additional context in comments.

Comments

1

Just store it using float.

Think in this way: you have two variables, one integer type (let's call it i) and another float type (let's call it f). If you do:

i = 0.55
RESULT -> i = 0

But if you have:

f = 0.55
RESULT -> f = 0.55

In this way you can store in f also integer value:

f = 1
RESULT -> f = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.