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

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Conditional select

Author  Topic 

duncanwill
Starting Member

20 Posts

Posted - 2009-04-22 : 11:19:49
Hi all,

I am probably making this harder than needs to be but it is hurting my head!

I am trying to conditionally selct a line of data from a table with the following logic (obviously pseudo code!):

Select * from table where field = "my value" and if "my value" is not there use the value "default value"


In actuality "default value" is NULL but i am not sure if this complicates things or not. The table data is set and not for me to change - I am just trying to leverage what is there.

Does this make sense? Any help appreciated!



nr
SQLTeam MVY

12543 Posts

Posted - 2009-04-22 : 11:23:40
select * from table where field = 'my value'
union all
select * from table
where not exists (select * from table where field = 'my value')
and field = 'default value'


or

if exists (select * from table where field = 'my value')
select * from table where field = 'my value'
else
select * from table where field = 'default value'

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

duncanwill
Starting Member

20 Posts

Posted - 2009-04-22 : 11:29:08
Hmmm told you i was trying too hard - I was going down the route of Coalesce and/or CASE but if the cap fits... ta for the sanity check
Go to Top of Page
   

- Advertisement -