2

我需要获得每年 7 月 1 日至 10 月 31 日之间的日期范围。基于此,我必须更新另一列。

日期字段是日期时间。应该像下面这样:

Select Cash = Case When date between '1st July' and '31st October' Then (Cash * 2) End
From MytTable

注意:这个范围应该适用于每一年。

4

4 回答 4

3

这是一种方式:

SELECT Cash = CASE WHEN RIGHT(CONVERT(VARCHAR(8),[date],112),4) 
              BETWEEN '0701' AND '1031' THEN Cash*2
              ELSE Cash END --I added this 
于 2013-10-03T16:51:42.373 回答
2

对于您的情况,您可以只使用月份并确保它在 7 到 10 之间。

这就是您的查询的方式:

select Cash = case when month([Date]) in (7, 8, 9, 10) then (Cash * 2) else Cash end

或者

select Cash = case when month([Date])  between 7 and 10 then (Cash * 2) else Cash end
于 2013-10-03T16:52:08.000 回答
1

这个解决方案对我有用,可以让我得到一些按月和天重复出现的主题。它处理跨越多年的项目,例如计划从 11 月 1 日到 3 月 1 日的项目。

我的数据是这样存储的:

ThemeName, StartMonth, StartDay, EndMonth, EndDay
Christmas, 12,1,12,26
Winter, 11, 1, 3, 1
Spring, 3,1,5,1
Sports, 0,0,0,0 //continuous

这是查询:

    SELECT
         DISTINCT ThemeName
    FROM (
        SELECT 
            CASE WHEN EndDate < StartDate AND ((Current Between StartDate AND 1231) OR (Current Between 0101 AND EndDate) ) THEN -- span multiple year between dates
                ThemeName
            WHEN StartDate < EndDate AND Current Between StartDate AND EndDate THEN -- normal between dates
                ThemeName
            WHEN StartDate = 0 AND EndDate = 0 THEN -- continuous
                ThemeName
            ELSE
                'NG'
            END as ThemeName
        FROM (
                 SELECT
                         ThemeName, StartDate, EndDate, Current
                 FROM (     
                         SELECT
                             ThemeName,
                             CAST(Concat(LPAD(CONVERT(StartMonth , CHAR(2)),2,'0'), LPAD(CONVERT(StartDay , CHAR(2)),2,'0') ) as DECIMAL) as StartDate,
                             CAST(Concat(LPAD(CONVERT(EndMonth , CHAR(2)),2,'0'), LPAD(CONVERT(EndDay , CHAR(2)),2,'0') ) as DECIMAL) as EndDate,
                             CAST(Concat(LPAD(CONVERT(MONTH(CURRENT_DATE) , CHAR(2)),2,'0'), LPAD(CONVERT(DAY(CURRENT_DATE) , CHAR(2)),2,'0') ) as DECIMAL)  as current
                         FROM
                             companythemeschedules
                         WHERE
                             companyid = 221 
                 ) a

         ) b
    ) c
    WHERE
        ThemeName != 'NG'
于 2014-02-21T15:39:40.517 回答
0

试试这个:

DECLARE @DT DATE = GETUTCDATE() , @START_DATE VARCHAR(10) = '07-01' , @END_DATE VARCHAR(10) = '10-31'
SELECT 
CASE 
    WHEN RIGHT(@DT,5) BETWEEN  @START_DATE AND  @END_DATE
    THEN
    (CASH * 2)
    ELSE
    'NO MATCH'
END AS CASH 
于 2013-10-08T11:57:45.213 回答