Reference > Functions > String

Left

  • 2005, 2008, 2012, 2014, 2016, 2017, 2019, 2022

Left String Function

Use the Left function to return the start of a string with the specified number of characters.

Syntax

db.fx.Left({expression}, {character_count})

Arguments

expression
The value to take beginning characters from.
character_count
The number of characters to take from `expression`.

Returns

string or string?
(depending on the nullability of `expression` and `character_count`)

Examples

Select Statement

Select the first letter of city

IEnumerable<string> result = db.SelectMany(
		db.fx.Left(dbo.Address.City, 1)
	)
	.From(dbo.Address)
	.Execute();

Where Clause

Select address ids where the city name starts with "D"

IEnumerable<int> result = db.SelectMany(
		dbo.Address.Id
	)
	.From(dbo.Address)
    .Where(db.fx.Left(dbo.Address.City, 1) == "D")
	.Execute();

Order By Clause

Select a list of persons, ordered by their initials.

IEnumerable<Person> persons = db.SelectMany<Person>()
    .From(dbo.Person)
    .OrderBy(
        db.fx.Left(dbo.Person.FirstName, 1),
        db.fx.Left(dbo.Person.LastName, 1)
    )
    .Execute();

Group By Clause

Select a list of data for persons, grouped by the initial of their last name.

IEnumerable<dynamic> values = db.SelectMany(
        db.fx.Count().As("count"),
        dbo.Person.YearOfLastCreditLimitReview,
        db.fx.Left(dbo.Person.LastName, 1).As("last_initial")
    )
    .From(dbo.Person)
    .GroupBy(
        dbo.Person.YearOfLastCreditLimitReview,
        db.fx.Left(dbo.Person.LastName, 1)
    )
    .Execute();

Having Clause

Select a list of data for persons, grouped by the initial of their last name having a first initial of last name in the last half of the alphabet.

IEnumerable<dynamic> addresses = db.SelectMany(
        db.fx.Count().As("count"),
        dbo.Person.YearOfLastCreditLimitReview,
        db.fx.Left(dbo.Person.LastName, 1).As("last_initial")
    )
    .From(dbo.Person)
    .GroupBy(
        dbo.Person.YearOfLastCreditLimitReview,
        db.fx.Left(dbo.Person.LastName, 1)
    )
    .Having(
        db.fx.Left(dbo.Person.LastName, 1) > "M"
    )
    .Execute();
Previous
Concat

© 2024 dbExpression. All rights reserved.