cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Data type length enforcement

elgeo
Valued Contributor II

Hello. Is there a way to enforce the length of a column in SQL? For example that a column has to be exactly 18 characters? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions

Kaniz_Fatma
Community Manager
Community Manager

Hi @ELENI GEORGOUSI​ , Yes, enforcing a column to have precisely several characters, not less than a number, in Databricks SQL is possible.

In Databricks SQL, you can use a constraint to ensure that a column has a specific length.

After creating the table, you will have to use the ADD CONSTRAINT parameter in the ALTER TABLE.

For example, you can create a table with a STRING column and specify a

constraint to ensure that the column has exactly 34 characters:

%sql
 
CREATE TABLE new_table (
  iban STRING
);

image 

%sql
 
ALTER TABLE new_table ADD CONSTRAINT IBAN1 CHECK (length(IBAN) = 34);

imageThis will ensure that any value inserted into the iban column has exactly 34 characters.

imageimageimage 

View solution in original post

6 REPLIES 6

Kaniz_Fatma
Community Manager
Community Manager

Hi @ELENI GEORGOUSI​,

In SQL, you can enforce the length of a column by specifying a maximum size for the column in the table definition using the appropriate data type.

For example, if you want to enforce a maximum length of 50 characters for a column named "name," you can define it as a VARCHAR(50) data type when creating the table. The syntax for creating a table with a column of this type would be as follows:

%sql
 
CREATE TABLE my_table1 (
  id INT,
  name VARCHAR(50),
  age INT
);

In this example, the "name" column is defined as a VARCHAR(50) data type, meaning it can store up to 50 characters.

You will receive an error if you try inserting a value longer than 50 characters into this column.

image.png 

elgeo
Valued Contributor II

Hi @Kaniz Fatma​. Thank you for your feedback. However, my question is if we can enforce a column to have exactly a number of characters, not less than a number. For example an iban needs to have exactly 34 chars, not less than 34 not more than 34.

Kaniz_Fatma
Community Manager
Community Manager

Hi @ELENI GEORGOUSI​ , Yes, enforcing a column to have precisely several characters, not less than a number, in Databricks SQL is possible.

In Databricks SQL, you can use a constraint to ensure that a column has a specific length.

After creating the table, you will have to use the ADD CONSTRAINT parameter in the ALTER TABLE.

For example, you can create a table with a STRING column and specify a

constraint to ensure that the column has exactly 34 characters:

%sql
 
CREATE TABLE new_table (
  iban STRING
);

image 

%sql
 
ALTER TABLE new_table ADD CONSTRAINT IBAN1 CHECK (length(IBAN) = 34);

imageThis will ensure that any value inserted into the iban column has exactly 34 characters.

imageimageimage 

elgeo
Valued Contributor II

Thank you @Kaniz Fatma​. This is exactly what I was looking for.

Kaniz_Fatma
Community Manager
Community Manager

Hi @ELENI GEORGOUSI​,

Awesome. I'm glad you got the solution. Thank you for choosing the best answer. Keep learning!

databricks31
New Contributor II

we are facing similar issues while write into adls location delta format, after that we created on top delta location unity catalog tables. below format of data type length should be possible to change spark sql supported ?

Azure SQL Spark             SQL supported ?
column1 nvarchar(5)   -->  column1 <how to set custome datatype length>
startdate datetime2 ----> startdate <how to set custome datatype length>
enddate datetime2(0) ---> enddate <how to set custome datatype length>

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!