SQL datalength and Len distinguish usage

Source: Internet
Author: User

Original: SQL datalength and Len distinguished usage

Len (string_expression) parameter: The string to be evaluated

Len () function
The Len function returns the length of the value in the Text field.

SQL Len () syntax
Select Len (column_name) from table_name

We want to get the length of the value in the City column.

We use the following SQL statement:

Select Len (city) as lengthofaddress from persons

Returns the number of characters in the specified string expression that does not contain trailing spaces.
If the data type of expression is varchar (max), nvarchar (max), or varbinary (max), it is bigint; otherwise, int.
The ntext field cannot be evaluated


DATALENGTH (expression) parameter: An expression of any data type

Grammar
Datalength (Expre (www.111cn.net) ssion)

Parameters
Expression

An expression of any type.

return type
Int

Use ADVENTUREWORKS2008R2;
Go
Select length = datalength (name), name
From Production.Product
Order BY name;
Go


Returns the number of bytes used to represent any expression.
If the data type of expression is varchar (max), nvarchar (max), or varbinary (max) data type, bigint is returned, otherwise int is returned.
For varchar, varbinary, text, image, nvarchar, and ntext data types, datalength is especially useful because these data types can store variable-length data. Www.111cn.net
The result of NULL datalength is null.
From:http://www.111cn.net/database/mssqlserver/37823.htm

SQL datalength and Len distinguish usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.