Core Concepts > Advanced Queries

Database Functions

Database functions can be used in building queries by using fx. If you're using dbExpression in static mode, fx is a property of the db database accessor (or the name of the database accessor if it was overridden in scaffolding configuration). If you're using dbExpression with dependency injection, fx is a property of the instance (representing the database accessor) injected into your service.

Use the fx property of the database accessor to use database functions

When using dbExpression in static mode:

public string GetSomeValue()
{
    return db.SelectOne(
            db.fx.Cast(dbo.Person.Id).AsVarChar(20)
        )
        .From(dbo.Person)
        .Execute();
}

When using dbExpression with dependency injection:

...

private readonly MyDatabase _myDb;
public SomeService(MyDatabase myDb)
{
    _myDb = myDb;
}

public string GetSomeValue()
{
    return _myDb.SelectOne(
            _myDb.fx.Cast(dbo.Person.Id).AsVarChar(20)
        )
        .From(dbo.Person)
        .Execute();
}

Supported Functions

dbExpression supports many of the native SQL functions provided by Microsoft SQL Server. The following tables list all of the functions dbExpression supports, grouped by general category.

Mathematical

Microsoft SQL Server FunctionAlias
AbsAbsolute
ACosArccosine
ASinArccosine
ATanArctangent
CeilingCeiling
CosCosine
CotCotangent
ExpExponential
FloorFloor
LogLog
RandRandom
RoundRound
SinSine
SqrtSquare Root
SquareSquare
TanTangent

Aggregate

Microsoft SQL Server FunctionAlias
AvgAverage
CountCount
MaxMaximum
MinMinimum
StDevStandard Deviation
StDevPPopulation Standard Deviation
SumSum
VarVariance
VarPPopulation Variance

Conversion

Microsoft SQL Server FunctionAlias
CastCast

Date and Time

Microsoft SQL Server FunctionAlias
Current_TimestampCurrent Timestamp
DateAddDate Add
DateDiffDate Diff
DatePartDate Part
GetDateGet Date
GetUtcDateGet UTC Date
SysDateTimeSystem Date and Time
SysDateTimeOffsetSystem Date Time with Offset
SysUtcDateTimeSystem UTC Date and Time

String

Microsoft SQL Server FunctionAlias
CharIndexCharacter Index
ConcatString Concatenation
LeftLeft
LenLength
LTrimLeft Trim
PatIndexPattern Index
ReplaceReplace
RightRight
RTrimRight Trim
SubstringSubstring
SoundexSoundex
TrimTrim

Expressions

Microsoft SQL Server FunctionAlias
CoalesceCoalesce
IsNullIs Null

System

Microsoft SQL Server FunctionAlias
NewIdNew Uniqueidentifier
Previous
Advanced Queries

© 2024 dbExpression. All rights reserved.