Friday, February 24, 2012

Issue On Views

I have a view which has a date field. However, this date field is in the form of a number: 20041401. There are also places where the date is 0 (zero). I want to get this date to look like 1/14/04. I did this by creating a view on the first view, and used case to set the 0 (zero) values to null, and then I created another view on the last one to convert the not null values to a date using cast. Is there a simpler way to do what I want? Pretty soon I will lose track of all my views.
Thanks.Maybe you should cast or convert the datetime column the right way in your first view.|||I can't because the date string is in the form of a number. When I execute the view, and it comes across a 0 (zero) I get an error. Because cast can't convert a number to char.|||Not sure I follow..but...

DECLARE @.x int
SELECT @.x = 20041401
SELECT CONVERT(varchar(10),
CONVERT(datetime,SUBSTRING(CONVERT(char(8),@.x),7,2 )
+ '/' + SUBSTRING(CONVERT(char(8),@.x),5,2)
+ '/' + SUBSTRING(CONVERT(char(8),@.x),1,4)),1)|||Originally posted by exdter
I can't because the date string is in the form of a number. When I execute the view, and it comes across a 0 (zero) I get an error. Because cast can't convert a number to char.

Really?

SELECT CONVERT(varchar(10),0)|||That's what I need. Thanks.|||I strongly agree with the notion that, if you're dealing with date-type information that hasn't been stored as a Date field (and which, e.g. for application compatibility reasons, can't be converted now), it is highly desirable that you should use a base view that Casts the value to a date; then base other views on that.|||And doen't that make it an non-updateable view?|||Thanks.

No comments:

Post a Comment