Dofactory.com
Dofactory.com
Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

SQL VARCHAR(Max) Data Type

The VARCHAR(Max) data type stores variable-length character strings.

VARCHAR(Max) is used to store very large, i.e. Max, character data.

VARCHAR(Max) can hold as much as 2GB of ASCII character data.

The word VARCHAR stands for varying character.

Example

#

A table with 2 VARCHAR(Max) columns.

CREATE TABLE DemoTable  
( 
  Id INT IDENTITY, 
  FullName VARCHAR(100),
  Subject VARCHAR(max),
  Message VARCHAR(max)
);
GO  

INSERT INTO DemoTable VALUES ('Harold Smith', 'Technical Support', 'Hi, please help me fix ...');  
INSERT INTO DemoTable VALUES ('Robert Wang', 'After Sales', 'Greetings! I need to know the details ...');  
INSERT INTO DemoTable VALUES ('Janice Lopez', 'After Sales', 'Are you also opening ...');
INSERT INTO DemoTable VALUES ('Kelly Wilson', 'Customer Support', 'Can you help me find the ...'); 
INSERT INTO DemoTable VALUES ('Grace Taylor', 'Technical Support', NULL); 
GO  

SELECT * FROM DemoTable;
GO

DROP TABLE DemoTable;
GO

Very large character strings can be stored in Subject and Message.

Result:  5 records
Id FullName Subject Message
1 Harold Smith Technical Support Hi, please help me fix ...
2 Robert Wang After Sales Greetings! I need to know the details ...
3 Janice Lopez After Sales Are you also opening ...
4 Kelly Wilson Customer Support Can you help me find the ...
5 Grace Taylor Technical Support NULL

Syntax

#

Syntax of VARCHAR(Max).

VARCHAR(Max)

More Examples

VARCHAR(Max) with CONVERT MONEY VALUE

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: List monthly sales for the year 2013 with properly formatted $ amounts.
SELECT MONTH(OrderDate) AS [Month],
       '$' + CONVERT(VARCHAR(Max), SUM(CAST(TotalAmount AS MONEY)), 3) AS 'Total Sales'
  FROM [Order]
 WHERE YEAR(OrderDate) = 2013
 GROUP BY MONTH(OrderDate)
 ORDER BY MONTH(OrderDate)

Storing values in VARCHAR(Max) is overkill in this situation. A smaller VARCHAR or CHAR type would suffice.

Result:  12 records
Month Total Sales
1 $ 66,692.80
2 $ 41,207.20
3 $ 39,979.90
4 $ 55,699.39
5 $ 56,823.70

You may also like



Last updated on Dec 21, 2023

Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.