T-SQL筆記21_Conversion failed when converting date and/or time from character string.

 

每次透過ado.net api要來塞入datetime欄位的值
但是這個值來源可能是一系列string parser處理後的結果
通常沒有特別T-SQL指令家以處理很容易跑出這個錯誤


比方說出現00000000或是20210231這種不合法怪異的日期格式
通常若只有去作hyphen分隔的處理是不夠的仍會引發這種錯誤


所以建議要用try-convert(datetime,'外部串近來的字串對應日期值')
預設不合法不會直接報錯會返回NULL
預設外部直接傳進來沒有Hyphen分隔透過convert也可以正確轉換為日期格式





Ref:
http://sharedderrick.blogspot.com/2012/06/t-sql-tryconvert.html
https://www.aspforums.net/Threads/186047/Solved-Conversion-failed-when-converting-date-andor-time-from-character-string-in-SQL-Server/
https://www.cnblogs.com/yunfeifei/p/3820770.html


留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題