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 2008 Forums
 Transact-SQL (2008)
 Get value if ID Exist else get Default value

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2012-03-21 : 23:06:55
Dear ALl,

I have a table contain:

Type Company Rate
Software ABC 5
Software Null 3
Hardware Null 2

if Type=Software

1. Company=ABD (Rate: 3)
2. Company=ABC (Rate: 5)
3. Company=Null (Rate: 3)

I looking for single sql not Stored procedure.

I will past in the 2 parameter; Type & Company. IIf not exist in the MS SQL 2008. I can't perform the work.

Please advise.

Thank you.

Regards,
Micheale
How can i do that?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-21 : 23:17:01
you can use CASE WHEN


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2012-03-21 : 23:26:36
Thank you.

I found the solution.

Here it go...

IF exists (SELECT Rate FROM tblTypeSetup WHERE (Company = 'ABD'))
SELECT Rate FROM TypeSetup WHERE [Type]='Software' and (Company = 'ABD')
ELSE
SELECT Rate FROM TypeSetup WHERE [Description]='Software' and Company is null
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-21 : 23:31:52
[code]
SELECT top 1 Rate
FROM TypeSetup
WHERE [Type] = 'Software'
and (
Company is null
or Company = 'ABD'
)
ORDER BY Company DESC
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -