SQL Server Developer Center > SQL Server Forums > Transact-SQL > how to pass parameter to functions
Ask a questionAsk a question
 

Answerhow to pass parameter to functions

  • Friday, November 06, 2009 4:32 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I am having a Zip table that has zip code, longitude and latitude, city, state .. all in nvarchar(50). And I downloaded these functions below. Now I am asking how do I pass the parameters to those functions given my Zip table's data types?
    ALTER FUNCTION [dbo].[RadiusFunc]
    
    (
    	@ZipCode nvarchar(5),
    	@Miles decimal(18, 9)
    )

    RETURNS
    	@MaxLongLats TABLE
    	(
    		Latitude decimal(10,8),
    		Longitude decimal(11,8),
    		MaxLatitude decimal(10,8),
    		MinLatitude decimal(10,8),
    		MaxLongitude decimal(11,8),
    		MinLongitude decimal(11,8)
    	)
    AS
    ALTER FUNCTION [dbo].[DistanceFunc]
    (
    	@Latitude1 decimal(11,6),
    	@Longitude1 decimal(11,6),
    	@Latitude2 decimal(11,6),
    	@Longitude2 decimal(11,6)
    )


Answers

  • Friday, November 06, 2009 6:10 PMHunchbackMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    SELECT *
    FROM ZipCodes as Z cross apply RadiusFunc(Z.Zipcode , 15.0) as R
    WHERE Z.Zipcode = '83706';


    AMB
  • Friday, November 06, 2009 7:57 PMBrad_SchulzModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try this...

    SELECT *
    FROM ZipCodes
    WHERE ISNUMERIC(Latitude)=0 OR ISNUMERIC(Longitude)=0

    That will at least tell you which values are not numeric (and therefore can't be CAST or CONVERTed correctly).



    --Brad (My Blog)

All Replies

  • Friday, November 06, 2009 4:42 PMAbdshall Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    This function is missing the second part. What do you mean by this inside another function? What are you trying to do?

    ALTER FUNCTION [dbo].[DistanceFunc]
    (
    	@Latitude1 decimal(11,6),
    	@Longitude1 decimal(11,6),
    	@Latitude2 decimal(11,6),
    	@Longitude2 decimal(11,6)
    )

    Abdallah, PMP, ITIL, MCTS
  • Friday, November 06, 2009 4:45 PMSQLUSAAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am having a Zip table that has zip code, longitude and latitude, city, state .. all in nvarchar(50). And I downloaded these functions below. Now I am asking how do I pass the parameters to those functions given my Zip table's data types?
    You can use the CAST or CONVERT functions to resolve the data type issue.
    Kalman Toth, SQL Server & BI Training, SSAS, SSIS, SSRS; http://www.SQLUSA.com
  • Friday, November 06, 2009 4:58 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    This function is missing the second part. What do you mean by this inside another function? What are you trying to do?

    ALTER FUNCTION [dbo].[DistanceFunc]
    (
    	@Latitude1 decimal(11,6),
    	@Longitude1 decimal(11,6),
    	@Latitude2 decimal(11,6),
    	@Longitude2 decimal(11,6)
    )

    Abdallah, PMP, ITIL, MCTS
    No, sorry this is another function. 

    I am calling it in a stored procedure like this:
    SELECT ZIP.ZipCode, ZIP.City, 
    	dbo.DistanceFunc(ZIP.Latitude, ZIP.Longitude, RAD.Latitude, RAD.Longitude) As Distance
    	INTO #TempZips
    FROM ZipCodes ZIP, RadiusFunc('"83706"', 15.0) RAD
    WHERE (ZIP.Latitude BETWEEN RAD.MinLatitude AND RAD.MaxLatitude) AND
    	(ZIP.Longitude BETWEEN RAD.MinLongitude AND RAD.MaxLongitude) AND
    	(dbo.DistanceFunc(ZIP.Latitude,ZIP.Longitude,RAD.Latitude,RAD.Longitude) <= 15.0)
    But it's not working. Am I missing something?
  • Friday, November 06, 2009 4:59 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It doesn't give me any conversion errors, but I am just not sure how to call it correctly.
  • Friday, November 06, 2009 5:08 PMHunchbackMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Check the APPLY operator in BOL.

    > FROM ZipCodes ZIP, RadiusFunc('"83706"', 15.0) RAD

    ...
    FROM ZipCodes ZIP CROSS APPLY RadiusFunc(ZIP.ZIPCODE, 15.0) RAD
    ...


    AMB
  • Friday, November 06, 2009 5:15 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Check the APPLY operator in BOL.

    > FROM ZipCodes ZIP, RadiusFunc('"83706"', 15.0) RAD

    ...
    FROM ZipCodes ZIP CROSS APPLY RadiusFunc(ZIP.ZIPCODE, 15.0) RAD
    ...


    AMB
    Shouldn't I put the Zip.ZipCode as 83706 in that CROSS APPLY?
  • Friday, November 06, 2009 5:22 PMHunchbackMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Check the APPLY operator in BOL.

    > FROM ZipCodes ZIP, RadiusFunc('"83706"', 15.0) RAD

    ...
    FROM ZipCodes ZIP CROSS APPLY RadiusFunc(ZIP.ZIPCODE, 15.0) RAD
    ...


    AMB
    Shouldn't I put the Zip.ZipCode as 83706 in that CROSS APPLY?

    You can use the "where" clause if you are just interested in a group of rows from your zip codes table


    ...
    FROM ZipCodes ZIP CROSS APPLY RadiusFunc(ZIP.ZIPCODE, 15.0) RAD
    where zip.zipcode = "83706"
    ...


    AMB
  • Friday, November 06, 2009 5:52 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Check the APPLY operator in BOL.

    > FROM ZipCodes ZIP, RadiusFunc('"83706"', 15.0) RAD

    ...
    FROM ZipCodes ZIP CROSS APPLY RadiusFunc(ZIP.ZIPCODE, 15.0) RAD
    ...


    AMB
    Shouldn't I put the Zip.ZipCode as 83706 in that CROSS APPLY?

    You can use the "where" clause if you are just interested in a group of rows from your zip codes table


    ...
    FROM ZipCodes ZIP CROSS APPLY RadiusFunc(ZIP.ZIPCODE, 15.0) RAD
    where zip.zipcode = "83706"
    ...


    AMB
    Hunchback, that doesn't work either. It gives nothing. When I do SELECT * FROM ZipCodes WHErE Zipcode = '83706', it gives me Boise.
  • Friday, November 06, 2009 6:10 PMHunchbackMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    SELECT *
    FROM ZipCodes as Z cross apply RadiusFunc(Z.Zipcode , 15.0) as R
    WHERE Z.Zipcode = '83706';


    AMB
  • Friday, November 06, 2009 6:16 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    At least this SELECT * 
    FROM ZipCodes as Z cross apply RadiusFunc(Z.Zipcode , 15.0) as R
    WHERE Z.Zipcode = '83706'; gives me the whole table, but still doesn't display the result as Boise. Something wrong with the RadiusFunc?
    set ANSI_NULLS OFF
    set QUOTED_IDENTIFIER ON
    GO
    
    ALTER FUNCTION [dbo].[RadiusFunc]
    
    (
    	@ZipCode nvarchar(5),
    	@Miles decimal(18, 9)
    )
    
    RETURNS
    	@MaxLongLats TABLE
    	(
    		Latitude decimal(10,8),
    		Longitude decimal(11,8),
    		MaxLatitude decimal(10,8),
    		MinLatitude decimal(10,8),
    		MaxLongitude decimal(11,8),
    		MinLongitude decimal(11,8)
    	)
    AS
    
    BEGIN
    	--Declare variables
    	DECLARE @Latitude decimal(10,8), @Longitude decimal(11,8)
    	DECLARE @MaxLatitude decimal(10, 8), @MinLatitude decimal(10, 8)
    	DECLARE @MaxLongitude decimal(11, 8), @MinLongitude decimal(11, 8)
    
    	--Get the Lat/Long of the zipcode
    	SELECT @Latitude = Latitude, @Longitude = Longitude
    	FROM dbo.ZipCodes
    	WHERE ZipCode = @ZipCode
    
    	--Zipcode not found?
    	IF @@ROWCOUNT = 0
    		RETURN 
    
    	--Determine the maxes (69.17 is the # of miles/degree)
    	SET @MaxLatitude = @Latitude + @Miles / 69.17
    	SET @MinLatitude = @Latitude - (@MaxLatitude - @Latitude)
    	SET @MaxLongitude = @Longitude + @Miles / (COS(@MinLatitude * PI() / 180) * 69.17)
    	SET @MinLongitude = @Longitude - (@MaxLongitude - @Longitude)
    
    	--Insert data into return table
    	INSERT INTO @MaxLongLats
    		(Latitude, Longitude, MaxLatitude, MinLatitude, MaxLongitude, MinLongitude)
    	SELECT @Latitude, @Longitude, @MaxLatitude, @MinLatitude, @MaxLongitude, @MinLongitude
    	RETURN
    END
    
    
    
    
    
    
    
    

  • Friday, November 06, 2009 7:16 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It comes down to my latitude and logitude fields that are saved as Nvarchar(50). I cannot conver/cast those fields to numerical data type. anyone has tricks?
  • Friday, November 06, 2009 7:34 PMBrad_SchulzModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can't you use CAST or CONVERT as SQLUSA suggested?

    Or is your latitude and longitude in some non-numeric format (like "37° 37' N" and "122° 23' W")?


    --Brad (My Blog)
  • Friday, November 06, 2009 7:52 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can't you use CAST or CONVERT as SQLUSA suggested?

    Or is your latitude and longitude in some non-numeric format (like "37° 37' N" and "122° 23' W")?


    --Brad (My Blog)
    I don't understand why I cannot conver/cast it. I am trying to convert it to float, numeric, and it keeps saying Error converting from nvarchar to whatever data type I was trying to convert to.

    The values are Numbers. Latitude has "-" minus in front of it. NOt sure if that matters, but longitude are positive numbers.
  • Friday, November 06, 2009 7:57 PMBrad_SchulzModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try this...

    SELECT *
    FROM ZipCodes
    WHERE ISNUMERIC(Latitude)=0 OR ISNUMERIC(Longitude)=0

    That will at least tell you which values are not numeric (and therefore can't be CAST or CONVERTed correctly).



    --Brad (My Blog)
  • Friday, November 06, 2009 8:02 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Oh Brad, you are the man ;) hehe.... thank you. there's a date that didn't get imported correctly. It puts in the column name as fields, zip, longitude, etc.
  • Friday, November 06, 2009 8:51 PMAznDude Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yup, that's the issue due to the data type conversion. Now it gives me every city within the given distance ;) Thanks to all.