Binding Sources: SQL

The sql object can be used to format field value literals according to the current database format. In the context of an AXVW, it can be used in the AXVW's data source (SQL query or restriction).

For examples with Oracle, Sybase, and SQL Server, see Binding Sources: SQL (with database examples)

Expression Description/Comments
${sql.as} If required, inserts the 'AS' keyword for assigning a table or field alias
${sql.convertToInt(expression)} Expression parameter is a field name or SQL expression.
${sql.convertToString(expression)} expression parameter is a field name or SQL expression.
${sql.datetime(isoDateLiteral,isoTimeLiteral)} Takes only literals as parameters.
${sql.dateAdd(datepart, number, date)}

Equivalent to DATEADD function in MSSQL. Values for datepart:

  • year: Add years
  • quarter: Add quarters
  • month: Add months
  • day: Add days
  • week: Add weeks
  • hour: Add hours
  • minute: Add minutes
  • second: Add seconds

date parameter is an expression, field name, or literal value. Literal values must be preceded with # symbol, '#2014-04-08'.

${sql.dateDiffInterval(datepart, date1, date2)}

Difference in periods between two dates. Values for datepart:

  • year: Difference in years between two dates
  • month: Difference in months between two dates
  • week: Difference in weeks between two dates
  • day: Difference in days between two dates
  • hour: Difference in hours between two dates
  • minute: Difference in minutes between two dates
  • second: Difference in seconds between two dates

date1 and date2 parameters are expressions, field names, and/or literal values. Literal values must be preceded with # symbol, '#2014-04-08'.

${sql.dateDiffCalendar(datepart, date1,date2)}

Equivalent to DATEDIFF function in MSSQL. Difference in calendar periods between two dates. Values for datepart: year: Difference in calendar years between two dates

  • quarter: Difference in calendar quarters between two dates
  • month: Difference in calendar months between two dates
  • week: Difference in calendar weeks between two dates
  • day: Difference in calendar days between two dates
  • hour: Difference in calendar hours between two dates

date1 and date2 parameters are expressions, field names, and/or literal values. Literal values must be preceded with # symbol, '#2014-04-08'.

The return value is positive if the “date1” field value is before the “date2” field value and negative if it is after the “date2” field value.

${sql.datePart(datepart, dateExpression)}

Equivalent to DATEPART functions in MSSQL and Oracle. Returns an integer. Values for datepart:

  • year: Returns the month of the year (1-12)
  • quarter: Returns the quarter of the year (1-4)
  • month: Returns the month of the year (1-12)
  • day: Returns the day of the month (1-31)
  • dayofyear: Returns the day of the year (1-366)
  • weekday: Returns the day of the week (1-7)
  • week: Returns the week of the year (1-53)
  • isoweek: Returns the ISO week of the year (1-53)
  • hour: Returns the hour (0-23) minute: Returns the minute of the hour (0-59)
  • second: Returns the second of the minute (0-59)

dateExpression is a field name or SQL expression that evaluates to a datetime result.

${sql.trim(expression)} TRIM function for each database engine. expression parameter is a field name or SQL expression.
value1${sql.concat}value2 Formats SQL expression that concatenates two values
${sql.currentDate} Formats current date
${sql.currentTime} Formats current time
${sql.currentTimestamp} Formats current timestamp
${sql.date(value)} Formats date literal
${sql.time(value)} Formats time literal
${sql.isNull(value, replaceWith)} Replaces NULL with the specified replacement value
${sql.timestamp(value)} Formats timestamp literal value in 'date' 'time' format (e.g. '\'YYYY-MM-DD\' \'HH:mm\'')
${sql.timestamp(dateField, timeField)} Formats timestamp SQL expression that concatenates two fields: date and time
${sql.yearOf(column)}

Formats SQL expression that returns the year of a value contained in specified column

${sql.yearQuarterOf(column)} Formats SQL expression that returns the year-quarter of a value contained in specified column
${sql.yearWeekOf(column)}

The ${sql.yearWeekOf} expression uses default calculation for the first week of the year on all databases. All Archibus applications use this calculation by default.

Example:

${sql.yearWeekOf('wr.date_requested')}

When desired, add-in managers can specify the ISO 8601 calculation using the optional weekType parameter:

  • ${sql.yearWeekOf('wr.date_requested', 'DEFAULT')} Same calculation as in the example above.
  • ${sql.yearWeekOf('wr.date_requested', 'ISO')}

Solution template view:

schema/ab-products/solutions/programming/binding-expr/ab-ex-binding-expr-year-week-of.axvw

${sql.yearMonthOf(column)} Formats SQL expression that returns the year-month of a value contained in specified column
${sql.yearMonthDayOf(column)} Formats SQL expression that returns the year-month-day of a value contained in specified column
${sql.daysBeforeCurrentDate(field)} Returns the number of days between today’s date and specified date field value. The number is positive if the field value is before today’s date and negative if it is after today’s date
${sql.daysBetween(fromField, toField)} Returns the number of days between two date fields. The number is positive if the “from” field value is before the “to” field value, and negative if it is after the “to” field value.

${sql.hoursBetween(fromDateColumn, fromTimeColumn, toDateColumn, toTimeColumn)}

Returns the number of hours between two date-time fields. The number is positive if the “from” field date-time value is before the “to” field date-time value, and negative if it is after the “to” field date-time value.

${sql.replaceZero(fieldName)}

Generates the CASE operator that:

  • Returns specified field value if it is not equal 0.
  • Returns a very large value (9999999999) if the field value is equal to 0.

Example: ${sql.replaceZero('bl.area_usable')}

The parameter is fieldName or sql expression.

   
${sql.replaceZero(fieldName, defaultValue)}

Generates the CASE operator that:

  • Returns specified field value if it is not equal 0.
  • Returns specified default value if the field value is equal to 0.

Example: ${sql.replaceZero('bl.area_usable', '1000000')}

The parameter is fieldName or sql expression.

${sql.vpaRestriction} Returns the VPA (Virtual Private Archibus) SQL restriction attached to the current user session. If the current user/role has no VPA, the expression will be resolved as (1=1).
${sql.getVpaRestrictionForTable(tableName)}

Returns the VPA (Virtual Private Archibus) SQL restriction attached to the current user session, for specified table. If the current user/role has no VPA for that table, the expression will be resolved as (1=1).

Example: ${sql.getVpaRestrictionForTable('property')}

${sql.literal(textValue)}

Literalizes a text value so that it can be used in an SQL query even when the text contains single quotes. The literalized value contains two single quotes instead of every single quote in the original text.

Example: ${sql.literal(user.employee.id)}

${sql.vpaRestriction}

Generates SQL WHERE clause from VPAs for current user/role applicable to the main table in current DataSource.

${sql.getVpaRestrictionForTable('table')}

Generates SQL WHERE clause from VPAs for current user/role applicable to the specified table in current DataSource.

table - name of main or standard table in the current DataSource for which to generate SQL.

${sql.getVpaRestrictionForTableAndRole('table','role')}

Generates SQL WHERE clause from VPAs applicable to the specified table in current DataSource. The role will be used by "forVpaGroups" VPAs (if current user/role has such restrictions) to generate "EXISTS (SELECT ...)" to restrict table using mapping tables; otherwise it will be ignored.

This expression is useful for system WFRs, who must suspend the user's VPA (e.g., a room reservation query that can see all conference rooms in the campus, even if the user does not have VPA access to all rooms).

Example: ${sql.getVpaRestrictionForTableAndRole('rm','Reservation User'}

This expression enables you to add roles and VPAs that you can manage centrally, but that application code can use to get access to specific groups of records as part of specific features (e.g., Room reservations).

The Admin assigns any needed VPA Group permissions to the role; this expression then creates an SQL statement that references those VPA groups.

  • table - name of main or standard table in the current DataSource for which to generate.
  • role - name of user role.

${sql.getVpaGroupsRestrictionForBridgeTable(table, bridgeTable)}

Generates "EXISTS (SELECT ...)" to restrict table using mapping tables vpa_groupstoroles, vpa_groupstousers, vpa_[bridgeTable] for the current user and role.

  • table - name of table to which access should be restricted.
  • bridgeTable - name of bridge table. The bridge table has to be referenced in foreign key of table.

${sql.getVpaGroupsRestrictionForMappingTable(table, vpaMappingTable, fieldName)}

Generates "EXISTS (SELECT ...)" to restrict table using mapping tables vpa_groupstoroles, vpa_groupstousers, vpaMappingTable for current user and role.

  • table - name of table to which access should be restricted.
  • vpaMappingTable - name of VPA mapping table.
  • fieldName - name of field in table and in vpaMappingTable.