Skip to content
Release: Australia · Updated: 2026-03-10 · Official documentation · View source

Supported SQL functions

Common SQL functions used in SQL API for querying and analyzing incident data.

ServiceNow supports a variety of SQL functions for querying and analyzing data in tables such as the incident table. This abbreviated list covers common SQL functions organized by category, with sample use cases and queries focused on incident management scenarios.

Note: The query engine only supports INNER and LEFT OUTER joins currently.

CategoryFunction NameSample Use CaseSample Query
Aggregate FnAVGCalculate average priority level of resolved incidents to measure severity trendsSELECT AVG(CAST(priority AS FLOAT)) AS avg_priority_level FROM incident WHERE state = 7;
Aggregate FnCOUNTCount total open incidents by priority levelSELECT priority, COUNT (*) AS incident_count FROM incident WHERE state IN (1,2,3) GROUP BY priority;
Aggregate FnSUMCalculate total number of updates across all P1 incidentsSELECT SUM(sys_mod_count) AS total_updates FROM incident WHERE priority = 1;
Aggregate FnMAXFind the highest priority value in open incidentsSELECT MAX(priority) AS highest_priority FROM incident WHERE state IN (1,2,3);
ClausesCASECategorize incidents by priority for dashboard visualizationSELECT number, CASE WHEN priority = 1 THEN 'Critical' WHEN priority = 2 THEN 'High' WHEN priority = 3 THEN 'Medium' ELSE 'Low' END AS priority_label FROM incident WHERE state IN (1,2,3);
ClausesTOPIdentify top 10 most modified incidents for quality reviewSELECT TOP 10 number, sys_mod_count FROM incident ORDER BY sys_mod_count DESC;
ClausesGROUP BYAnalyze incident volume by category for trendingSELECT category, COUNT(*) AS count FROM incident WHERE state IN (1,2,3) GROUP BY category;
ClausesHAVINGFind assignment groups with more than 50 open incidents for capacity planningSELECT assignment_group , COUNT(*) AS open_count FROM incident WHERE state IN (1,2,3) GROUP BY assignment_group HAVING COUNT(*) > 50;
ClausesLEFT JOINList incidents with assigned user details for team performance reportSELECT i.number, i.priority, u.name AS assigned_to, u.department FROM incident i LEFT JOIN sys_user u ON i.assigned_to = u.sys_id WHERE i.state IN (1,2,3);
ClausesORDER BY ASCRetrieve oldest incidents by opened date for escalation reviewSELECT number, short_description, opened_at FROM incident WHERE state IN (1,2,3) ORDER BY opened_at ASC;
ClausesDISTINCTGet unique list of categories from recent incidentsSELECT DISTINCT category FROM incident WHERE state IN (1,2,3);
ClausesSUBQUERY (FROM)Calculate average incident count per user from active assignmentsSELECT AVG(incident_count) AS avg_per_user FROM (SELECT assigned_to, COUNT(*) AS incident_count FROM incident WHERE assigned_to IS NOT NULL GROUP BY assigned_to) AS user_stats;
ClausesSUBQUERY (WHERE)Find incidents assigned to active IT Support usersSELECT number, short_description, assigned_to FROM incident WHERE assigned_to IN (SELECT sys_id FROM sys_user WHERE department = 'IT Support' AND active = 1) AND state IN (1,2,3);
ClausesUNIONCombine high priority and unassigned incidents for urgent action listSELECT number, 'P1-Critical' AS reason, assigned_to, priority, state FROM incident WHERE priority = 1 AND state IN (1,2,3) UNION SELECT number, 'Unassigned' AS reason, assigned_to, priority, state FROM incident WHERE assigned_to IS NULL AND state IN (1,2,3);
DateTime FNdate_partAnalyze incident creation patterns by year for historical trendingSELECT date_part('year', opened_at) AS year, COUNT(*) AS incidents FROM incident GROUP BY date_part('year', opened_at) ORDER BY date_part('year', opened_at);
DateTime FNdate_truncGroup incidents by month for executive monthly trend reportSELECT date_trunc('month', opened_at) AS month, COUNT(*) AS total FROM incident GROUP BY month ORDER BY month;
Numeric FNABSCalculate absolute difference between priority values for normalizationSELECT number, ABS(priority - 3) AS priority_deviation FROM incident WHERE state IN (1,2,3);
Numeric FNCEILINGRound up priority division for weighted scoring calculationsSELECT number, priority, CEILING(CAST(sys_mod_count AS FLOAT) / 3) AS update_score FROM incident WHERE state IN (1,2,3);
Numeric FNFLOORCalculate floored average of modification counts for baseline metricsSELECT assignment_group, FLOOR(AVG(CAST(sys_mod_count AS FLOAT))) AS avg_updates FROM incident GROUP BY assignment_group;
OperatorsINFilter incidents for business-critical categories for executive dashboardSELECT number, category, priority, state FROM incident WHERE category IN ('Network', 'Database', 'Security', 'Application');
OperatorsIS NOT NULLFind incidents with configuration item assignments for CMDB analysisSELECT number, cmdb_ci, category, assignment_group FROM incident WHERE cmdb_ci IS NOT NULL;
OperatorsLIKESearch for password reset and access-related incidents for self-service analysisSELECT number, short_description, caller_id FROM incident WHERE short_description LIKE '%password%' OR short_description LIKE '%login%';
OperatorsNOT BETWEENIdentify incidents with unusual priority values for data quality auditSELECT number, priority, short_description FROM incident WHERE priority NOT BETWEEN 2 AND 4;
ServiceNow Specific FNDVDisplay human-readable reference field values in reportsSELECT number, DV(assignment_group) AS group_name, DV(assigned_to) AS assignee_name FROM incident WHERE state IN (1,2,3);
String FNCONCAT_WSCreate formatted incident summaries for external ticketing systemsSELECT concat_ws(' - ', number, category, short_description) AS formatted_summary FROM incident WHERE state = 7;
String FNLOWERStandardize category names for case-insensitive grouping and analysisSELECT LOWER(category) AS category_normalized FROM incident;
String FNREPLACETransform incident numbers for external system integrationSELECT REPLACE(number, 'INC', 'TICKET-') AS external_id, short_description FROM incident WHERE state = 7;
String FNSUBSTRExtract incident prefix for categorization and reportingSELECT number, SUBSTR(number, 1, 3) AS prefix, SUBSTR(number, 4, 20) AS sequence FROM incident;
String FNTRIMClean white space from descriptions for data quality improvementSELECT number, TRIM(short_description) AS clean_description FROM incident WHERE short_description IS NOT NULL;
Windows FNRANK()Rank incidents by number of updates to identify most frequently modified ticketsSELECT number, sys_mod_count, assignment_group, RANK() OVER (ORDER BY sys_mod_count DESC) AS modification_rank FROM incident WHERE assignment_group IS NOT NULL;

Parent Topic:Getting started with ServiceNow SQL API