文本数据类型问题

时间:2017-11-17 19:34:05

标签: sql-server tsql

我在下面有这个代码,当我运行它时,我收到错误

  

ORDER BY子句中的“Orders.User_Memo1”列无效,因为它不包含在聚合函数或GROUP BY子句中

然后我添加o.User_Memo1(或只是删除CONVERT(VARCHAR(MAX)),我收到以下错误消息:

  

无法比较或排序text,ntext和image数据类型,除非使用IS NULL或LIKE运算符

所以我基本上陷入了困境,无论我做什么,我都会收到错误,排除它,包括它,并将它包含在CONVERT中都会给我带来错误。我有什么选择?

SELECT
    CAST(o.OrderNo AS INT) AS [OrderNo], 
    CONVERT(VARCHAR(MAX), CAST(SUBSTRING(od.User_Memo1, 
    CHARINDEX('Pack',od.User_Memo1)+5, 8) AS DATE), 1) AS [Prod Due Date],
    o.User_Date1 AS [3 Week Ack Date],
    o.User_Date2 AS [1 Week Ack Date],
    CASE 
       WHEN CONVERT(VARCHAR(MAX), o.User_Memo1) LIKE '%RDS%' 
          THEN CONVERT(VARCHAR(MAX), CAST(SUBSTRING(od.User_Memo1, CHARINDEX('RSD', od.User_Memo1) + 4, 8) AS DATE), 1) 
          ELSE NULL
    END AS [Revised Ship Date],
    d.ShipDate
FROM 
    Orders o 
JOIN 
    OrderDet od ON o.OrderNo = od.OrderNo
LEFT JOIN 
    DelTicket d ON o.OrderNo = d.OrderNo
WHERE 
    CONVERT(VARCHAR(MAX), o.User_Memo1) IS NOT NULL
    AND SUBSTRING(o.User_Memo1, 1, 3) = 'Cut'
GROUP BY 
    o.OrderNo, 
    CONVERT(VARCHAR(MAX), CAST(SUBSTRING(od.User_Memo1, CHARINDEX('Pack', od.User_Memo1) + 5, 8) AS DATE), 1), 
    o.User_Date1, o.User_Date2, 
    CONVERT(VARCHAR(MAX), o.User_Memo1), CONVERT(VARCHAR(MAX), CAST(SUBSTRING(od.User_Memo1, CHARINDEX('RSD', od.User_Memo1) + 4, 8) AS DATE), 
1), 
    d.ShipDate
ORDER BY 
    CAST(SUBSTRING(o.User_Memo1, CHARINDEX('Pack',o.User_Memo1) + 5, 8) AS DATETIME) DESC;

1 个答案:

答案 0 :(得分:0)

我认为你的cast条款中缺少where。尝试将其更改为此

AND CAST(Substring(o.user_memo1, 1, 3) as varchar(3)) = 'Cut'

完成查询

SELECT Cast(o.orderno AS INT) AS [OrderNo], 
       CONVERT(VARCHAR(max), Cast(Substring(od.user_memo1, Charindex('Pack', od.user_memo1) + 5, 8) AS  DATE), 1)    AS [Prod Due Date], 
       o.user_date1                                                                                                  AS [3 Week Ack Date], 
       o.user_date2                                                                                                  AS [1 Week Ack Date], 
       CASE 
         WHEN CONVERT(VARCHAR(max), o.user_memo1) LIKE '%RDS%' 
         THEN CONVERT(VARCHAR(max), Cast(Substring(od.user_memo1, Charindex('RSD', od.user_memo1) + 4, 8) AS DATE), 1) 
         ELSE NULL 
       END                                                                                                          AS [Revised Ship Date], 
       d.shipdate 
FROM orders o 
JOIN orderdet od ON o.orderno = od.orderno 
LEFT JOIN delticket d  ON o.orderno = d.orderno 
WHERE  CONVERT(VARCHAR(max), o.user_memo1) IS NOT NULL 
       AND  CAST(Substring(o.user_memo1, 1, 3) as varchar(3)) = 'Cut' 
GROUP  BY o.orderno, 
          CONVERT(VARCHAR(max), Cast(Substring(od.user_memo1,  Charindex('Pack', od.user_memo1) + 5, 8) AS DATE), 1), 
          o.user_date1, 
          o.user_date2, 
          CONVERT(VARCHAR(max), o.user_memo1), 
          CONVERT(VARCHAR(max), Cast(Substring(od.user_memo1,  Charindex('RSD', od.user_memo1) + 4, 8) AS  DATE), 1), 
          d.shipdate 
ORDER  BY Cast(Substring(o.user_memo1, Charindex('Pack', o.user_memo1) + 5, 8) AS DATETIME) DESC;