2017-08-12 91 views

回答

2

如果是2012+,可以使用Try_Convert()。對於任何轉換失敗,這將返回NULL,而不是拋出錯誤。

Declare @YourTable Table ([SomeCol] varchar(50)) 
Insert Into @YourTable Values 
('Oct 25, 2004') 
,('May 02, 2006') 
,('Nov 19, 2002') 
,('Not a Date Format') 

Select * 
     ,AsDate = try_convert(date,SomeCol) 
from @YourTable 

返回

SomeCol    AsDate 
Oct 25, 2004  2004-10-25 
May 02, 2006  2006-05-02 
Nov 19, 2002  2002-11-19 
Not a Date Format NULL   -- << Notice Returned NULL 
+0

如果default不是'EN'會怎麼樣?另外'TRY_CONVERT'可用於SQL Server 2012及更高版本。 – lad2025

+1

說'try_convert'不是一個公認的內置函數名稱。儘管SSMS將其作爲關鍵字進行了突出顯示 – user3953989

+0

我的服務器是2012年,但看起來像數據庫兼容級別設置爲2008,更改了它,並且它工作了 – user3953989