sqldba20
Posting Yak Master
183 Posts |
Posted - 2011-12-13 : 22:19:02
|
Folks:When I run the SQL Query inside the view it works fine with no errors. When I execute the view with where condition of date (select * from vwTEST where ADate > '12/13/2011') it errors with 'Conversion failed when converting date and/or time from character string' even though the data is converted as datetime in the view. The 'cdata' column is data type varchar and stores dates. Here are all the details: CREATE TABLE [dbo].[TEST]( [id] [int] IDENTITY(1,1) NOT NULL, [details] [varchar](200) NULL, [cdata] [varchar](200) NULL, [timestamp] [datetime] NULL, [rtype] [varchar](50) NULL) ON [PRIMARY]GOinsert into TEST (details,cdata,timestamp,rtype) values ('europe','emea',getdate(),'Region')insert into TEST (details,cdata,timestamp,rtype) values ('August','Permanent','12/13/11 17:03','Area')insert into TEST (details,cdata,timestamp,rtype) values ('8/25/2011','8/25/2011','12/13/11 17:03','Common')insert into TEST (details,cdata,timestamp,rtype) values ('10/5/2011','10/5/2011','12/13/11 17:03','AuctionDate')insert into TEST (details,cdata,timestamp,rtype) values ('10/12/2011','10/12/2011','12/13/11 17:03','SettleDate')insert into TEST (details,cdata,timestamp,rtype) values (NULL,NULL,'12/13/11 17:03',NULL)insert into TEST (details,cdata,timestamp,rtype) values ('Japan','Japan','12/13/11 17:03','Region')GOCREATE view [dbo].[vwTEST]asselect r1.cdata as Region , r2.cdata as [Reference Entity] , cast(r3.cdata as datetime) as [DDate] , cast(r4.cdata as datetime) as [ADate] , cast(r5.cdata as datetime) as [SDate] , r1.timestamp as [RDate]from dbo.TEST r1 with (nolock)join dbo.TEST r2 with (nolock) on r2.id = r1.id + 1join dbo.TEST r3 with (nolock) on r3.id = r1.id + 2join dbo.TEST r4 with (nolock) on r4.id = r1.id + 3join dbo.TEST r5 with (nolock) on r5.id = r1.id + 4where r1.rtype = 'Region'Thanks ! |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-12-14 : 09:23:11
|
Article fails to mention that there are other universal formats supported by sql server.FORMATS:'CCYYMMDD' { '2012-12-25' )'CCYY-MM-DDTHH:MM:SS.mmm' ( '2012-12-25T07:30:03.000' )'CCYYMMDD HH:MM:SS' ( '20121225 07:30:03' )Are all universal and cannot be confused by language setting.Everything else is prone to error.Alsoquote: The 'cdata' column is data type varchar and stores dates. Here are all the details:
NO NO NO NO. Don't do that.If the column is supposed to store a DATE, store a DATE, not a string.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|