Reference > Functions > String

Concat

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

Concat String Function

Use the Concat function to join two or more string values.

Syntax

db.fx.Concat({first_expression}, {second_expression}[, ...{n-expression}])

Arguments

first_expression
The value to search for in `expression`.
second_expression
Value to append to `first_expression`.
n-expression
Additional expressions to append.

Returns

string

Examples

Select Statement

Select the concatenation of city and state for addresses

IEnumerable<string> result = db.SelectMany(
		db.fx.Concat(dbo.Address.City, ", ", db.fx.Cast(dbo.Address.State).AsVarChar(2))
	)
	.From(dbo.Address)
	.Execute();

Where Clause

Select address id's where line2 has a value.

IEnumerable<int> result = db.SelectMany(
		dbo.Address.Id
	)
	.From(dbo.Address)
    .Where(db.fx.Concat(dbo.Address.Line1, dbo.Address.Line2) != dbo.Address.Line1)
	.Execute();

Order By Clause

Select a list of addresses, ordered by the concatenation of city and state.

IEnumerable<Address> addresses = db.SelectMany<Address>()
    .From(dbo.Address)
    .OrderBy(db.fx.Concat(dbo.Address.City, ", ", db.fx.Cast(dbo.Address.State).AsVarChar(2)))
    .Execute();

Group By Clause

Select a list of address values grouped by address type and the concatenation of city and state.

IEnumerable<dynamic> values = db.SelectMany(
        dbo.Address.AddressType,
        db.fx.Concat(dbo.Address.City, ", ", db.fx.Cast(dbo.Address.State).AsVarChar(2)).As("formatted_city_state")
    )
    .From(dbo.Address)
    .GroupBy(
        dbo.Address.AddressType,
        db.fx.Concat(dbo.Address.City, ", ", db.fx.Cast(dbo.Address.State).AsVarChar(2))
    )
    .Execute();

Having Clause

Select a count of addresses grouped by address type and the concatenation of city and state, having a city that ends in "y" and a state that begins with "A".

IEnumerable<dynamic> values = db.SelectMany(
        db.fx.Count().As("count"),            
        dbo.Address.AddressType
    )
    .From(dbo.Address)
    .GroupBy(
        dbo.Address.AddressType,
        db.fx.Concat(dbo.Address.City, ", ", db.fx.Cast(dbo.Address.State).AsVarChar(2))
    )
    .Having(
        db.fx.Concat(dbo.Address.City, ", ", db.fx.Cast(dbo.Address.State).AsVarChar(2)).Like("%y, A%")
    )
    .Execute();
Previous
CharIndex

© 2024 dbExpression. All rights reserved.