且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

T-SQL:将 NTEXT 转换为 VARCHAR 到 INT/BIGINT

更新时间:2023-01-30 13:00:44

您无法控制 where 子句和转换的应用顺序.在某些情况下,SQL Server 会尝试对未通过过滤器的行执行转换 - 这一切都取决于计划.试试这个:

You can't control the order in which the where clause and conversions apply. In some cases SQL Server will attempt to perform the conversion on rows that wouldn't pass the filter - all depends on the plan. Try this instead:

SELECT CASE WHEN ID = 111 THEN 
  CONVERT(INT, CONVERT(VARCHAR(12), FileSize))
  -- don't be lazy, size ------^^ is important
  END
FROM dbo.myTable
WHERE ID = 111;

还要考虑使用整数列来存储整数.然后你就不会在 FileSize 列中出现像 '7/1/2008 3:39:30 AM' 这样愚蠢的废话.

Also consider using an integer column to store integers. Then you don't end up with goofy nonsense in the FileSize column like '7/1/2008 3:39:30 AM'.