Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Select statement does not return any result

I have one database table name test123 and having column name. And it contains the data like 'nir,kal,man' Now, when i am querying the table with select statement as per below :

select * from test123 where name = 'nir,kal,man';

But this will not return any result...Why this happened.? How i have to write query so that will return the result? I am using Sql server 2008.

Thanks...!

like image 603
nirav patel Avatar asked Apr 30 '12 06:04

nirav patel


People also ask

What does a select statement return?

The SQL SELECT statement returns a result set of records, from one or more tables. A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used data manipulation language (DML) command.

Why select * is not recommended?

Avoid using SELECT * There are many reasons for that recommendation, like: SELECT * Retrieves unnecessary data besides that it may increase the network traffic used for your queries. When you SELECT *, it is possible to retrieve two columns of the same name from two different tables (when using JOINS for example).

How do you execute a SQL query only if another SQL query has no results?

The common table expression ( WITH clause) wraps the first query that we want to execute no matter what. We then select from the first query, and use UNION ALL to combine the result with the result of the second query, which we're executing only if the first query didn't yield any results (through NOT EXISTS ).

Can we write select select statement?

You can construct a SELECT statement with a subquery to replace two separate SELECT statements. Subqueries in SELECT statements allow you to perform the following actions: Compare an expression to the result of another SELECT statement. Determine whether the results of another SELECT statement include an expression.


2 Answers

= operator returns exact match, so if your cell contain data "like" that you need to use LIKE operator:

select * from test123 where name like '%nir,kal,man%'

where % will be replaced with any set of characters.

Also check that you're targeting correct database by using full name

select * from yourdb.dbo.test123 where....
like image 196
Fedor Hajdu Avatar answered Oct 16 '22 11:10

Fedor Hajdu


if Nir is in first row Kal in 2nd row and man is in 3rd row then you should write query like this

select * from test123 where name in ('nir','kal','man')
like image 28
Mujassir Nasir Avatar answered Oct 16 '22 11:10

Mujassir Nasir