Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 tricky SELECT
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 02/19/2011 :  06:48:26  Show Profile  Reply with Quote
I'm pretty new to this and i need help with a SELECT problem.

From my search comes $city and $service
$city compares only to the column city and works fine!
But $service is not just one column, its the name of many columns.

ID - username - password - city - serviceA - serviceB - serviceC
01 - username1 - password1 - city1 - service1 - service1 - service1
02 - username2 - password2 - city2 - [empty ] - service2 - service2
03 - username3 - password3 - city3 - service3 - service3 - service3

I want the rows where $city=city and the $service column is not empty on that row! (witch service that is searched for)

Like this: SELECT * FROM sj_users WHERE city = '$city' AND '$service' IS NOT NULL
If I search for ex. serviceA I would get ID 01 and 03, but not 02 because its serviceA column is empty!

Is this possible?
Very great full for any help!! THANKS!

Flowing Fount of Yak Knowledge

1182 Posts

Posted - 02/19/2011 :  07:21:41  Show Profile  Reply with Quote
It can be acheived using dynamic sql but dynamic sql have its own pros and cons. Have a look at:
Go to Top of Page

Flowing Fount of Yak Knowledge

1182 Posts

Posted - 02/19/2011 :  07:28:33  Show Profile  Reply with Quote
--Sample table with data
create table TabDyn
( Id int,
UserName varchar(50),
City varchar(50),
ServiceA varchar(50),
ServiceB varchar(50))

Insert into TabDyn
Select 1,'username1','city1','service1', 'service1' union all
Select 2,'username2','city2',null, 'service1' union all
Select 3,'username3','city3','service3', 'service3'

--Dynamic select logic start here
Declare @City varchar(50)
Declare @Service varchar(50)

Select @City='city1', @Service = 'ServiceA'

Exec ('Select * from TabDyn where city = '''+ @City + ''' or ' + @Service + ' is not null')
Go to Top of Page

In (Som, Ni, Yak)

17689 Posts

Posted - 02/19/2011 :  07:40:04  Show Profile  Reply with Quote
if the number of "service" column is limited, you don't have to use Dynamic SQL

select *
from   yourtable
where  city = @city
and    (
           @service = 'service1' and service1 is not null
       or  @service = 'service2' and service2 is not null
       or  @service = 'service3' and service3 is not null

Time is always against us

Go to Top of Page

Esteemed SQL Purist

547 Posts

Posted - 02/19/2011 :  17:24:42  Show Profile  Visit jcelko's Homepage  Reply with Quote
You are doing everything wrong.

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you want to learn how to ask a question on a Newsgroup, look at:

If you don't know anything about RDBMS, then get a copy of the simplest intro book I know --

You have a repeated group in violation of 1NF. The column names are wrong. You put a $ in names. Did you mean an empty string or a NULL (we have no DDL)?

Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000