SQL Server Data Types & Variables

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:00

SQL Numeric Data Types

SQL Numeric data types are used to store numeric values such as integers and decimals. They include INT for whole numbers and DECIMAL for precise decimal values.

Example

DECLARE @myInt INT = 42;
SELECT @myInt;

SQL Date and Time Data Types

SQL Date and Time data types are used to store date and time values. They include DATETIME for the date and time together and DATE for the date only.

Example

DECLARE @myDate DATETIME = '2023-10-07 15:30:00';
SELECT @myDate;

SQL Character and String Data Types

SQL Character and String data types are used to store text data. They include VARCHAR for variable-length strings and CHAR for fixed-length strings.

Example

DECLARE @myString VARCHAR(50) = 'Hello, SQL!';
SELECT @myString;

SQL Unicode Character and String Data Types

SQL Unicode Character and String data types are used for multilingual text. They include NVARCHAR for variable-length Unicode strings and NCHAR for fixed-length Unicode strings.

Example

-- Create a table to store Unicode text
CREATE TABLE Greetings (
  ID INT PRIMARY KEY,
  IndianGreeting NVARCHAR(100)
);


-- Insert an Indian greeting in English into the table INSERT INTO Greetings (ID, IndianGreeting) VALUES (1, N'Namaste, SQL Server!'); -- "Namaste" is a common Indian greeting-- Retrieve and display the Indian greeting from the table SELECT * FROM Greetings;

SQL Binary Data Types

SQL Binary data types store binary data, such as images or files. They include VARBINARY for variable-length binary data.

Example

DECLARE @myBinary VARBINARY(MAX) = 0x48656C6C6F;
SELECT @myBinary;

SQL Miscellaneous Data Types

SQL Miscellaneous data types include unique identifiers (UNIQUEIDENTIFIER) and SQL_VARIANT for storing values of various data types in a single column.

Example

DECLARE @myVariant SQL_VARIANT = 'OpenAI';
SELECT @myVariant;

DECLARE @local_variable

The DECLARE @local_variable statement is used to declare a local variable in SQL Server. Local variables are scoped to the batch or stored procedure where they are declared.

Example

DECLARE @myVariable INT;
SET @myVariable = 100;

SET @local_variable

The SET @local_variable statement assigns a value to a previously declared local variable in SQL Server.

Example

DECLARE @myVariable INT;
SET @myVariable = 42;

SELECT @local_variable

The SELECT @local_variable statement is used to retrieve and display the value stored in a local variable.

Example

DECLARE @myVariable INT;
SET @myVariable = 73;
SELECT @myVariable;
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this