SQL Server Cannot Call Methods on Date

后端 未结 11 1452
时光取名叫无心
时光取名叫无心 2020-12-16 10:41

I\'ve got a DATETIME column in a SQL Server 2008 table called ShiftDate. I want to convert this to a DATE column in a query:

SELECT     ID, ScheduleID, Shift         


        
相关标签:
11条回答
  • 2020-12-16 10:49

    It seems to be a real bug as mentioned above by https://stackoverflow.com/users/464923/will, inherited from MS Access old days with all it's inconveniences, You have just to ignore the error message and save your view then run it in a regular SSMS window, no error will be thrown .

    0 讨论(0)
  • 2020-12-16 10:53

    This bug is still present in Management Studio 2016, seems for now the only solution is to cast to a varchar and live with it if you want to use the query designer.

    0 讨论(0)
  • 2020-12-16 10:55

    From my experience, the error can be ignored. Just save the view and query it outside the Design/"View editor" window (in a new query window) and it will work.

    0 讨论(0)
  • 2020-12-16 10:56

    You can use the Convert option instead.

    SELECT     ID, ScheduleID, ShiftDate, CONVERT(VARCHAR(10), ShiftDate,101) AS ProductionDate
    FROM       dbo.ScheduleResults
    

    You can look at the various options of date formats here.

    0 讨论(0)
  • 2020-12-16 11:01

    You are correct, it is a bug in SSMS. I'm using SQL Server Management Studio 2008 R2 and when I try to create a View using the built-in designer, I get the same error message as you:

    SQL Execution Error
    Error Source: .Net SqlClient Data Provider
    Error Message: Cannot call methods on date.
    

    As @Aaron Bertrand mentioned, to solve the issue, select 'New Query' and create the View in the Query window. For your code, it would be:

    CREATE VIEW myView AS
    SELECT     ID, ScheduleID, ShiftDate, CAST(ShiftDate AS DATE) AS ProductionDate
    FROM       dbo.ScheduleResults
    
    0 讨论(0)
  • 2020-12-16 11:01

    The problem is with the name ShiftDate of the attribute in the underlying table. When renamed to something that does not contain the word Date as the last characters, the view works. Try renaming to ShiftDateDD for example.

    0 讨论(0)
提交回复
热议问题