BETWEEN logical operator (SQL) is Inclusive

SQL logical operator BETWEEN is inclusive. It uses a closed interval ([a,b]).

Syntax:

 <#test_expression#> [ NOT ] BETWEEN <#begin_expression#> AND <#end_expression#>

Result Type: Boolean

BETWEEN returns TRUE if the value of the <#test_expression#> is greater than or equal to the value of <#begin_expression#> and less than or equal to the value of <#end_expression#>.

NOT BETWEEN returns TRUE if the value of <#test_expression#> is less than the value of <#begin_expression#> or greater than the value of <#end_expression#>.

Note: You can use the greater than (>) and less than operators (<) to specify an exclusive range. If any input to the BETWEEN or NOT BETWEEN predicate is NULL, the result is UNKNOWN.

Example: The example below retrieves a list of Opportunities which has been implemented last 60 days:

SELECT a.Id, a.Name, a.StageName
FROM dbo.sf_opportunity a
WHERE a.Consultation_Date_Time_test__c BETWEEN DATEADD(DAY, -60, GETDATE()) AND GETDATE() AND a.IsDeleted = 0

No comments:

Post a Comment