SQL: Is there a way to get the average number of characters for a field?
Is there a simple sql query that can help me to determine the average number of characters that a (text) database field has?
For instance my field is called "message". Id开发者_开发百科eally I would love to do something like this...
select average(characterlength(message)) from mydatabasetable
is this even possible through sql?
Thanks!
Edit
Original bad phrasing: In SQL Server, LEN is for varchar fields. For Text fields, try DATALENGTH
Correction because @gbn is right: LEN will not work with Text or NText datatypes. For TEXT, try Datalength.
End Edit
SELECT AVG(DATALENGTH(yourtextfield)) AS TEXTFieldSize
Edit - added
The above is for the TEXT datatype. For NTEXT, divide by 2.
select avg(length(fieldname)) from table
Though the answer could potentially differ depending on your RDBMS.
For MySQL:
SELECT AVG(CHAR_LENGTH(<column>)) AS avgLength FROM <table>
Retrieved from:
http://forums.devshed.com/database-management-46/average-length-field-38905.html
select avg(length(textfield)) from mytable;
Yes, it is possible. In SqlServer for example it would be:
SELECT AVG(LEN(Name)) FROM MyTable
select sum(len(theTextColumn)) / count(*) from theTable;
精彩评论