简体   繁体   中英

LIKE and NOT LIKE functionality

I'm trying to get a single record (I know there is only one that matches the query) but using "LIKE" doesn't return what I need, but using "NOT LIKE" does. It doesn't make sense, I need to understand!

Results with LIKE Results with NOT LIKE

The datetime datatype is implicitly casted to a character datatype to perform the like comparison, using the default format of mon dd yyyy hh:miAM (or PM) .

Thus, you might use like 'Aug 22 2022 %' in your where clause to evaluate to true. Rather dubious, and possibly even language-dependent.

The calls in the comments to use comparison operators are spot-on, and you can explicitly cast to a date datatype if you're not interested in the time portion of the data:

where cast(TRNF_FECHA as date) = cast('2022-08-22' as date)

I Microsoft would also recommend using the datetime2 datatype over the deprecated datetime datatype where possible (which would've incidentally solved your like problem as well since ODBC canonical format is the default for datetime2).

It's funny, but I found the solution myself. I wanted to make my string (in the code it is a variable) match (with the "LIKE" and the "%" at the end because I didn't know the time) with the value of "TRNF_FECHA". But what I did was cast "TRNF_DATE" so that it matches regardless of the time.

SELECT * FROM V_EXTRACTS WHERE CAST(TRNF_FECHA AS DATE) LIKE '2022-08-22' AND TRNF_IMPORTE = 297.76

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM