Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What SQL-server function can I use to get the character or byte length of a nvarchar(max) column?

Tags:

sql

sql-server

I have a column which is of type nvarchar(max). How do I find the length of the string (or the number of bytes) for the column for each row in the table?

like image 779
Jim Avatar asked Aug 31 '08 02:08

Jim


People also ask

What is Max nvarchar in SQL Server?

NVARCHAR (max) Code language: SQL (Structured Query Language) (sql) In this syntax, max is the maximum storage size in bytes which is 2^31-1 bytes (2 GB). In general, the actual storage size in bytes of a NVARCHAR value is two times the number of characters entered plus 2 bytes.

How to declare a nvarchar column in SQL Server?

Another way to declare a NVARCHAR column is to use the following syntax: In this syntax, max is the maximum storage size in bytes which is 2^31-1 bytes (2 GB). In general, the actual storage size in bytes of a NVARCHAR value is two times the number of characters entered plus 2 bytes.

What is the difference between nvarchar and VARCHAR?

Generally it is the same as for varchar really. The number is still the maximum number of characters not the data length. nvarchar(100) allows 100 characters (which would potentially consume 200 bytes in SQL Server).

What is the default length of a nvarchar column?

In this syntax, n defines the string length that ranges from 1 to 4,000. If you don’t specify the string length, its default value is 1. Another way to declare a NVARCHAR column is to use the following syntax:


2 Answers

SELECT LEN(columnName) AS MyLength FROM myTable

like image 101
Brian R. Bondy Avatar answered Sep 30 '22 01:09

Brian R. Bondy


If you want to find out the max there should be a way for you to get the schema of the table. Normally you can do something like SHOW COLUMNS in SQL or a DESCRIBE style command. In a mysql shell that can be shortened to:

desc tablename;

Then if you want to determine the length of a string there is normally a function like LENGTH (for bytes) or CHAR_LENGTH (for characters).

SELECT *, LENGTH(fieldname) AS len FROM tablename
like image 40
Joseph Pecoraro Avatar answered Sep 30 '22 03:09

Joseph Pecoraro