Ms Sql A Domain Error Occurred
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to fix “domain error” in SQL Server 2005 when using LOG() function to get product of set up vote 1 down vote favorite I have a inline select statement to calculate the product of the set of values. Since SQL Server 2005 doesn't have a built in Product aggregate function, I am using LOG/EXP to get it. My select statement is: (select exp(sum(log(value))) from table where value > 0) Unfortunately I keep getting the following error: Msg 3623, Level 16, State 1, Line 1 A domain error occurred. I've ensured that none of the values are zero or negative so I'm not really sure why this error is occurring. Does anyone have any ideas? sql-server sql-server-2005 tsql share|improve this question asked Oct 14 '09 at 5:27 user189600 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted One of the features of the query planner introduced in SQL 2005 is that, in some circumstances where the table statistics indicate it will be more efficient, the WHERE clause of a statement will be processed after the SELECT clause. (I can't find the Books On-Line reference for this right now). I suspect this is what is happening here. You either need to exclude the rows where value = 0 before carrying out the calculation - the most reliable way being to store the rows you need in a temporary (#) table - or to modify your query to handle zero internally: SELECT EXP(SUM(LOG(ISNULL(NULLIF(VALUE,0),1)))) AS result FROM [table] The NULLIF\ISNULL pair I have added to your query substitutes 1 for 0 - I think this will work, but you will need to test it on your data. share|improve this answer answered Oct 14 '09 at 7:06 Ed Harper 16.1k33562 You're right, I believe this is exactly what was happening. What I ended up doing was t
Recent PostsRecent Posts Popular TopicsPopular Topics Home Search Members Calendar Who's On Home » SQL Server 2005 » SQL Server 2005 General Discussion » A domain error occurred in Sql server 2005... A domain error occurred in Sql server 2005 while updating the data Rate Topic Display Mode Topic Options Author Message roshan.nc 74309roshan.nc 74309 Posted Monday, April 22, 2013 7:21 AM Grasshopper Group: General Forum Members Last Login: Monday, September 21, 2015 1:05 AM Points: 14, Visits: 47 I have created a function like the http://stackoverflow.com/questions/1564405/how-to-fix-domain-error-in-sql-server-2005-when-using-log-function-to-get-pr one belowCreate FUNCTION fn_Calc(@Lat1 Float, @Lng1 Float, @Lat2 Float, @Lng2 Float)RETURNS FloatASBEGINDeclare @x as FloatDeclare @y as FloatDeclare @Distance as FloatSelect @x = (SIN(RADIANS(@Lat1)) * SIN(RADIANS(@Lat2)) + COS(RADIANS(@Lat1)) * COS(RADIANS(@Lat2)) * COS(ABS((RADIANS(@Lng2)) - (RADIANS(@Lng1)))))Select @y = ATAN((SQRT(1-(POWER(@x,2))) / @x))Select @Distance = (1.852 * 60.0 * ((@y / PI()) * 180)) / 1.609344RETURN @DistanceENDI am using the above function to update a column in a table like http://www.sqlservercentral.com/Forums/Topic1444977-149-1.aspx below:Update test set calc = dbo.fn_Calc( cast(Lat as float), cast(Long as float), dblLat, dblLong) While running the above query I got the error."A domain error occured."What can be causing this error?Thanks,Roshan. N Post #1444977 wolfkilljwolfkillj Posted Tuesday, April 23, 2013 10:26 AM Ten Centuries Group: General Forum Members Last Login: Monday, August 24, 2015 12:20 PM Points: 1,064, Visits: 2,582 I had this problem once before, but I was using a slightly different formula for calculating distances between lat/long pairs. Unfortunately, I no longer work for my employer at the time and don't have the notes I made of this issue to be more specific, so I only vaguely recollect the circumstances. IIRC, I discovered that there were certain input values that would cause the T-SQL COS function to return results just slightly outside the -1 < x < 1 range. This cascaded through the calculation and resulted in an input to the ACOS function that also fell outside the -1 < x < 1 range, which generated the domain error. I corrected the problem by adjusting any results of the COS function that fell outside the range to 1 or -1 as appropriate. The
SQL Server experts to answer http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=184755 whatever question you can come up with. Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums. SQL Server Forums Profile | ActiveTopics | Members | Search | ForumFAQ Register Now and get your question ms sql answered! Username: Password: Save Password Forgot your Password? All Forums SQL Server 2005 Forums Transact-SQL (2005) A domain error occurred in Sql server Reply to Topic Printer Friendly Author Topic roshanrise Starting Member India 1 Posts Posted-04/22/2013: 09:24:42 I have created a function like ms sql a the one belowALTER FUNCTION fn_Calc(@Lat1 Float, @Lng1 Float, @Lat2 Float, @Lng2 Float)RETURNS FloatASBEGINDeclare @x as FloatDeclare @y as FloatDeclare @Distance as FloatSelect @x = (SIN(RADIANS(@Lat1)) * SIN(RADIANS(@Lat2)) + COS(RADIANS(@Lat1)) * COS(RADIANS(@Lat2)) * COS(ABS((RADIANS(@Lng2)) - (RADIANS(@Lng1)))))Select @y = ATAN((SQRT(1-(POWER(@x,2))) / @x))Select @Distance = (1.852 * 60.0 * ((@y / PI()) * 180)) / 1.609344RETURN @DistanceENDI am using the above function to update a column in a table like below:Update test set calc = dbo.fn_Calc( cast(Lat as float), cast(Long as float), dblLat, dblLong) While running the above query I got the error."A domain error occured."What can be causing this error?Thanks,Roshan. NThanks,Roshan.N Edited by - roshanrise on 04/22/2013 09:25:33 bandi Flowing Fount of Yak Knowledge India 2242 Posts Posted-04/22/2013: 09:42:48 Check this link http://books.google.co.in/books?id=bTIq5XpJgTkC&pg=PA106&lpg=PA106&dq=%22A+domain+error+occurred.%22+in+sql+server&source=bl&ots=d1WfCcx8DQ&sig=gARctqqHx6V1ywHaaBhNIsS0SVg&hl=en&sa=X&ei=fz11UdqWLsTorQew5IGICA&ved=0CIgBEOgBMAk#v=onepage&q=%22A%20domain%20error%20occurred.%22%20in%20sql%20server&f=falseSome trigonometric or LOG functions can throw domain error in the case of NULL values... http://stackoverflow.com/questions/1564405/how-to-fix-domain-error-i