While INNER
and LEFT JOIN
s are the workhorses of SQL queries, T-SQL offers several more advanced join techniques that can be invaluable for complex data analysis and manipulation. This article explores some of these powerful join methods and how they can enhance our queries.
Category: SQL Server
Date Formats Accepted by SQL Server’s datetime2 Type
Below are the string literal date formats that SQL Server supports for the datetime2 data type. These are the formats that we can provide when setting a datetime2 date from a string literal.
Continue readingFix Error “The function ‘RANK’ must have an OVER clause” in SQL Server
If you’re getting an error that reads “The function ‘RANK’ must have an OVER clause” in SQL Server, it’s probably because you’re calling the RANK()
function without an OVER
clause.
The RANK()
function requires an OVER
clause (and that clause must have an ORDER BY
clause).
To fix this issue, add an OVER
clause when calling the RANK()
function.
How to Fix “The function ‘NTILE’ must have an OVER clause with ORDER BY” Error in SQL Server
When using the NTILE()
function in SQL Server, we must include an OVER
clause clause with an ORDER BY
clause.
If you’re getting error msg 4112 that reads “The function ‘NTILE’ must have an OVER clause with ORDER BY” when using the NTILE()
function, it’s because, although you’re (correctly) including an OVER
clause, you’re omitting the ORDER BY
clause.
To fix this error, add an ORDER BY
clause to the OVER
clause.
Fix “Invalid object name ‘GENERATE_SERIES'” in SQL Server
If you’re getting SQL Server error 208 that reads “Invalid object name ‘GENERATE_SERIES’“, it could be that you’re calling GENERATE_SERIES()
in a version of SQL Server that doesn’t support this function.
The GENERATE_SERIES()
function was introduced in SQL Server 2022 (16.x), and so if we try to call it in an earlier version of SQL Server, we’ll get the above error.
So if you’re running this on an earlier version, you’ll need to upgrade before you can run it successfully. You’ll also need to run it on a database with a compatibility level of at least 160.
Continue readingFix Error “The function ‘DENSE_RANK’ must have an OVER clause” in SQL Server
If you’re getting error 10753 when using a window function in SQL Server, it’s probably because you’re calling the function without an OVER
clause.
When using the DENSE_RANK()
function, the error message reads “The function ‘DENSE_RANK’ must have an OVER clause”.
The DENSE_RANK()
function requires an OVER
clause (and that clause must have an ORDER BY
clause).
To fix this issue, add an OVER
clause to the DENSE_RANK()
function.
Fix “‘GENERATE_SERIES’ is not a recognized built-in function name” in SQL Server
If you’re getting an error that reads “‘GENERATE_SERIES’ is not a recognized built-in function name” in SQL Server, it could be that you’re calling GENERATE_SERIES()
in the wrong context.
In SQL Server, GENERATE_SERIES()
is a relational operator that returns a series of values from a given start and stop point.
When we call GENERATE_SERIES()
in a SQL query, we don’t include it in the SELECT
list, like we would with many SQL functions. Instead, we reference it in the FROM
clause, like we would reference a table.
So if you’re getting the above error, it could be that you’re inadvertently making this mistake. To fix, be sure to call GENERATE_SERIES()
in the correct context.
Understanding the Right Shift and Left Shift Operators in SQL Server
SQL Server 2022 introduced a bunch of new bit manipulation functions and operators. Amongst these are the right shift and left shift operators.
The right shift and left shift operators can be used to shift bits to the left or right. Given an integer or binary expression, these operators can shift bits by the amount that we specify.
Continue readingHow to Fix “The function ‘CUME_DIST’ must have an OVER clause with ORDER BY” Error in SQL Server
If you’re getting error message 4112 that reads “The function ‘CUME_DIST’ must have an OVER clause with ORDER BY” when using the CUME_DIST()
function, it’s probably because you’re omitting the ORDER BY
clause from the OVER
clause.
When using the CUME_DIST()
function in SQL Server, we must include an OVER
clause that contains an ORDER BY
clause. This error happens when we provide the OVER
clause but not the ORDER BY
clause.
To fix this error, add an ORDER BY
clause to the OVER
clause.
A Quick Introduction to the GET_BIT() Function in SQL Server
In SQL Server, we can use the GET_BIT()
function to return the bit from the specified location in a value. We pass the value as either an integer or binary expression.
The function accepts two arguments; the expression, and the offset. The function then returns the bit from the given offset in the expression.
Continue reading