0

我正在使用 SQL Server 2008 R2。我需要同时检查列值是否有"-",我case这里使用的语句。

如果"-"存在,那么我需要将日期格式更改为dd/mm/yy. 数据库中的给定日期是"2013-01-01 00:00:00.000" to "dd/mm/yy"

你对此有什么想法吗?

我的代码是:

select  
    DISTINCT 
    D30.SPGD30_SHIP_SITE_C As ShipSite, D30.SPGD30_RATING_MONTH_Y As RatingMonth,
    D30.SPGD30_PRIOR_SCORE_R As Prior, D30.SPGD30_REVISED_SCORE_R As Revised, 
    CASE 
      WHEN SUBSTRING(D30.SPGD30_TRACKED_ADJUSTMENT_X, 1, 1) = 'D' 
        THEN 'Dispute - ' + D30.SPGD30_TRACKED_ADJUSTMENT_X 
      WHEN SUBSTRING(D30.SPGD30_TRACKED_ADJUSTMENT_X, 1, 1) = 'R' 
        THEN 'Return Of Points - '+D30.SPGD30_TRACKED_ADJUSTMENT_X
      WHEN containstable(D30.SPGD30_TRACKED_ADJUSTMENT_X, '"*-" or "-*"') 
        THEN 'Score Calculation - ' + CONVERT(VARCHAR(8), D30.SPGD30_TRACKED_ADJUSTMENT_X, 1)
    END AS Adjustments,
    J02.SPGJ02_MSG_CODE_X As Comments,
    D31.SPGA04_RATING_ELEMENT_D As Comments,
    D30.SPGD30_LAST_TOUCH_Y As LastUpdated,
    D30.SPGD30_LAST_TOUCH_C As LastUpdatedCDSID 
FROM
    CSPGD30_TRACKING D30, CSPGD31_TRACKING_RATING_ELEMNT D31,
    CSPGA04_RATING_ELEMENT_MSTR A04 , CSPGJ02_MSG_OBJ J02, CSPGJ03_MSG_TRANSLN J03
WHERE
    D30.SPGD30_SHIP_SITE_C = D31.SPGD30_SHIP_SITE_C -- D30 and D31
    AND D30.SPGD30_RATING_MONTH_Y = D31.SPGD30_RATING_MONTH_Y --D30 and D31
    AND D31.SPGA04_RATING_ELEMENT_D = A04.SPGA04_RATING_ELEMENT_D --D31 and A04
    AND J02.SPGJ02_MSG_K = J03.SPGJ02_MSG_K --J02 and J03
    AND A04.SPGJ02_MSG_K = J02.SPGJ02_MSG_K --A04 and Jo2
    AND d30.SPGA02_BUSINESS_TYPE_C = 'prod' -- org
    AND d30.SPGA03_REGION_C = 'EU' -- region
    AND d30.SPGD30_SHIP_SITE_C = 'ms01a' -- shipsite
    AND D30.SPGD30_LAST_TOUCH_Y BETWEEN '2013-01-01 00:00:00.000'  --rating month
                                    AND '2013-01-30 23:59:59.000' -- rating month
    AND d30.SPGD30_LAST_TOUCH_C = 'sadalara' --CDSID   
4

1 回答 1

4

只需使用CHARINDEX进行测试

...
when CHARINDEX('-*', D30.SPGD30_TRACKED_ADJUSTMENT_X) > 0 OR
  CHARINDEX('*-', D30.SPGD30_TRACKED_ADJUSTMENT_X) > 0

THEN 'Score Calculation - '+CONVERT(VARCHAR(8), D30.SPGD30_TRACKED_ADJUSTMENT_X, 1)

仅供参考,您需要样式 103dd/mm/yyyy

于 2013-02-21T14:59:55.197 回答