Please support our MS SQL advertiser: Programming Forums
Views: 6231 | Replies: 12
![]() |
•
•
Join Date: Nov 2005
Posts: 13
Reputation:
Rep Power: 3
Solved Threads: 0
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
I want this to select all and not just null rows if somone can help me out i would really appreciated it! :eek:
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:
•
•
Join Date: Nov 2005
Posts: 13
Reputation:
Rep Power: 3
Solved Threads: 0
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
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
I used an between statement
then i just placed the variable $year_results in my query statement like this
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
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.
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')";
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.
•
•
Join Date: Jul 2005
Location: Dallas, TX
Posts: 482
Reputation:
Rep Power: 4
Solved Threads: 19
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')
$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')
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 .
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 .
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode