Solved

if statement in select query

Posted on 2009-03-30
7
601 Views
Last Modified: 2012-05-06
Hi,

Is it possible to include an IF statement into a select query??

I am trying to include "IF (CONVERT(VARCHAR(10),GETDATE(),108) > '09:00:00')" into a select query but am receving several errors when I do.

Thanks
SELECT  INCI.INCIDENT_CODE,
		INCI.INCIDENT_TITLE,
		INCI.INCIDENT_MESSAGE,
		U.USER_NAME  AS OWNER,
		C.FIRST_NAME + ' ' + C.LAST_NAME AS CONTACT_REQUESTER,
		L1.LIST_VALUE AS INCIDENT_STATUS,
		L2.LIST_VALUE AS INCIDEND_PRIORITY,
		INCI.CREATE_DATETIME,
 
--IF (CONVERT(VARCHAR(10),GETDATE(),108) > '09:00:00')
	
		
		--CASE STATEMENT TO IDENTIFY SLA FIX DATETIME COLOUR
		CASE 	
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS.FIELD_VALUE, 103) ) > (SLA.SLA_TIME / 2) THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000361')
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS.FIELD_VALUE, 103) ) <= (SLA.SLA_TIME / 2) AND DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS.FIELD_VALUE, 103) ) > (SLA.SLA_TIME / 3) THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000362')
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS.FIELD_VALUE, 103) ) <= (SLA.SLA_TIME / 3) THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000360')
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS.FIELD_VALUE, 103) ) < 0 THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000359')
		ELSE (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000358')
		END AS [SLA Fix KPI],
 
		--CASE STATEMENT TO IDENTIFY SLA RESP DATETIME COLOUR
		CASE 	
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS3.FIELD_VALUE, 103) ) > (SLA2.SLA_TIME / 2) THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000361')
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS3.FIELD_VALUE, 103) ) <= (SLA2.SLA_TIME / 2) AND DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS.FIELD_VALUE, 103) ) > (SLA.SLA_TIME / 3) THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE= 'LIST00000000362')
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS3.FIELD_VALUE, 103) ) <= (SLA2.SLA_TIME / 3) THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = '~/Images/redSLA.jpg')
		WHEN DATEDIFF(MI, CONVERT(DATETIME, GETDATE(), 103), CONVERT(DATETIME, INCIS3.FIELD_VALUE, 103) ) < 0 THEN (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000359')
		ELSE (SELECT LIST_VALUE FROM TBL_LIST WHERE LIST_CODE = 'LIST00000000358')
		END AS [SLA Resp KPI]
 
 
FROM TBL_INCIDENT INCI
LEFT JOIN TBL_USER U ON U.USER_CODE = INCI.OWNER_CODE
LEFT JOIN TBL_CONTACT C ON C.CONTACT_CODE = INCI.CONTACT_REQUESTER
LEFT JOIN TBL_LIST L1 ON L1.LIST_CODE = INCI.INCIDENT_STATUS
LEFT JOIN TBL_LIST L2 ON L2.LIST_CODE = INCI.INCIDENT_PRIORITY
 
--NEW CODE TO ACCOMODATE THE SLA IMAGE--
LEFT JOIN TBL_INCIDENT_IS INCIS ON INCI.INCIDENT_CODE = INCIS.INCIDENT_CODE AND INCIS.OBJECT_PROPERTY_CODE = 'OBJP00000000020' --SLA End DateTime
LEFT JOIN TBL_INCIDENT_IS INCIS2 ON INCI.INCIDENT_CODE = INCIS2.INCIDENT_CODE AND INCIS2.OBJECT_PROPERTY_CODE = 'OBJP00000000016' --SLA FIX CAT
LEFT JOIN TBL_INCIDENT_IS INCIS3 ON INCI.INCIDENT_CODE = INCIS3.INCIDENT_CODE AND INCIS3.OBJECT_PROPERTY_CODE = 'OBJP00000000034' --SLA RESP End DateTime
LEFT JOIN TBL_INCIDENT_IS INCIS4 ON INCI.INCIDENT_CODE = INCIS4.INCIDENT_CODE AND INCIS4.OBJECT_PROPERTY_CODE = 'OBJP00000000035' --SLA RESP CAT
 
LEFT JOIN TBL_SLA SLA ON SLA.SLA_CODE = INCIS2.FIELD_VALUE  --SLA FIX CAT MINS
LEFT JOIN TBL_SLA SLA2 ON SLA2.SLA_CODE = INCIS4.FIELD_VALUE  --SLA resp CAT MINS
--END OF NEW CODE--
 
WHERE INCI.TEAM_CODE = 'TEAM00000000010'

Open in new window

0
Comment
Question by:dwhitfield_CPWN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24016855
what do you mean if?
what are you trying to achieve?
it is not possible to determine using an if the number of columns that will be selected, since each row must have the exact same columns

you can use a case statement in order to decide if you wish to return a value or just a null for a certain column
0
 
LVL 7

Accepted Solution

by:
luani earned 500 total points
ID: 24016870
you cannot place the IF inside the query but you can do :
DECLARE @VAR VARCHAR(10)

SET @Var=CONVERT(VARCHAR(10),GETDATE(),108)

IF @Var>''09:00:00'
SELECT ..........
ELSE
SELECT .........


0
 
LVL 13

Expert Comment

by:St3veMax
ID: 24016871
WHat are you trying to achieve ?

Looking at your example it would be something for monitoring SLA's; I do something similar with our Support Centre; but I use in the where clause:

DATEPART(HH, YourField) BETWEEN '8' AND '18'
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:dwhitfield_CPWN
ID: 24016873
Ok, basically I only want to use the two case statements if the current time is greater than 09:00:00.

If is before 09:00:00 then I dont want the two case statements to run

Hope this helps
0
 
LVL 13

Expert Comment

by:St3veMax
ID: 24016905
The example above will help as you're saying your criteria is between set hours.

HTH
0
 

Author Comment

by:dwhitfield_CPWN
ID: 24016948
I still want the query to run even if its past say 17:00:00.

So ive used the variable technique and done and if staement around that.

Thanks for all your help.
0
 
LVL 8

Expert Comment

by:vinurajr
ID: 24017178
This IF too u can avoid... by bringing the condition in where clause
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 70
Why is the output of this function is like this? 4 47
Query 14 62
SSMS Imprt data from Excel 7 37
Hello, In my precious Article  (http://www.experts-exchange.com/Database/Reporting/A_15280-Create-Project-in-Microstrategy-Part-I.html)we saw the Configuration part for Microstrategy which included Metadata Creation and DataSource Preparation as …
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.
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 …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

737 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