Databases 5 min read

How to Build INSERT Statements for Different Data Types in SQL

This article explains how to construct SQL INSERT statements for string, numeric, date, and boolean fields, demonstrates variable concatenation for dynamic values, and provides a comprehensive example that combines multiple data types into a single query.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
How to Build INSERT Statements for Different Data Types in SQL

The article begins by presenting a sample table mytable with fields username (string), age (numeric), birthday (date), marry (boolean), and leixing (string).

Inserting string values : When inserting a literal name such as "张红", the value must be enclosed in single quotes. Example:

strsql = "Insert into mytable (username) values ('张红')"

If the name is stored in a variable thename , concatenate the variable with the surrounding quotes:

strsql = "Insert into mytable (username) values ('" & thename & "')"

For multiple string fields (e.g., username and leixing ), separate the values with commas:

strsql = "Insert into mytable (username, leixing) values ('张红','学生')"

When both fields are variables ( thename and thetype ), use concatenation:

strsql = "Insert into mytable (username, leixing) values ('" & thename & "','" & thetype & "')"

Inserting numeric values : Numeric fields do not require quotes. For a fixed age of 12:

strsql = "Insert into mytable (age) values (12)"

If the age is stored in a variable theage :

strsql = "Insert into mytable (age) values (" & theage & ")"

Inserting date values : Dates are wrapped with # symbols (Access syntax) instead of single quotes. Example with a literal date:

strsql = "Insert into mytable (birthday) values (#1980-10-01#)"

Using a date variable thedate :

strsql = "Insert into mytable (birthday) values (#" & thedate & "#)"

Inserting boolean values : Booleans are written as True or False without quotes.

strsql = "Insert into mytable (marry) values (True)"

With a boolean variable themarry :

strsql = "Insert into mytable (marry) values (" & themarry & ")"

Comprehensive example : To insert a record with a string name and a numeric age:

strsql = "Insert into mytable (username, age) values ('张红',12)"

When both values are variables ( thename and theage ), concatenate them appropriately:

strsql = "Insert into mytable (username, age) values ('" & thename & "'," & theage & ")"

The article concludes with a tip: always surround string literals with single quotes in SQL statements, as omitting them can cause syntax errors and affect performance, especially for primary‑key fields of type string.

SQLdatabaseData TypesstringBooleanDateINSERTnumeric
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.