Skip to main content

Operators & Functions

Master SuiteQL operators and functions for filtering, transforming, and manipulating data.


Comparison Operators

Basic Comparisons

OperatorDescriptionExample
=Equal tostatus = 'A'
!= or <>Not equal tostatus != 'Closed'
>Greater thanamount > 1000
<Less thanquantity < 10
>=Greater or equalbalance >= 0
<=Less or equalcreditlimit <= 50000

Test Case: Basic Comparisons

-- Find high-value customers (balance over $10,000)
SELECT
id,
companyname,
balance
FROM customer
WHERE balance > 10000
ORDER BY balance DESC

Sample Result:

idcompanynamebalance
105Enterprise Corp45000.00
203Global Tech28500.00
89Mega Industries15750.00

Logical Operators

AND, OR, NOT

-- AND: Both conditions must be true
SELECT id, companyname
FROM customer
WHERE isinactive = 'F'
AND balance > 1000

-- OR: Either condition can be true
SELECT id, companyname
FROM customer
WHERE category = 'Retail'
OR category = 'Wholesale'

-- NOT: Negates the condition
SELECT id, companyname
FROM customer
WHERE NOT isinactive = 'T'

Test Case: Complex Logic

-- Find active customers who are either:
-- 1. High value (balance > 10000), OR
-- 2. In California with any balance
SELECT
id,
companyname,
state,
balance
FROM customer
WHERE isinactive = 'F'
AND (
balance > 10000
OR (state = 'CA' AND balance > 0)
)
ORDER BY balance DESC
LIMIT 20

Explanation:

  • isinactive = 'F' - Must be active
  • Parentheses group OR conditions
  • Without parentheses, logic would be wrong

LIKE Operator (Pattern Matching)

Wildcards

WildcardMeaningExample
%Any characters (0 or more)'A%' matches "Apple", "A", "Amazing"
_Exactly one character'_at' matches "Cat", "Bat", "Hat"

Test Cases: Pattern Matching

-- Starts with "Tech"
SELECT companyname FROM customer
WHERE companyname LIKE 'Tech%'
-- Matches: "TechCorp", "Technology Solutions", "Tech"

-- Ends with "Inc"
SELECT companyname FROM customer
WHERE companyname LIKE '%Inc'
-- Matches: "Acme Inc", "Global Inc"

-- Contains "Solutions"
SELECT companyname FROM customer
WHERE companyname LIKE '%Solutions%'
-- Matches: "Tech Solutions", "Solutions Corp", "Best Solutions Inc"

-- Exactly 5 characters
SELECT companyname FROM customer
WHERE companyname LIKE '_____'
-- Matches: "Acme1", "Tech5" (exactly 5 chars)

-- Second character is 'a'
SELECT companyname FROM customer
WHERE companyname LIKE '_a%'
-- Matches: "Walmart", "Target", "Samsung"

IN Operator

Multiple Value Matching

-- Instead of multiple ORs
SELECT id, tranid, type
FROM transaction
WHERE type IN ('SalesOrd', 'CustInvc', 'CashSale')

-- Equivalent to:
-- WHERE type = 'SalesOrd' OR type = 'CustInvc' OR type = 'CashSale'

Test Case: IN with Subquery

-- Find customers who have placed orders
SELECT id, companyname
FROM customer
WHERE id IN (
SELECT entity
FROM transaction
WHERE type = 'SalesOrd'
)

NOT IN

-- Find customers who have NOT placed orders
SELECT id, companyname
FROM customer
WHERE id NOT IN (
SELECT entity
FROM transaction
WHERE type = 'SalesOrd'
)

BETWEEN Operator

Range Queries

-- Amount between 1000 and 5000 (inclusive)
SELECT id, tranid, amount
FROM transaction
WHERE amount BETWEEN 1000 AND 5000

-- Equivalent to:
-- WHERE amount >= 1000 AND amount <= 5000

Test Case: Date Range

-- Orders from Q1 2024
SELECT
id,
tranid,
trandate,
amount
FROM transaction
WHERE type = 'SalesOrd'
AND trandate BETWEEN TO_DATE('2024-01-01', 'YYYY-MM-DD')
AND TO_DATE('2024-03-31', 'YYYY-MM-DD')
ORDER BY trandate

NULL Handling

IS NULL / IS NOT NULL

-- Find customers without email
SELECT id, companyname
FROM customer
WHERE email IS NULL

-- Find customers with email
SELECT id, companyname, email
FROM customer
WHERE email IS NOT NULL

NVL Function (NULL Value)

-- Replace NULL with default value
SELECT
id,
companyname,
NVL(email, 'no-email@example.com') AS email,
NVL(phone, 'N/A') AS phone
FROM customer

COALESCE (First Non-NULL)

-- Use first available contact method
SELECT
id,
companyname,
COALESCE(email, phone, fax, 'No Contact') AS primary_contact
FROM customer

Test Case: NULL Handling

-- Customer contact report with fallbacks
SELECT
id,
companyname,
NVL(email, 'Not Provided') AS email,
NVL(phone, 'Not Provided') AS phone,
CASE
WHEN email IS NOT NULL THEN 'Email'
WHEN phone IS NOT NULL THEN 'Phone'
ELSE 'No Contact'
END AS preferred_contact
FROM customer
WHERE isinactive = 'F'
LIMIT 20

Sample Result:

idcompanynameemailphonepreferred_contact
1Acme Corpacme@test.com555-1234Email
2Beta IncNot Provided555-5678Phone
3Gamma LtdNot ProvidedNot ProvidedNo Contact

String Functions

Common String Functions

FunctionDescriptionExampleResult
UPPER(s)UppercaseUPPER('hello')'HELLO'
LOWER(s)LowercaseLOWER('HELLO')'hello'
INITCAP(s)Title CaseINITCAP('hello world')'Hello World'
LENGTH(s)String lengthLENGTH('hello')5
TRIM(s)Remove spacesTRIM(' hi ')'hi'
LTRIM(s)Remove left spacesLTRIM(' hi')'hi'
RTRIM(s)Remove right spacesRTRIM('hi ')'hi'
SUBSTR(s,start,len)SubstringSUBSTR('hello',1,3)'hel'
CONCAT(a,b)ConcatenateCONCAT('Hello',' World')'Hello World'
||Concatenate'Hello' || ' World''Hello World'
REPLACE(s,old,new)Replace textREPLACE('hello','l','L')'heLLo'
INSTR(s,sub)Find positionINSTR('hello','l')3

Test Case: String Manipulation

-- Format customer names and extract info
SELECT
id,
UPPER(companyname) AS company_upper,
INITCAP(companyname) AS company_title,
LENGTH(companyname) AS name_length,
SUBSTR(companyname, 1, 10) AS short_name,
CONCAT(companyname, ' - ', NVL(city, 'Unknown')) AS full_display
FROM customer
WHERE LENGTH(companyname) > 5
LIMIT 10

Test Case: Parsing Email Domain

-- Extract email domain
SELECT
id,
companyname,
email,
SUBSTR(email, INSTR(email, '@') + 1) AS email_domain
FROM customer
WHERE email IS NOT NULL
AND INSTR(email, '@') > 0
LIMIT 20

Sample Result:

idcompanynameemailemail_domain
1Acme Corpinfo@acme.comacme.com
2Tech Incsales@tech.iotech.io

Numeric Functions

Common Numeric Functions

FunctionDescriptionExampleResult
ROUND(n,d)Round to d decimalsROUND(123.456, 2)123.46
TRUNC(n,d)Truncate to d decimalsTRUNC(123.456, 2)123.45
CEIL(n)Round upCEIL(123.1)124
FLOOR(n)Round downFLOOR(123.9)123
ABS(n)Absolute valueABS(-123)123
MOD(n,m)Modulo (remainder)MOD(10, 3)1
POWER(n,p)PowerPOWER(2, 3)8
SQRT(n)Square rootSQRT(16)4

Test Case: Financial Calculations

-- Calculate invoice details with rounding
SELECT
id,
tranid,
amount,
ROUND(amount * 0.1, 2) AS tax_amount,
ROUND(amount * 1.1, 2) AS total_with_tax,
FLOOR(amount / 100) AS hundreds,
MOD(CAST(amount AS INTEGER), 100) AS remainder
FROM transaction
WHERE type = 'CustInvc'
AND amount > 0
LIMIT 10

Sample Result:

idtranidamounttax_amounttotal_with_taxhundredsremainder
101INV-0011234.56123.461358.021234
102INV-002567.8956.79624.68567

Date Functions

Current Date/Time

FunctionDescription
SYSDATECurrent date and time
CURRENT_DATECurrent date
CURRENT_TIMESTAMPCurrent timestamp

Date Conversion

-- String to Date
TO_DATE('2024-01-15', 'YYYY-MM-DD')
TO_DATE('15-Jan-2024', 'DD-Mon-YYYY')
TO_DATE('01/15/2024', 'MM/DD/YYYY')

-- Date to String
TO_CHAR(trandate, 'YYYY-MM-DD')
TO_CHAR(trandate, 'Month DD, YYYY')
TO_CHAR(trandate, 'Day')

Date Parts (EXTRACT)

-- Extract parts of a date
SELECT
trandate,
EXTRACT(YEAR FROM trandate) AS year,
EXTRACT(MONTH FROM trandate) AS month,
EXTRACT(DAY FROM trandate) AS day,
TO_CHAR(trandate, 'Day') AS day_name,
TO_CHAR(trandate, 'Month') AS month_name
FROM transaction
LIMIT 5

Date Arithmetic

-- Add/subtract days
SELECT
SYSDATE AS today,
SYSDATE + 7 AS next_week,
SYSDATE - 30 AS last_month,
SYSDATE + 365 AS next_year
FROM DUAL

-- Difference between dates (in days)
SELECT
tranid,
trandate,
duedate,
duedate - trandate AS days_to_pay
FROM transaction
WHERE type = 'CustInvc'

Test Case: Date Analysis

-- Order aging analysis
SELECT
id,
tranid,
trandate,
TO_CHAR(trandate, 'YYYY-MM') AS order_month,
ROUND(SYSDATE - trandate) AS days_old,
CASE
WHEN SYSDATE - trandate <= 30 THEN 'Current'
WHEN SYSDATE - trandate <= 60 THEN '30-60 Days'
WHEN SYSDATE - trandate <= 90 THEN '60-90 Days'
ELSE 'Over 90 Days'
END AS aging_bucket
FROM transaction
WHERE type = 'CustInvc'
AND status != 'C' -- Not closed
ORDER BY trandate
LIMIT 20

Sample Result:

idtranidtrandateorder_monthdays_oldaging_bucket
501INV-5012024-11-152024-113730-60 Days
502INV-5022024-10-012024-108260-90 Days
503INV-5032024-07-152024-07160Over 90 Days

Type Conversion (CAST)

Converting Data Types

-- String to Number
CAST('123' AS INTEGER)
CAST('123.45' AS FLOAT)

-- Number to String
CAST(123 AS VARCHAR)
TO_CHAR(123.45, '999.99')

-- Date conversions
CAST(trandate AS VARCHAR)
TO_CHAR(trandate, 'YYYY-MM-DD')

Test Case: Type Conversion

-- Format amounts as currency strings
SELECT
tranid,
amount,
'$' || TO_CHAR(amount, 'FM999,999,990.00') AS formatted_amount,
CAST(amount AS INTEGER) AS rounded_amount
FROM transaction
WHERE type = 'CustInvc'
LIMIT 10

DISTINCT Keyword

Removing Duplicates

-- Unique customer states
SELECT DISTINCT state
FROM customer
WHERE state IS NOT NULL
ORDER BY state

-- Unique combinations
SELECT DISTINCT state, city
FROM customer
WHERE state IS NOT NULL
ORDER BY state, city

Test Case: Unique Values Report

-- Count unique values
SELECT
COUNT(*) AS total_customers,
COUNT(DISTINCT state) AS unique_states,
COUNT(DISTINCT city) AS unique_cities,
COUNT(DISTINCT salesrep) AS unique_salesreps
FROM customer
WHERE isinactive = 'F'

Sample Result:

total_customersunique_statesunique_citiesunique_salesreps
12504831215

Quick Reference

Operator Precedence (Highest to Lowest)

  1. () - Parentheses
  2. *, / - Multiplication, Division
  3. +, - - Addition, Subtraction
  4. =, !=, <, >, <=, >= - Comparisons
  5. NOT - Negation
  6. AND - Logical AND
  7. OR - Logical OR

Common Patterns

-- Case-insensitive search
WHERE UPPER(companyname) LIKE UPPER('%search%')

-- Check if value exists in list
WHERE status IN ('A', 'B', 'C')

-- Date in current month
WHERE EXTRACT(MONTH FROM trandate) = EXTRACT(MONTH FROM SYSDATE)
AND EXTRACT(YEAR FROM trandate) = EXTRACT(YEAR FROM SYSDATE)

-- Handle NULL in comparisons
WHERE NVL(field, 'default') = 'value'

-- Between with dates
WHERE trandate BETWEEN TRUNC(SYSDATE, 'MM') AND SYSDATE