sql-server-tsql-types-and-queries
Step 6 of the pipeline (WORKFLOW.md), for T-SQL on SQL Server and Azure SQL: the type and literal defaults
that truncate or round without an error, and the query shapes that quietly turn an index seek into a scan.
The premise: T-SQL's defaults were chosen for compatibility, so a declaration or predicate that works on
ten rows can truncate, round or scan on ten million. Transactions, dynamic SQL and operations are the
sibling block sql-server-transactions-and-operations; the same ideas for EF Core are
dotnet-aspnet-efcore-pitfalls §3.
When
- A column, variable or parameter is declared, or a literal is written.
- A string is built by concatenation, or
ISNULL or COALESCE picks a default.
- A query is slow, a plan "flips", or the same statement is fast with one value and slow with another.
- A predicate wraps a column in a function, or a parameter type differs from its column.
Steps
Read only the section the task meets. They are independent.
| § |
Covers |
Read it when |
File |
| 1 |
Types, literals and dates: datetime2, lengths, N literals, deprecated types, money, ISNULL, concatenation |
a type, a literal or a default value is chosen |
01-types-literals-dates.md |
| 2 |
Query traps: sargability, implicit conversion, scalar and multi-statement functions, table variables, parameter sniffing, naming |
a query is slow, flips, or a predicate touches a column with an expression |
02-query-traps.md |
Output / checkpoint
Each rule was checked against the engine: a declared length was probed with a value one character too long
(§1.2), a predicate rewrite was compared by its actual plan (seek or scan, rows read) before and after
(§2.1), and a parameter-sensitivity fix was tried with a skewed and a rare value (§2.3). A rule checked by
reading code only is not verified.
Guardrails
- Never declare
varchar, nvarchar, char or nchar without a length (§1.2).
- Never store timestamps in
datetime or money in money for new columns (§1.1, §1.3).
- Never wrap a filtered or joined column in a function when a range on the other side expresses the same test (§2.1).
- Never "fix" parameter sniffing by copying the parameter into a local variable (§2.3).
- Versions: each rule names the SQL Server version it needs. Nothing was run while writing this block.
- Transactions, dynamic SQL, error handling and operations stay in
sql-server-transactions-and-operations;
injection review beyond those points belongs to security-hardening.
Origin
Rewritten from the SQL Server documentation (CC-BY-4.0), the tsqllint rule documents and the Brent Ozar First
Responder Kit check lists and code (MIT), read 2026-10-08. 🟡: never run by us; open points are in
references/origin.md. Overlaps with skills/sql-conventions (2026-10-19); cross-referenced, not merged.
1---2name: sql-server-tsql-types-and-queries3description: "Use when writing or reviewing T-SQL for SQL Server: choosing column and variable types (datetime vs datetime2, varchar lengths, N-prefixed literals, text and ntext, money, ISNULL vs COALESCE, string concatenation past 8,000 bytes), or investigating a slow or wrong query (functions on filtered columns, implicit conversion from mismatched types, scalar functions, multi-statement table-valued functions, table variables, parameter sniffing, unqualified object names)."4metadata:5 source: https://skillmd.com/skills/techmefr/sql-server-tsql-types-and-queries6---78# sql-server-tsql-types-and-queries910Step 6 of the pipeline (`WORKFLOW.md`), for T-SQL on SQL Server and Azure SQL: the type and literal defaults11that truncate or round without an error, and the query shapes that quietly turn an index seek into a scan.12The premise: **T-SQL's defaults were chosen for compatibility, so a declaration or predicate that works on13ten rows can truncate, round or scan on ten million**. Transactions, dynamic SQL and operations are the14sibling block `sql-server-transactions-and-operations`; the same ideas for EF Core are15`dotnet-aspnet-efcore-pitfalls` §3.1617## When18- A column, variable or parameter is declared, or a literal is written.19- A string is built by concatenation, or `ISNULL` or `COALESCE` picks a default.20- A query is slow, a plan "flips", or the same statement is fast with one value and slow with another.21- A predicate wraps a column in a function, or a parameter type differs from its column.2223## Steps2425**Read only the section the task meets.** They are independent.2627| § | Covers | Read it when | File |28|---|---|---|---|29| 1 | Types, literals and dates: datetime2, lengths, N literals, deprecated types, money, ISNULL, concatenation | a type, a literal or a default value is chosen | [`01-types-literals-dates.md`](./references/01-types-literals-dates.md) |30| 2 | Query traps: sargability, implicit conversion, scalar and multi-statement functions, table variables, parameter sniffing, naming | a query is slow, flips, or a predicate touches a column with an expression | [`02-query-traps.md`](./references/02-query-traps.md) |3132## Output / checkpoint33Each rule was checked against the engine: a declared length was probed with a value one character too long34(§1.2), a predicate rewrite was compared by its actual plan (seek or scan, rows read) before and after35(§2.1), and a parameter-sensitivity fix was tried with a skewed and a rare value (§2.3). A rule checked by36reading code only is not verified.3738## Guardrails39- Never declare `varchar`, `nvarchar`, `char` or `nchar` without a length (§1.2).40- Never store timestamps in `datetime` or money in `money` for new columns (§1.1, §1.3).41- Never wrap a filtered or joined column in a function when a range on the other side expresses the same test (§2.1).42- Never "fix" parameter sniffing by copying the parameter into a local variable (§2.3).43- Versions: each rule names the SQL Server version it needs. Nothing was run while writing this block.44- Transactions, dynamic SQL, error handling and operations stay in `sql-server-transactions-and-operations`;45 injection review beyond those points belongs to `security-hardening`.4647## Origin48Rewritten from the SQL Server documentation (CC-BY-4.0), the tsqllint rule documents and the Brent Ozar First49Responder Kit check lists and code (MIT), read 2026-10-08. 🟡: never run by us; open points are in50[`references/origin.md`](./references/origin.md). Overlaps with `skills/sql-conventions` (2026-10-19); cross-referenced, not merged.