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 NVARCHAR(Max) Data Type

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

NVARCHAR(Max) is used to store very large character data.

NVARCHAR(Max) can hold as much as 2GB of Unicode character data.

The word NVARCHAR stands for national varying character.

Example

#

A table with 2 VARCHAR(Max) columns.

CREATE TABLE DemoTable  
( 
  Id INT IDENTITY, 
  FullName NVARCHAR(100),
  Subject NVARCHAR(Max),
  Message NVARCHAR(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 Johnson 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 NVARCHAR(Max).

NVARCHAR(Max)

More Examples

NVARCHAR(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(NVARCHAR(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 NVARCHAR(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.