Date Conversion Issue MS Access to SQL Server

本小妞迷上赌 提交于 2019-12-13 07:35:32

问题


I'm creating a table B from an exisitng table A. In the table A I have a column ValDate which is varchar and contains Date. When I try to create the table B I have a function used in the query as given below and I get a conversion error. Table A contains null values as well. MS Access:

((DateDiff("d",Date(),format(Replace(Replace([Table A].ValDate,".","/"),"00/00/0000","00:00:00"),"dd/mm/yyyy")))>0)).

Tables were in MS Access and are being migrated to SQL Server 2012.

SQL Server:

((DATEDIFF(day,FORMAT( GETDATE(), 'dd-MM-yyyy', 'en-US' ),FORMAT( ValDate, 'dd-MM-yyyy', 'en-US' ))>0)) 

or

((DateDiff(day,GETDATE(),Format(Replace(Replace([TableA].[ValidFrom],'.','/'),'00/00/0000','00:00:00'),'dd/mm/yyyy')))

I tried converting the date using several approachs like Convert , Format and Cast but I end up getting error as below.

Msg 8116, Level 16, State 1, Line 1 Argument data type date is invalid for argument 1 of isdate function.

I would really appreciate someone telling me what I'm missing here.


回答1:


since you have date data in a string field is very likely you have some value that is not valid against your expected date format.

copy the data in a sql server table and then perform check and validation of the content of the string field.

have a look to the function try_convert that can be helpful when checking the content of the string field containing the date values.

when bad data is ruled out you can apply again your formula with (hopefully) a different result.
a better solution would be to create a separate field with appropriate datatype to store date values converted from the string field and apply your logic to that field.



来源:https://stackoverflow.com/questions/26383947/date-conversion-issue-ms-access-to-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!