Sets the search conditions in a select, insert, update, or delete statement.
Search conditions immediately follow the keyword where in a select, insert, update, or delete statement. If you use more than one search condition in a single statement, connect the conditions with and or or.
where [not] expression comparison_operator expression
where {[not] expression comparison_operator expression} | {...}
where [not] expression [not] like "match_string" [escape "escape_character "]
where [not] expression is [not] null
where [not] expression [not] between expression and expression
where [not] expression [not] in ({value_list | subquery})
where [not] exists (subquery)
where [not] expression comparison_operator {any | all} (subquery)
where [not] column_name join_operator column_name
where [not] logical_expression
where [not] expression {and | or} [not] expression
where column_name is [not] null
Operator |
Meaning |
---|---|
= |
Equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
!= |
Not equal to |
<> |
Not equal to |
!> |
Not greater than |
!< |
Not less than |
In comparing char, nchar, unichar, varchar, univarchar, and nvarchar data, < means closer to the beginning of the alphabet and > means closer to the end of the alphabet.
Case and special character evaluations depend on the collating sequence of the operating system on the machine on which the SAP ASE server is located. For example, lowercase letters may be greater than uppercase letters, and uppercase letters may be greater than numbers.
Trailing blanks are ignored for the purposes of comparison. For example, “Dirk” is the same as “Dirk ”.
= "Bennet" > "94609"
See User-Defined Datatypes in Reference Manual: Building Blocks for more information about data entry rules.
You can use the keyword like and wildcard characters with datetime and date data as well as with char and varchar. When you use like with datetime or date and time values, the SAP ASE server converts the dates to standard datetime format, then to varchar. Since the standard storage format does not include seconds or milliseconds, you cannot search for seconds or milliseconds with like and a pattern.
where arrival_time = '9:20'
where arrival_time like '%9:20%'
where @val between x and y
x and @val < y
Queries using between return no rows if the first value specified is greater than the second value.
When more than one logical operator is used in a statement, and operators are usually evaluated first. However, you can change the order of execution with parentheses.
in (@a, @b, @c)
@a = "'1', '2', '3'"
column_name allows text, unitext, or image datatypes.
When more than one logical operator is used in a statement, or operators are normally evaluated after and operators. However, you can change the order of execution with parentheses.
where advance * $2 > total_sales * price
where phone not like '415%'
where au_lname like "[CK]ars[eo]n"
where sales_east.syb_identity = 4
where advance < $5000 or advance is null
where (type = "business" or type = "psychology") and advance > $5500
where total_sales between 4095 and 12000
where state in ('CA', 'IN', 'MD')
create table temp1(c1 int, c2 text null, c3 unitext null, c4 image null) insert into temp1 values(1, null, replicate("u",5), null) insert into temp1 values(2, replicate("x",3), null, null) go select * from temp1 where c2 is null go
c1 c2 c3 c4 ----- ---------- -------------------------- ------------ 1 NULL 0x75007500750075007500 NULL (1 row affected)
select * from temp1 where c2 is not null and c3 is null and c4 is null go
c1 c2 c3 c4 ----- ---------- ---------- ---------- 2 xxx NULL NULL
insert into temp1 values(3, replicate("y", 3), null, 0x858585847474) insert into temp1 values(4, replicate("z",3),"aaa", 0x75) go update temp1 set c2 = "updated" where c2 is not null select * from temp1 where c2 is not null go
(3 rows affected) c1 c2 c3 c4 ----- ---------- ---------- ---------- 2 updated NULL NULL 3 updated NULL 0x858585847474 4 updated 0x610061006100 0x75
select c1, c2 into temp2 from temp1 where c2 is null select * from temp2 go
(1 row affected) c1 c2 ----- ---------- 1 NULL
insert into temp2 select c1, c2 from temp1 where c2 is not null select * from temp2 go
(3 rows affected) c1 c2 ----- ---------- 1 NULL 2 updated 3 updated 4 updated (4 rows affected)
select count(*) from temp2 where c1 in (select c1 from temp1 where c2 is null and c3 is not null)
----------- 1 (1 row affected)
delete from temp1 where c3 is null go
(2 rows affected)
having avg (price) > 20
where avg (price) > 20
For examples, see Transact-SQL Functions in Reference Manual: Building Blocks for information on the use of aggregate functions, and group by and having clauses.
Joins and subqueries are specified in the search conditions; see the Transact-SQL User’s Guide for full details.
You can use the keyword like to search a unitext column for a specific pattern. However, the like clause is not optimized when it is used with a unitext column. like pattern matching for unitext depends on the default Unicode sort order, which is also used for like pattern matching for unichar and univarchar datatypes.
... where expression like ‘match_string’ ...
When match_string is a locator, the SAP ASE server uses only up to 16KB of the corresponding LOB.
Specifying the null condition selects only those rows with a null value in the specified LOB column. The LOB value may be null either because it was explicitly assigned a null value, or because the LOB was not initialized.
The number of and and or conditions in a where clause is limited only by the amount of memory available to run the query.
The pattern string included in the like predicate is limited only by the size of string that can be placed in a varchar.
'I don''t understand.'
"He said, ""It's not really confusing."""
'George said, "There must be a better way."' "Isn't there a better way?" 'George asked, "Isn"t there a better way?"'
To enter a character string that is longer than the width of your screen, enter a backslash (\) before going to the next line.
where int_column = 2
selects rows where int_column = 2.
When the SAP ASE server optimizes queries, it evaluates the search conditions in where and having clauses, and determines which conditions are search arguments (SARGs) that can be used to choose the best indexes and query plan. All of the search conditions are used to qualify the rows. For more information on search arguments, see the Performance and Tuning Guide.
Date and Time Datatypes in Reference Manual: Building Blocks
sp_helpjoins in Reference Manual: Procedures
ANSI SQL – Compliance level: Entry-level compliant.