Skip to content

Commit 0092b4e

Browse files
committed
Add BETWEEN recommendation
1 parent 8c75467 commit 0092b4e

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

SQL Server Name Convention and T-SQL Programming Style.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,22 @@ More details about SQL Server data types and mapping it with another databases a
199199

200200
This is only recommendations! But it is consistent for choosing only 1 function from possibles alterntives and use only it.
201201

202-
| Not Recommended | Recommended | When and Why | More details |
203-
|-----------------------|------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|----------------|
204-
| [`!=`][12] | [`<>`][12] | `<>` is [`ANSI`], `!=` not `ANSI`, [`<>` and `!=` are identical][13] | [13] |
205-
| [`CONVERT`][10] | [`CAST`][10] | `CAST` is [`ANSI`] | [14],[15] |
206-
| [`ISNULL`] | [`COALECSE`] | `COALECSE` is [`ANSI`] and supports more than two arguments, `ISNULL` has dangerous behaviour with possibility to implicit triming string | [16],[17] |
207-
| [`DATEDIFF`] | [`DATEADD`] | The predicate `MyDateTime < DATEADD(SECOND, -1, GETUTCDATE())` syntax is [`SARGable`] | [18],[19] |
208-
| [`SELECT`] | [`SET`] | Using `SET` (is [`ANSI`]) instead of `SELECT` when assigning variables due to properly work with `Msg 501 Subquery returned more than 1 value` | [20],[21],[22] |
209-
| [`STR`] | [`CAST`][10] | `STR` is not [`ANSI`], extremly slow, don't use more than 15 digits, and has rounding problem - use `CAST` plus concatenate instead `STR` | [23] |
210-
| [`ISNUMERIC`] | [`TRY_CONVERT`] | `ISNUMERIC` can often lead to data type conversion errors, when importing data. For SQL Server below 2012 use `WHERE` with `LIKE`. | [24] |
211-
| [`GETDATE`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
212-
| [`GETUTCDATE`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
213-
| [`SYSDATETIME`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
214-
| [`CURRENT_TIMESTAMP`] | [`SYSUTCDATETIME`] | It's too similar to the poorly-named TIMESTAMP data type, which has nothing to do with dates and times and should be called ROWVERSION. | [26] |
215-
| [`DATETIMEFROMPARTS`] | [`DATETIME2FROMPARTS`] | It's too similar to the poorly-named TIMESTAMP data type, which has nothing to do with dates and times and should be called ROWVERSION. | [26] |
216-
| [`ISDATE`] | [`TRY_CONVERT`] | `ISNUMERIC` can often lead to data type conversion errors, when importing data. For SQL Server below 2012 use `WHERE` with `LIKE`. | [26] |
202+
| Not Recommended | Recommended | When and Why | More details |
203+
|-----------------------|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------|
204+
| [`!=`][12] | [`<>`][12] | `<>` is [`ANSI`], `!=` not `ANSI`, [`<>` and `!=` are identical][13] | [13] |
205+
| [`CONVERT`][10] | [`CAST`][10] | `CAST` is [`ANSI`] | [14],[15] |
206+
| [`ISNULL`] | [`COALECSE`] | `COALECSE` is [`ANSI`] and supports more than two arguments, `ISNULL` has dangerous behaviour with possibility to implicit triming string | [16],[17] |
207+
| [`DATEDIFF`] | [`DATEADD`] | The predicate `MyDateTime < DATEADD(SECOND, -1, GETUTCDATE())` syntax is [`SARGable`] | [18],[19] |
208+
| [`SELECT`] | [`SET`] | Using `SET` (is [`ANSI`]) instead of `SELECT` when assigning variables due to properly work with `Msg 501 Subquery returned more than 1 value` | [20],[21],[22] |
209+
| [`STR`] | [`CAST`][10] | `STR` is not [`ANSI`], extremly slow, don't use more than 15 digits, and has rounding problem - use `CAST` plus concatenate instead `STR` | [23] |
210+
| [`ISNUMERIC`] | [`TRY_CONVERT`] | `ISNUMERIC` can often lead to data type conversion errors, when importing data. For SQL Server below 2012 use `WHERE` with `LIKE`. | [24] |
211+
| [`GETDATE`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
212+
| [`GETUTCDATE`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
213+
| [`SYSDATETIME`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
214+
| [`CURRENT_TIMESTAMP`] | [`SYSUTCDATETIME`] | It's too similar to the poorly-named TIMESTAMP data type, which has nothing to do with dates and times and should be called ROWVERSION. | [26] |
215+
| [`DATETIMEFROMPARTS`] | [`DATETIME2FROMPARTS`] | It's too similar to the poorly-named TIMESTAMP data type, which has nothing to do with dates and times and should be called ROWVERSION. | [26] |
216+
| [`ISDATE`] | [`TRY_CONVERT`] | `ISNUMERIC` can often lead to data type conversion errors, when importing data. For SQL Server below 2012 use `WHERE` with `LIKE`. | [26] |
217+
| [`BETWEEN`] | `>=` and `<=` | Always use an open-ended range to prevent erroneously including or excluding rows. It's much less complex to find the beginning of the next period than the end of the current period. | [27] |
217218

218219
[12]:https://docs.microsoft.com/sql/t-sql/language-elements/comparison-operators-transact-sql
219220
[13]:https://dba.stackexchange.com/a/155670/107045
@@ -244,6 +245,8 @@ This is only recommendations! But it is consistent for choosing only 1 function
244245
[`SYSDATETIME`]:https://docs.microsoft.com/sql/t-sql/functions/sysdatetime-transact-sql
245246
[26]:https://bornsql.ca/blog/dates-and-times-in-sql-server-more-functions-you-should-never-use/
246247
[`ISDATE`]:https://docs.microsoft.com/sql/t-sql/functions/isdate-transact-sql
248+
[`BETWEEN`]:https://docs.microsoft.com/sql/t-sql/language-elements/between-transact-sql?view=sql-server-ver15
249+
[27]:https://www.mssqltips.com/sqlservertip/5206/sql-server-datetime-best-practices/
247250

248251
**[⬆ back to top](#table-of-contents)**
249252

@@ -437,7 +440,7 @@ SQL Server T-SQL Coding Conventions, Best Practices, and Programming Guidelines.
437440
- Avoid using `INSERT INTO` a permanent table with `ORDER BY`.
438441
More details [here](https://www.red-gate.com/hub/product-learning/sql-prompt/sql-prompt-code-analysis-insert-permanent-table-order-pe020).
439442
- Avoid using shorthand (`wk, yyyy, d` etc.) with date/time operations, use full names: `month, day, year`.
440-
More details [here](https://sqlblog.org/2011/09/20/bad-habits-to-kick-using-shorthand-with-date-time-operations).
443+
More details [here](https://sqlblog.org/2011/09/20/bad-habits-to-kick-using-shorthand-with-date-time-operations) and [here][27].
441444
- Avoid ambiguous formats for date-only literals, use `CAST('yyyymmdd' AS DATE)` format.
442445
- Avoid treating dates like strings and avoid calculations on the left-hand side of the `WHERE` clause.
443446
More details [here](https://sqlblog.org/2009/10/16/bad-habits-to-kick-mis-handling-date-range-queries).

0 commit comments

Comments
 (0)