Despite the lack of a clear description of your problem I think you are trying to understand how to check for more than one status in your where clause. It also doesn't help that the code you have posted is not SQL (it looks to be VB?).
I set up your data as follows (for simplicity I have set all of the dates to today)
declare @message table (message_thread_id int, created_at datetime, [status] varchar(10))
insert into @message (message_thread_id, created_at, [status]) values
(229, getdate(),''),
(229, getdate(), 'delivered'),
(229, getdate(), 'sent'),
(229, getdate(), 'delivered'),
(229, getdate(), 'delivered'),
(240, getdate(), 'sent'),
(240, getdate(), 'sent'),
(1044, getdate(),''),
(1044, getdate(),''),
(1044, getdate(),''),
(1068, getdate(), 'delivered');
When I run the code you have allegedly tried I get two error messages
Quote:
--'date' is not a recognized built-in function name.
-- 'now' is not a recognized built-in function name.
So the first thing I did was convert that to SQL ...
select message_thread_id, created_at, status
from @message
where cast(created_at as date)= cast(getdate() as date) and status <> 'delivered'
Unsurprisingly that gives me the rows that are not 'delivered' - which includes the blank status rows. Which is what you want.
However, you originally said that
Quote:
I want to get data by id with status 'sent' and ' ' only
By saying "not 'delivered'" this is getting you the results you want sort of by accident. If you have another status (e.g. 'Pending') then those rows will also be included in your results, which is not what you want.
Reword your requirements slightly - you want data with status 'sent' OR with status '' and that will give you a clue how to progress
select message_thread_id, created_at, status
from @message
where cast(created_at as date)= cast(getdate() as date)
and (status = 'delivered' or status = '')
Note the use of parentheses to group the conditions for [status] - I'm not going to expand on that here but if you want to see what happens when you miss them out add the following data into the demo
insert into @message (message_thread_id, created_at, [status]) values
(229, getdate() + 1,''),
(229, getdate() + 1, 'delivered'),
(229, getdate() + 1, 'sent'),
(229, getdate() + 1, 'delivered'),
(229, getdate() + 1, 'delivered'),
(240, getdate() + 1, 'sent'),
(240, getdate() + 1, 'sent'),
(1044, getdate() + 1,''),
(1044, getdate() + 1,''),
(1044, getdate() + 1,''),
(1068, getdate() + 1, 'delivered');
This article goes into more detail
SQL: Using Parentheses with And / OR condition is necessary with where clause to gives expected result - Codepedia[
^]
An alternative method - since you know exactly which [status] values you want, you could use an IN condition in your WHERE clause e.g.
select message_thread_id, created_at, status
from @message
where cast(created_at as date)= cast(getdate() as date) and status in ('delivered','');
Of course, after all that, if this has nothing to do with your question then please clarify your point as requested