How to use where clause to get more than one value in sql server?

2021-08-26T23:26:50.893+00:00

Hi Team

I am getting a one specific value when im using where clause. Is there a better to get other values when IN statement inside parameters is used? Please have a look at my query below

select
[Station Description]
[Transaction Code],
[DateTime Passed]
from [Tracking_Server_DB].[dbo].TS_Station as stn
inner join [Tracking_Server_DB].[dbo].[Checkpoint Movement] as mv
on stn.[Transaction Code] = mv.[Transaction Code]
where stn.[Station Description] IN ('T0011', 'M250', 'T0011')
and [DateTime Passed] = '2021-08-26 22:49:19.000'
order by [DateTime Passed] desc

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,361 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,601 questions
0 comments No comments
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,581 Reputation points
    2021-08-27T01:35:10.237+00:00

    Hi @Farhan Sabir

    Welcome to the microsoft TSQL Q&A forum!

    The WHERE clause will return all rows that meet the conditions. The parameter IN will also return all rows that meet the condition. In other words, all rows that meet the conditions behind them will be returned.

    As pituach said, it may be that your JOIN statement filters out other rows. Please try the following code first to see if the JOIN sentence is incorrectly filtered:

    select  
    [Station Description]  
    [Transaction Code],  
    [DateTime Passed]  
    from [Tracking_Server_DB].[dbo].TS_Station as stn  
    inner join [Tracking_Server_DB].[dbo].[Checkpoint Movement] as mv  
    on stn.[Transaction Code] = mv.[Transaction Code]  
    --where stn.[Station Description] IN ('T0011', 'M250', 'T0011')  
    --and [DateTime Passed] = '2021-08-26 22:49:19.000'  
    order by [DateTime Passed] desc  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ronen Ariely 15,186 Reputation points
    2021-08-27T00:49:10.107+00:00

    Good day,

    How to use where clause to get more than one value in sql server?

    Simply make sure that the condition do not filter out all the rows...

    The WHERE simply specifies the search condition for the rows which returned by the query. There is no limit to the number of rows which can return when you use WHERE (if there are rows that fit the condition). If you do not get all rows, then your condition in the WHERE or the condition in the INNER JOIN ON filter these rows out.

    If the amount of rows in not too high then you can check manually the rows which return without the where after using the JOIN. Try to execute the same query and examine the rows. My guess is that your INNER JOIN filter some rows which you expect to get in the result, and your first thought was that filter is done by the WHERE only -I cannot know if this is the case since we don't have your table and data in roder to execute the query

    0 comments No comments