Skip to content

Commit 09b0fcd

Browse files
committed
Add getdate functions recommendations
1 parent 7506845 commit 09b0fcd

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,18 @@ More details about SQL Server data types and mapping it with another databases a
201201

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

204-
| Not Recommended | Recommended | When and Why | More details |
205-
|-----------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------|----------------|
206-
| [`!=`][12] | [`<>`][12] | `<>` is [`ANSI`], `!=` not `ANSI`, [`<>` and `!=` are identical][13] | [13] |
207-
| [`CONVERT`][10] | [`CAST`][10] | `CAST` is [`ANSI`] | [14],[15] |
208-
| [`ISNULL`] | [`COALECSE`] | `COALECSE` is [`ANSI`] and supports more than two arguments, `ISNULL` has dangerous behaviour with possibility to implicit triming string | [16],[17] |
209-
| [`DATEDIFF`] | [`DATEADD`] | The predicate `MyDateTime < DATEADD(SECOND, -1, GETUTCDATE())` syntax is [`SARGable`] | [18],[19] |
210-
| [`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] |
211-
| [`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] |
212-
| [`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] |
204+
| Not Recommended | Recommended | When and Why | More details |
205+
|-----------------|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------|----------------|
206+
| [`!=`][12] | [`<>`][12] | `<>` is [`ANSI`], `!=` not `ANSI`, [`<>` and `!=` are identical][13] | [13] |
207+
| [`CONVERT`][10] | [`CAST`][10] | `CAST` is [`ANSI`] | [14],[15] |
208+
| [`ISNULL`] | [`COALECSE`] | `COALECSE` is [`ANSI`] and supports more than two arguments, `ISNULL` has dangerous behaviour with possibility to implicit triming string | [16],[17] |
209+
| [`DATEDIFF`] | [`DATEADD`] | The predicate `MyDateTime < DATEADD(SECOND, -1, GETUTCDATE())` syntax is [`SARGable`] | [18],[19] |
210+
| [`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] |
211+
| [`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] |
212+
| [`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] |
213+
| [`GETDATE`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
214+
| [`GETUTCDATE`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
215+
| [`SYSDATETIME`] | [`SYSUTCDATETIME`] | Daylight Saving Time and other factors can play havoc with our dates and times, rounding to the nearest 3 milliseconds. | [25] |
213216

214217
[12]:https://docs.microsoft.com/sql/t-sql/language-elements/comparison-operators-transact-sql
215218
[13]:https://dba.stackexchange.com/a/155670/107045
@@ -234,6 +237,10 @@ This is only recommendations! But it is consistent for choosing only 1 function
234237
[`ISNUMERIC`]:https://docs.microsoft.com/en-us/sql/t-sql/functions/isnumeric-transact-sql
235238
[`TRY_CONVERT`]:https://docs.microsoft.com/en-us/sql/t-sql/functions/try-convert-transact-sql
236239
[24]:https://www.red-gate.com/hub/product-learning/sql-prompt/sql-prompt-code-analysis-avoid-using-isnumeric-function-e1029
240+
[25]:https://bornsql.ca/blog/dates-times-sql-server-t-sql-functions-get-current-date-time/
241+
[`GETDATE`]:https://docs.microsoft.com/sql/t-sql/functions/getdate-transact-sql
242+
[`GETUTCDATE`]:https://docs.microsoft.com/sql/t-sql/functions/getutcdate-transact-sql
243+
[`SYSDATETIME`]:https://docs.microsoft.com/sql/t-sql/functions/sysdatetime-transact-sql
237244

238245
**[⬆ back to top](#table-of-contents)**
239246

0 commit comments

Comments
 (0)