RSS Forums RSS
Please support our MS SQL advertiser: Programming Forums
Views: 6231 | Replies: 12
Reply
Join Date: Nov 2005
Posts: 13
Reputation: blackraven is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
blackraven blackraven is offline Offline
Newbie Poster

Get All Rows In Column In A Where Statment

  #1  
Nov 28th, 2005
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:
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 482
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Get All Rows In Column In A Where Statment

  #2  
Nov 28th, 2005
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
Reply With Quote  
Join Date: Nov 2005
Posts: 13
Reputation: blackraven is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
blackraven blackraven is offline Offline
Newbie Poster

Re: Get All Rows In Column In A Where Statment

  #3  
Nov 28th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 482
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Get All Rows In Column In A Where Statment

  #4  
Nov 29th, 2005
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')
Reply With Quote  
Join Date: Nov 2005
Posts: 13
Reputation: blackraven is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
blackraven blackraven is offline Offline
Newbie Poster

Re: Get All Rows In Column In A Where Statment

  #5  
Nov 30th, 2005
Thanks that worked.

Now all i have to do is configure my php Thanks
Reply With Quote  
Join Date: Nov 2005
Location: Dubai,UAE
Posts: 36
Reputation: noman78 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
noman78's Avatar
noman78 noman78 is offline Offline
Light Poster

Re: Get All Rows In Column In A Where Statment

  #6  
Nov 30th, 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 .
Reply With Quote  
Join Date: Nov 2005
Posts: 13
Reputation: blackraven is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
blackraven blackraven is offline Offline
Newbie Poster

Re: Get All Rows In Column In A Where Statment

  #7  
Nov 30th, 2005
k thanks i will work with that and see want i can do.
Reply With Quote  
Join Date: Nov 2005
Posts: 13
Reputation: blackraven is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
blackraven blackraven is offline Offline
Newbie Poster

Re: Get All Rows In Column In A Where Statment

  #8  
Nov 30th, 2005
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
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 482
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Get All Rows In Column In A Where Statment

  #9  
Nov 30th, 2005
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?
Reply With Quote  
Join Date: Nov 2005
Posts: 13
Reputation: blackraven is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
blackraven blackraven is offline Offline
Newbie Poster

Re: Get All Rows In Column In A Where Statment

  #10  
Nov 30th, 2005
Well sorta now i am having a problem with placing a variable in the syntax like this

WHERE (MAS0030 LIKE '%$search8%')
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:14 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC