2012-03-05 4 views
0

T-SQL과 관련하여 간단한 질문이 있습니다. 날짜를 반환하는 함수를 호출하는 저장 프로 시저가 있습니다. IF 조건을 사용하여 오늘 날짜를 함수가 반환 한 날짜와 비교하려고합니다. IF를 true로 설정하면 데이터를 반환합니다.T-SQL IF 조건 날짜 평가

이 문제를 해결하는 가장 좋은 방법에 대한 아이디어. 나는 현재 t-SQL을 배우고 있으며 나는 C#을 사용하여 논리적 인 조건에 익숙하다.

ALTER FUNCTION [dbo].[monday_new_period](@p_date as datetime) -- Parameter to find current date 
RETURNS datetime 
BEGIN 

-- 1 find the year and period given the current date 

-- create parameters to store period and year of given date 
declare @p_date_period int, @p_date_period_year int 

-- assign the values to the period and year parameters 
select 
@p_date_period=period, 
@p_date_period_year = [year] 
from client_week_uk where @p_date between start_dt and end_dt 

-- 2 determine the first monday given the period and year, by adding days to the first day of the period 
-- this only works on the assumption a period lasts a least one week 

-- create parameter to store the first day of the period 
declare @p_start_date_for_period_x datetime 
select @p_start_date_for_period_x = min(start_dt) 
from client_week_uk where period = @p_date_period and [year] = @p_date_period_year 

-- create parameter to store result 
declare @p_result datetime 

-- add x days to the first day to get a monday 
select @p_result = dateadd(d, 
    case datename(dw, @p_start_date_for_period_x) 
     when 'Monday' then 0 
     when 'Tuesday' then 6 
     when 'Wednesday' then 5 
     when 'Thursday' then 4 
     when 'Friday' then 3 
     when 'Saturday' then 2 
     when 'Sunday' then 1 end, 
    @p_start_date_for_period_x) 

Return @p_result 
END 

ALTER PROCEDURE [dbo].[usp_data_to_retrieve] 
-- Add the parameters for the stored procedure here 

AS 
BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 

IF monday_new_period(dbo.trimdate(getutcdate()) = getutcdate() 

BEGIN 

-- SQL GOES HERE -- 

END 

감사합니다.

답변

2

나는 당신이 Sql2008에서 작업하고 있다고 가정합니다. 자세한 내용은 IFCASE 키워드의 설명서를 참조하십시오.

CREATE FUNCTION dbo.GetSomeDate() 
RETURNS datetime 
AS 
BEGIN 
    RETURN '2012-03-05 13:12:14' 
END 

GO 


IF CAST(GETDATE() AS DATE) = CAST(dbo.GetSomeDate() AS DATE) 
BEGIN 
    PRINT 'The same date' 
END 
ELSE 
BEGIN 
    PRINT 'Different dates' 
END 

-- in the select query 
SELECT CASE WHEN CAST(GETDATE() AS DATE) = CAST(dbo.GetSomeDate() AS DATE) THEN 1 ELSE 0 END AS IsTheSame 
+0

도움이되었습니다. 감사. –

0

이것은 T-SQL IF 및 날짜 비교의 기본 구문입니다. 당신이 어떤지 단지 날짜 부분을 비교하는 경우

당신은 사용해야합니다 :

select dateadd(dd,0, datediff(dd,0, getDate())) 

이 조각은 효과적으로 그래서 당신은 날짜를 바로 비교할 수 00:00:00 시간 부분을 설정합니다. 그래서 사용하면 다음과 같이 보일 것입니다.

IF dateadd(dd,0, datediff(dd,0, fn_yourFunction())) = dateadd(dd,0, datediff(dd,0, GETDATE())) 
BEGIN 
    RETURN SELECT * FROM SOMEDATA 
END 

희망 하시겠습니까?

+0

나는 함수에서 반환되는 날짜를 오늘 날짜와 비교하고, 동일한 경우 추가 SQL을 실행하려고합니다. 두 값이 동일해야하므로 작동하는 것보다 얼마나 큰지는 알 수 없습니다. –

+0

예제 코드가 없다고 대답했을 때 나는 당신이 비교하려고했던 것을 정확히 알지 못했습니다. 대답을 편집하십시오. – breathingdust

+0

답변을 보았을 때 코드를 게시해야 함을 알았습니다. 죄송합니다. –