0

嗨,我有以下查询,它给了我想要的结果,除了一件事。

我的查询:

(SELECT
    (SELECT
        '3'       as '@CategoryDepth',
        'CIMtrek_SelectedPage' as '@SelectedPage',
        'CIMtrek_ParentID'     as '@ParentID',
        'CIMtrek_LstParm'      as '@LstParm',
        'CIMtrek_SearchParm'   as '@SearchParm',
        'CIMtrek_Search_No'    as '@isSearch',
        'CIMtrek_CDS1'     as '@ViewName'
        FOR XML PATH('AppConfig'), TYPE ),
        (SELECT
        RES.Header as '@Header'
        FROM (
         SELECT 'Date' AS Header
 UNION ALL 
 SELECT 'MY MENU' AS Header
        ) AS RES

        FOR XML PATH('ColumnHeader'), TYPE ),

        (SELECT
        RES.Param as '@Param'
        FROM (
         SELECT 'CIMtrek_daily_shipdate' AS Param
 UNION ALL 
 SELECT 'MY PARM' AS Param
        ) AS RES

        FOR XML PATH('Params'), TYPE ),
        (SELECT
        RES.Search as '@Search'
        FROM (
         SELECT 'Date|[T1].[CIMtrek_daily_shipdate] ' AS Search
 UNION ALL 
 SELECT 'Department|[T1].[CIMtrek_daily_PackingMethod] ' AS Search
 UNION ALL 
 SELECT 'Rush|[T1].[CIMtrek_daily_Rush] ' AS Search
 UNION ALL 
 SELECT 'Customer Name|[T1].[CIMtrek_daily_CustomerName_1] ' AS Search
 UNION ALL 
 SELECT 'Schedule Name|[T1].[CIMtrek_daily_schedule_name] ' AS Search
 UNION ALL 
 SELECT 'Carrier|[T1].[CIMtrek_daily_carrier] ' AS Search
 UNION ALL 
 SELECT 'Order|[T1].[CIMtrek_daily_order] ' AS Search
 UNION ALL 
 SELECT 'Load #|[T1].[CIMtrek_daily_load] ' AS Search
 UNION ALL 
 SELECT 'Pieces|[T1].[CIMtrek_daily_pieces] ' AS Search
 UNION ALL 
 SELECT 'Cases|[T1].[CIMtrek_daily_cases] ' AS Search
 UNION ALL 
 SELECT 'Weight|[T1].[CIMtrek_daily_weight] ' AS Search
 UNION ALL 
 SELECT 'Cube|[T1].[CIMtrek_daily_cube] ' AS Search
 UNION ALL 
 SELECT 'MY SEARCH|Search' AS Search
        ) AS RES

        FOR XML PATH('Search'), TYPE ),
        (SELECT
        (
        SELECT COUNT(*) FROM (
        SELECT
         ISNULL([T1].[CIMtrek_daily_shipdate],'')  AS CIMtrek_daily_shipdate

        FROM
            [CIMtrek_SystemTable_DatawareHouse] [T1] WHERE  LEN(LTRIM(RTRIM(ISNULL(LTRIM(RTRIM([T1].[CIMtrek_daily_shipdate])),'')))) != 0  AND 
 1 = 1
            AND [T1].[CIMtrek_form] in ('Compliance Daily Shipments') 
            AND [T1].[CIMtrek_daily_status] not in ('Complete')
            AND [T1].[CIMtrek_Archive] not in ('Archived')
        GROUP BY
         ISNULL([T1].[CIMtrek_daily_shipdate],'') 

        ) AS COUNTS1
        ) AS '@CIMtrek_COUNT' ,
        '1' AS '@CIMtrek_COUNT_Level',
        (SELECT
        [T1].[ROW] AS '@Row'
        ,newid() AS '@UnqCimID'
        <!-- 
            changed this to format date to be DD/MM/YYYY

         , [T1].[CIMtrek_daily_shipdate] AS '@CIMtrek_daily_shipdate'

           -->
        ,CONVERT(VARCHAR(10),[T1].[CIMtrek_daily_shipdate],106) AS '@CIMtrek_daily_shipdate'

        FROM (SELECT
        ROW_NUMBER() OVER(ORDER BY  ISNULL([T1].[CIMtrek_daily_shipdate],'') 
 ASC) ROW
        , ISNULL([T1].[CIMtrek_daily_shipdate],'')  AS CIMtrek_daily_shipdate

        FROM
        [CIMtrek_SystemTable_DatawareHouse] [T1]  WHERE  LEN(LTRIM(RTRIM(ISNULL(LTRIM(RTRIM([T1].[CIMtrek_daily_shipdate])),'')))) != 0  AND 
 1 = 1
            AND [T1].[CIMtrek_form] in ('Compliance Daily Shipments') 
            AND [T1].[CIMtrek_daily_status] not in ('Complete')
            AND [T1].[CIMtrek_Archive] not in ('Archived')
        GROUP BY
         ISNULL([T1].[CIMtrek_daily_shipdate],'') 
) T1
        WHERE [T1].ROW BETWEEN 1 and 10
        order by convert(date,[T1].[CIMtrek_daily_shipdate],103)
        FOR XML PATH('Data'), TYPE
        )FOR XML PATH('Data'), TYPE) )FOR XML PATH('Root')

在上面的查询中,我评论了以下行:

<!-- 
            changed this to format date to be DD/MM/YYYY

         , [T1].[CIMtrek_daily_shipdate] AS '@CIMtrek_daily_shipdate'

           -->

并输入以下行

,CONVERT(VARCHAR(10),[T1].[CIMtrek_daily_shipdate],106) AS '@CIMtrek_daily_shipdate'

这会将日期转换为 : 01-Nov-12 12:00:00 AM01-Nov-12它也很好,但在某些情况下我得到日期01-Nov-12 12:45:21 AM,在这种情况下我必须显示日期和时间。

所以实际上我不应该显示日期时间是什么时候,而是显示日期时间是 01-Nov-12 12:00:00 AM 什么时候01-Nov-12 12:45:21 AM

如何在查询本身中处理这个问题?

请帮助我得到这个。

问候

4

1 回答 1

1

通过减去 Date 部分,您可以确定 DateTime 列是否实际包含时间元素,然后可以适当地格式化,例如..

Case When [T1].[CIMtrek_daily_shipdate]-Convert(Date,[T1].[CIMtrek_daily_shipdate]) = 0
     Then Convert(varchar(12), [T1].[CIMtrek_daily_shipdate], 113)
     Else Convert(varchar(20), [T1].[CIMtrek_daily_shipdate], 113)
     End AS '@CIMtrek_daily_shipdate'

我使用了日期格式 113,它为我提供了格式12 Dec 2012 13:43:44,但任何其他包含时间元素的日期格式都可以使用,只需更改Convert()函数调用即可。

这假定源列是 DateTime,并且您可以从 DateTime 列中减去日期部分,如图所示。如果源列是 varchar,那么您可以通过将不需要的字符串替换为空字符串来简单地删除任何不需要的时间元素。例如

REPLACE([T1].[CIMtrek_daily_shipdate],' 12:00:00 AM','') AS '@CIMtrek_daily_shipdate'

假设时间实际上是以这种方式格式化的。您还可以嵌套REPLACE()函数,例如

REPLACE(REPLACE([T1].[CIMtrek_daily_shipdate],' 12:00:00 AM',''),' 12:00 AM','') AS '@CIMtrek_daily_shipdate'
于 2012-12-12T13:52:18.003 回答