DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   MS SQL (http://www.daniweb.com/forums/forum127.html)
-   -   Get All Rows In Column In A Where Statment (http://www.daniweb.com/forums/thread36013.html)

blackraven Nov 28th, 2005 12:40 pm
Get All Rows In Column In A Where Statment
 
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:

campkev Nov 28th, 2005 6:10 pm
Re: Get All Rows In Column In A Where Statment
 
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

blackraven Nov 28th, 2005 8:35 pm
Re: Get All Rows In Column In A Where Statment
 
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.

campkev Nov 29th, 2005 12:06 am
Re: Get All Rows In Column In A Where Statment
 
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')

blackraven Nov 30th, 2005 12:18 am
Re: Get All Rows In Column In A Where Statment
 
Thanks that worked.

Now all i have to do is configure my php Thanks

noman78 Nov 30th, 2005 7:39 am
Re: Get All Rows In Column In A Where Statment
 
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 .

blackraven Nov 30th, 2005 11:17 am
Re: Get All Rows In Column In A Where Statment
 
k thanks i will work with that and see want i can do.

blackraven Nov 30th, 2005 11:20 am
Re: Get All Rows In Column In A Where Statment
 
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

campkev Nov 30th, 2005 11:46 am
Re: Get All Rows In Column In A Where Statment
 
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?

blackraven Nov 30th, 2005 12:45 pm
Re: Get All Rows In Column In A Where Statment
 
Well sorta now i am having a problem with placing a variable in the syntax like this

WHERE (MAS0030 LIKE '%$search8%')


All times are GMT -4. The time now is 3:16 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC