MSSQL Where Statement Problem

I want to select all in a column if variable = null
I am placing a php variable in there and i know how to do that so i no that is not the problem but i want a where statement to return all rows of its column when it is null

SELECT *
FROM mytablename
WHERE (mycolumnname = '')

I want this to select all and not just null rows if somone can help me out i would really appreciated it! :eek:

Recommended Answers

All 12 Replies

not familiar with php but i think you need to put in an if clause on the php side that goes something like

if (variable = null)
select * from mytablename
else select * from mytablename where mycolumnname = variable

MSSQL Get One Column To Work With The Other

Well, you are right i did that with my php. I used IF and ELSEIF statments in my php and then redirected the true statement to variables that then went into my MSSQL Statement for my query like this.

This would be this first part

if ($search1 == '')
$year_results = $allyears;

This say if variable is equal to null then $year_results (This is the variable that will be in my query statement) is equal to variable $allyears

Now the second part is to assign a statment for variable $allyears like this

$allyears = "(column1 < '2006') OR (column1 > '1900')";

I used an between statement

then i just placed the variable $year_results in my query statement like this

$query = "SELECT * FROM databasename WHERE $submit_results AND $year_results";

Now that i did that it works just fine except i have came across another problem.
I want to be able to show all the citys with a specific year so it will only show everything in the query for 2005, but the way i have my variables setup my query will look like this

$query = "SELECT * FROM databasename WHERE (columnname2 = 'city1') OR (columnname2 = 'city2') OR (columnname2 = 'city3') OR (columnname2 = 'city4') AND (columnname1 = '2005')

and well the city kinda overlap the year so either way it will show all the cities.

So i just need to know if there is any way i can change this in my SQL so i can have all the cities show but only with ones with 2005 for the year.

Sorry to be so long i just think this is really complicated.

change it to
$query = "SELECT * FROM databasename WHERE columnname2 in ('city1', 'city2', 'city3', 'city4') AND (columnname1 = '2005')

or add parentheses in the right spot
$query = "SELECT * FROM databasename WHERE ( (columnname2 = 'city1') OR (columnname2 = 'city2') OR (columnname2 = 'city3') OR (columnname2 = 'city4') )AND (columnname1 = '2005')

Thanks that worked.

Now all i have to do is configure my php Thanks

i think all four replies are wrong for u
if u r s=using SQL then u cant comapre null as u r doing the real statment is
"select * from <table name> where isnull(<fldname>,'comparevalue')='comparevalue'

the scenario is , in sql to comapre with null , u have to fill tht column with some value ,a nd then to compare that value to find the results of null, so use anyvalue to comapre abt whihc u r sure which cant be in ur field
i normally use this command
"select * from tablename where isnull(fldname='-')='-'" by assuming tht in my field "-" can't be .

k thanks i will work with that and see want i can do.

My next task is to find the addresses or business names in my database and well if i dont put in the exact information then i cant get that data. So i want to set up a wildcard or something that will let me get all information with that word in address or in business

let's say your are looking for business named General Hardware. But you aren't sure if the name is General Hardware or General Tool Shop or something like that.
What you want is

select * from tblBusinesses where businessName like '%General%'

Is that what you are looking for?

Well sorta now i am having a problem with placing a variable in the syntax like this

WHERE (MAS0030 LIKE '%$search8%')

i want to grab the the $variable because my variable connect to my textbox which the user will but the information in.

sorry, at that point it is a php problem, which i know nothing about

lol its alright i think one of my programming friends help me out on this one. keyword = Think.
,but thanks for your help the last solution you gave me was right on the money.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.