Solved

Conditionala where clause in sql query

Posted on 2013-01-28
4
298 Views
Last Modified: 2013-01-28
SQL Server 2005

in sql query to construct a where clause for
if host_name() = 'MATEEN' then
    unit_code = '21'
else
   ignore unit code matching and show all records
end if


I tried
where  (  unit_code =  '21' and host_name() =  'MATEEN') or 1=1 )
but it is showing all records in all machines including MATEEN'S P.C.
0
Comment
Question by:Mehram
  • 2
  • 2
4 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 38826339
Try this..

SELECT * FROM ur_table
WHERE unit_code = (CASE WHEN host_NAME() = 'MATEEN' then '21' ELSE unit_code end)

Replace ur_table with your table name and run the above query..
0
 

Author Comment

by:Mehram
ID: 38826344
SELECT * FROM ur_table
WHERE unit_code = (CASE WHEN host_NAME() = 'MATEEN' then '21' ELSE unit_code end)

cannot be else unit_code end , I want to show records of all units if user is not mateen.
0
 

Author Comment

by:Mehram
ID: 38826349
Oh no,

it is working, thanks
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 38826429
welcome, Mehram..
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question