SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding Where Condition on Select query on a View : Error converting data type varchar to bigint.


Adding Where Condition on Select query on a View : Error converting data type varchar to bigint.

Author
Message
ykmohank-911030
ykmohank-911030
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 138
Hi,

I am building a View with an integer column by filtering data from Master table and below is the sample script. And Select Query on the View with WHERE condition is throwing the following exception.


Create table MasterEmployees (EmpID varchar(50))

Insert into MasterEmployees values ('12345'),('ABCDEF'),('9876543210')

----------------Creating the view ------------------------

Create view ChildEmployees

AS

Select Cast(EmpiD as bigint) as EmpID from MasterEmployees where ISNUMERIC(EmpID)=1

---------------------------Issue ----------------------------

Executing Select query on View without WHERE condition is working fine.

Adding WHERE condition to the Query like below

Select * from ChildEmployees where Empid =12345 is throwing below exception

[color=#FF0000]
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to bigint
.[/color]
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39476 Visits: 19991
The numeric test and the cast to bigint have to occur together and in the correct order.

CREATE VIEW ChildEmployees

AS

SELECT x.EmpID
FROM MasterEmployees
CROSS APPLY (
SELECT EmpiD CASE
WHEN ISNUMERIC(EmpID) = 1 THEN CAST(EmpiD AS BIGINT)
ELSE NULL END
) x
WHERE x.EmpID IS NOT NULL



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
ykmohank-911030
ykmohank-911030
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 138
It worked.
Thanks a Ton
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search