Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count the number of characters in SQL server ntext (i.e. memo) field in an access query?

I want to write an access query to count the characters in an ntext field in a linked SQL server table.

In SQL server, I would just use this command (which wont work in access):

 select datalength(nTextFieldName) //this command works on sql server but not in access 

In access, I can only find the len command, which wont work on ntext fields:

select len(nTextFieldName) // access says nText is not a valid argument to this function.

Googling around, I've found a bunch of posts saying to use len, which is giving me an error.

What is the command?

like image 839
bernie2436 Avatar asked Apr 12 '12 18:04

bernie2436


People also ask

How do I count characters in a SQL query?

LEN() function calculates the number of characters of an input string, excluding the trailing spaces. It is an expression that can be a constant, variable, or column of either character or binary data. Returns : It returns the number of characters of an input string, excluding the trailing spaces.

How do I get the length of a Ntext field in SQL?

Solution. To get the length of an ntext field, use DATALENGTH() instead of LEN() . Note that DATALENGTH will return the number of bytes, not the number of characters in the string. Each character in an ntext field is 2 bytes, so you need to take this into account when writing your query.

How can you capture the length of a column when it is a text Ntext and or image data type?

Solution. In addition to the LEN() function, SQL Server also has a DATALENGTH() function. This function can be used on all data types in your table. That's all there is to it.


1 Answers

ntext type doesn't work with LEN. This specific type as well as a few others are deprecated:

ntext, text, and image data types will be removed in a future version of Microsoft SQL 
Server. Avoid using these data types in new development work, and plan to modify applications 
that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead. For more 
information, see Using Large-Value Data Types.

The best way to handle this is to convert/cast the datatype to one that works such as varchar(max)/nvarchar(max) and only then get the LEN.

SELECT LEN(CAST(nTextFieldName As nvarchar(max)))

like image 54
JonH Avatar answered Sep 23 '22 19:09

JonH