Reference > Functions > String
Len
- 2005
- 2008
- 2012
- 2014
- 2016
- 2017
- 2019
- 2022
- 2005, 2008, 2012, 2014, 2016, 2017, 2019, 2022
Len (Length) String Function
Use the Len
function to return the number of characters in a string expression.
Syntax
db.fx.Len({expression})
Arguments
- expression
- – The value to determine the length of.
Returns
long or long?
Microsoft SQL Server returns bigint if expression is of the varchar(max), varbinary(max), or nvarchar(max) data types; otherwise, int. dbExpression generally maps these to CLR types long
and int
respectively. As the dbExpression implementation for Len
works with the CLR type string
, there is no way to effect a different return type based on the length of the provided {expression}
method parameter. Therefore, Len
in dbExpression always returns long
or long?
.
Examples
Select Statement
Select the length of a person's last name.
IEnumerable<long> result = db.SelectMany(
db.fx.Len(dbo.Person.LastName)
)
.From(dbo.Person)
.Execute();
Where Clause
Select persons whose last name is longer than their first name.
IEnumerable<Person> persons = db.SelectMany<Person>()
.From(dbo.Person)
.Where(db.fx.Len(dbo.Person.LastName) > db.fx.Len(dbo.Person.FirstName))
.Execute();
Order By Clause
Select a list of persons, ordered descending by the length of their last name.
IEnumerable<Person> result = db.SelectMany<Person>()
.From(dbo.Person)
.OrderBy(db.fx.Len(dbo.Person.LastName).Desc())
.Execute();
Group By Clause
Select a list of address values grouped by address type and the length of the city field.
IEnumerable<dynamic> values = db.SelectMany(
dbo.Address.AddressType,
db.fx.Len(dbo.Address.City).As("city")
)
.From(dbo.Address)
.GroupBy(
dbo.Address.AddressType,
db.fx.Len(dbo.Address.City)
)
.Execute();
Having Clause
Select a list of address data, grouped by address type and city where the value of city has only 1 character.
IEnumerable<dynamic> addresses = db.SelectMany(
db.fx.Count().As("count"),
dbo.Address.AddressType,
dbo.Address.City
)
.From(dbo.Address)
.GroupBy(
dbo.Address.AddressType,
dbo.Address.City
)
.Having(
db.fx.Len(dbo.Address.City) == 1
)
.Execute();