SQL 根据值在 select 中投射

SQL Cast in select based on value

有没有办法根据它是否为数字在 select 语句中转换 nvarchar 字段。我目前有这段代码,但出于某种原因它正在尝试转换其中一个非数值。

  SELECT id,
  CASE WHEN ISNUMERIC(field) = 1 THEN CAST(field AS INT) ELSE CAST(field AS NVARCHAR(10)) END AS [fieldName]
  FROM table1

如果你用零填充数值的左边,它会正确排序吗,如下所示?

DECLARE @Table1 TABLE (Field NVARCHAR(10))

INSERT INTO @Table1 (Field) VALUES (1)
INSERT INTO @Table1 (Field) VALUES (10)
INSERT INTO @Table1 (Field) VALUES (100)
INSERT INTO @Table1 (Field) VALUES (2)
INSERT INTO @Table1 (Field) VALUES ('abv')

SELECT 
    Field,
    CASE 
        WHEN ISNUMERIC(Field) = 1 
        THEN RIGHT('000000000' + Field,10) 
        ELSE Field 
     END AS SortField
FROM @Table1
ORDER BY SortField DESC

SELECT 
    Field,
    CASE 
        WHEN ISNUMERIC(Field) = 1 
        THEN RIGHT('000000000' + Field,10) 
        ELSE Field 
     END AS SortField
FROM @Table1
ORDER BY SortField ASC