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 Function | Alias |
---|---|
Abs | Absolute |
ACos | Arccosine |
ASin | Arccosine |
ATan | Arctangent |
Ceiling | Ceiling |
Cos | Cosine |
Cot | Cotangent |
Exp | Exponential |
Floor | Floor |
Log | Log |
Rand | Random |
Round | Round |
Sin | Sine |
Sqrt | Square Root |
Square | Square |
Tan | Tangent |
Aggregate
Microsoft SQL Server Function | Alias |
---|---|
Avg | Average |
Count | Count |
Max | Maximum |
Min | Minimum |
StDev | Standard Deviation |
StDevP | Population Standard Deviation |
Sum | Sum |
Var | Variance |
VarP | Population Variance |
Conversion
Microsoft SQL Server Function | Alias |
---|---|
Cast | Cast |
Date and Time
Microsoft SQL Server Function | Alias |
---|---|
Current_Timestamp | Current Timestamp |
DateAdd | Date Add |
DateDiff | Date Diff |
DatePart | Date Part |
GetDate | Get Date |
GetUtcDate | Get UTC Date |
SysDateTime | System Date and Time |
SysDateTimeOffset | System Date Time with Offset |
SysUtcDateTime | System UTC Date and Time |
String
Microsoft SQL Server Function | Alias |
---|---|
CharIndex | Character Index |
Concat | String Concatenation |
Left | Left |
Len | Length |
LTrim | Left Trim |
PatIndex | Pattern Index |
Replace | Replace |
Right | Right |
RTrim | Right Trim |
Substring | Substring |
Soundex | Soundex |
Trim | Trim |
Expressions
Microsoft SQL Server Function | Alias |
---|---|
Coalesce | Coalesce |
IsNull | Is Null |
System
Microsoft SQL Server Function | Alias |
---|---|
NewId | New Uniqueidentifier |