I have a query in which I need to get results from a table, depending on the parameters specified by the user on a vb.net page.
Declaring a variable would be the ideal solution, but since its an inline table-valued function I can't do this, I had read on other questions that this is possible in a multstatment table-function but that the use of such function is not recommended.
The following query would be the ideal solution or what i want to achieve.
USE [AUDIT]
GO
ALTER FUNCTION [dbo].[GetCreated_LPA_Audits]
(
@fechaInicio nvarchar (max),
@fechaFin nvarchar (max)
@Period int,
@Fecha int
)
RETURNS TABLE
AS
RETURN
(
SELECT T1.Plant,T1.Area,T1.Location,T1.IDAudit,T1.Auditor,T1.AuditDate,T1.DueDate,T1.CreationDate
FROM Header AS T1 inner join Audits AS T2 ON T1.IDChecklist = T2.IDChecklist
inner join AuditGroups AS T3 ON T2.IDGroup = T3.IDGroup
WHERE T3.IDGroup = '2' and CreationDate is not null AND
CASE WHEN @Periodo = '0' THEN CreationDate>= @fechaInicio
WHEN @Periodo = '1' THEN DATEPART(MONTH,CreationDate)>= @Fecha
WHEN @Periodo = '2' THEN
CASE WHEN @Fecha = 13 THEN
DATEPART(MONTH,CreationDate)>= 1
END
WHEN @Periodo = '2' THEN
CASE WHEN @Fecha = 14 THEN
DATEPART(MONTH,CreationDate)>= 7
END
WHEN @Periodo = '3' THEN
CASE WHEN @Fecha = 15 THEN
DATEPART(Year,CreationDate)>= 2015
END
END
AND
CASE WHEN @Periodo = '0' THEN @fechaInicio<=CreationDate
WHEN @Periodo = '1' THEN @Fecha<=DATEPART(MONTH,CreationDate)
WHEN @Periodo = '2' THEN
CASE WHEN @Fecha = 13 THEN
6<=DATEPART(MONTH,CreationDate)
END
WHEN @Periodo = '2' THEN
CASE WHEN @Fecha = 14 THEN
12<=DATEPART(MONTH,CreationDate)
END
WHEN @Periodo = '3' THEN
CASE WHEN @Fecha = 15 THEN
DATEPART(Year,CreationDate)>= 2015
END
END
AND In the previous query @fechaInicio, @fechaFin, @Periodo and @Fecha are parameters provided by the user.
Basically if @Periodo = 0
I need to get the results where
CreationDate>= @fechaInicio and CreationDate<=@fechaFin
If @Periodo = 1
I need to get the results where
DATEPART(MONTH,CreationDate)>= @Fecha and DATEPART(MONTH,CreationDate)<= @Fecha
And so on. Hope I made myself clear, thanks in advance!
EDIT using pseudo-code from @MatBailie, with some slights alterations and answers to his questions
IF @periodo = '0' THEN
WHERE CreationDate >= @fechaInicio -- Copied from 1st CASE
AND @fechaFin <= CreationDate -- Copied from 2nd CASE
-- gets results from @fechaInicio to @fechaFin
-- i.e. results from 04/05/2016 to 04/16/2016
IF @periodo = '1' THEN
WHERE DATEPART(MONTH,CreationDate) >= @Fecha -- Copied from 1st CASE
AND @Fecha <= DATEPART(MONTH,CreationDate) -- Copied from 2nd CASE
-- In these case both conditions are the same 'cause
-- @Fecha is the number of a month (1 - 12)
-- i.e. @Fecha = 3 will get all the results of March
-- regardless of what it is on @fechaInicio and @fechaFin
IF @periodo = '2' THEN
IF @fetcha = 13 THEN
WHERE DATEPART(MONTH,CreationDate)>= 1 -- Copied from 1st CASE
AND 6<=DATEPART(MONTH,CreationDate) -- Copied from 2nd CASE
IF @fetcha = 14 THEN
WHERE DATEPART(MONTH,CreationDate)>= 7 -- Copied from 1st CASE
AND 12<=DATEPART(MONTH,CreationDate) -- Copied from 2nd CASE
-- You never use @fetchaInicio?
-- You want everything in the first 6 months or last 6 months
-- For every year
-- Regardless of what is in @fetchaInicio?
-- Exactly!!--
IF @periodo = '3' THEN
IF @fetcha = 15 THEN
WHERE DATEPART(Year,CreationDate)>= 2015 -- Copied from 1st CASE
AND 2015 <= DATEPART(Year,CreationDate) -- Copied from 2nd CASE
--
And what about the case @periodo = '2' AND @fetcha NOT IN (13,14)
?
And what about the case @periodo = '3' AND @fetcha NOT IN (15)
?
This case would not exist, its restricted on the client side.
If they chose @Periodo = '2'
then @Fecha
will have values of 1 - 12 and nothing else.
Same with @Periodo = '3'
then @Fecha
will have values of 15 or 16, both refering to 2015 or 2016.