CTE SQL中的字符串连接问题

时间:2010-10-07 09:18:47

标签: sql sql-server database sql-server-2008

我有以下CTE SQL

WITH Tasks AS (
    SELECT     TaskID, ParentTaskID, CAST(SortKey AS nChar) AS sort_key
               /*,cast(SortKey as char) as sort_key */
      FROM     oaTasks AS s
      WHERE    (TaskID = 1)
      UNION ALL
      SELECT   s2.TaskID, s2.ParentTaskID
               ,Cast( '0.'+ cast(Tasks_2.sort_key as NCHAR)
                 + '0.' + cast(s2.SortKey as NCHAR) as NCHAR) AS sort_key
               /*,cast(Tasks_2.sort_key +'.'+ cast(s2.SortKey as char) as char)*/
      FROM     oaTasks AS s2
      INNER JOIN Tasks AS Tasks_2
         ON Tasks_2.TaskID = s2.ParentTaskID
)
SELECT  Tasks_1.TaskID, oaTasks.Task, oaTasks.ParentTaskID
        , oaTasks.SortKey,Tasks_1.sort_key
  FROM  Tasks AS Tasks_1
  INNER JOIN oaTasks ON Tasks_1.TaskID = oaTasks.TaskID

在sort_key列中,我没有得到所需的格式,应该像01, 01.01, 01.01.01 etc.,但我得到1, 0.1, 0.1.1。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

试试吧 选择右('00',1,2)给出01