FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
SQL ServerSubqueries & ranked selection

Sort preferred rows ahead of other rows

SQL Server: A CASE ordering key expresses business priority before a stable secondary sort.

SQL Server✓ Tested
SQL ServerGetting Started

Sort the highest quantities first

SQL Server: Specify descending order and a stable tie breaker.

SQL Server✓ Tested
SQL ServerDates & text

Split delimited text with positions

Retain original order with STRING_SPLIT ordinals.

STRING_SPLIT✓ Tested
SQL ServerSelect & page

Substitute a value for NULL

Use SQL Server’s ISNULL expression.

ISNULL✓ Tested
SQL ServerWindow analytics

Sum a row and its immediate predecessor

SQL Server: A ROWS frame counts physical ordered rows rather than all peers with the same sort value.

SQL Server✓ Tested
SQL ServerText cleanup & matching

Treat an empty trimmed value as missing

SQL Server: NULLIF converts an empty string to NULL before a fallback is applied.

SQL Server✓ Tested
SQL ServerGetting Started

Turn a condition into a result label

SQL Server: Use CASE to describe stock availability for each row.

SQL Server✓ Tested
SQL ServerJSON & results

Turn JSON into typed rows

Project JSON array items into SQL columns.

OPENJSON✓ Tested
SQL ServerReporting & grouped calculations

Turn known group values into columns

SQL Server: Conditional aggregation gives a portable pivot when the category set is known.

SQL Server✓ Tested
SQL ServerSchema design & transactions

Undo a multi-row transfer atomically

SQL Server: The first transaction commits the fixtures. Rolling back the second transaction restores both sides of the attempted transfer.

SQL Server✓ Tested
SQL ServerSchema design & transactions

Undo only the work after a savepoint

SQL Server: After committing the fixtures, roll back a trial change while keeping earlier work in the second transaction.

SQL Server✓ Tested
SQL ServerJSON & results

Update a JSON property

Create updated JSON text with JSON_MODIFY.

JSON_MODIFY✓ Tested