all groups > sql server programming > september 2007 >
You're in the

sql server programming

group:

my first attemp at a function... one error left, but not sure why


Re: my first attemp at a function... one error left, but not sure why RMJCS
9/20/2007 9:42:57 PM
sql server programming: 'ello again :) It looks like you're quite close, this might be what
you're after.

CREATE FUNCTION dbo.func_CnvtHMS2DD( @HMS nvarchar(20) ) RETURNS
NUMERIC(10,7)
AS
BEGIN
Declare @hours NUMERIC(10,7),
@minutes NUMERIC(10,7),
@seconds NUMERIC(10,7),
@result NUMERIC(10,7)

Set @hours = Substring(@HMS, 2, CharIndex('-', @HMS) - 2)
Set @minutes = Substring(@HMS, CharIndex('-', @HMS) + 1,
CharIndex('.',@HMS) - 5)
Set @seconds = Substring(@HMS, CharIndex('.', @HMS) + 1, Len(@HMS))

set @minutes = @minutes / 60
set @seconds = @seconds / 3600

SET @result = @hours + @minutes + @seconds

RETURN @result

END

I've reworked it for the sake of SQL but I haven't checked the maths.
Use like this;

SELECT dbo.func_CnvtHMS2DD('N51-40.4.')

Good luck, Rhys

[quoted text, click to view]

Re: my first attemp at a function... one error left, but not sure why RMJCS
9/20/2007 10:05:21 PM
You need to qualify the function name with it's schema, so it is
called dbo.func_CnvtHMS2DD and not just func_CnvtHMS2DD. Off the top
of my head I can't remember the reason for the two part naming
requirement, but it's the answer anyway!

Regards, Rhys

[quoted text, click to view]

my first attemp at a function... one error left, but not sure why Aussie Rules
9/20/2007 10:17:54 PM
Hi,

I have some data in a field that I need to convert into a new value. I want
to be able to select from my table, but have the resultset have the
converted values, not the actual table values.

for example, a normal select against the table with the actual values would
be

select field1, field2, field3, field4, field5

this would return

value1, value2, value3, value5, value5.

I want to do some math, and convert value3 and value4 to new values.

select field1, field2, func_CnvtHMS2DD(field3), func_CnvtHMS2DD(field4),
field5

this would return

value1, value2, value9, value45, value5.

From reading a bit, using a function would do this (I think) so I have had a
go at do my first function, however I get the message 'A RETURN statement
with a return value cannot be used in this context.'

What have i done wrong below ?


CREATE FUNCTION func_CnvtHMS2DD
( @HMS nvarchar(20)

)
RETURNS @tbl TABLE (val nvarchar(30))
AS

BEGIN
Declare @hours int,
@minutes int,
@seconds int

Set @hours = Substring(@HMS, 2, CharIndex('-', @HMS) - 2)
Set @minutes = Substring(@HMS, CharIndex('-', @HMS) + 1, CharIndex('.',
@HMS) - 5)
Set @seconds = Substring(@HMS, CharIndex('.', @HMS) + 1, Len(@HMS))

set @minutes = @minutes /60
set @seconds = @seconds/3600

RETURN
(
-- Add the SELECT statement with parameter references here
Select @hours + @minutes + @seconds As DDValue

)


END
Re: my first attemp at a function... one error left, but not sure why Aussie Rules
9/20/2007 10:47:34 PM
Hi,

Thanks for you help.

Almost perfect, however is it possible, to a field name in.

The database looks like this
longitude latitude name notes
S33-56.8 E151-10.6 Sydney king


i want to be able to select back a resultset with the fields longitude and
latitude converted.

You sample select statement works ' SELECT
dbo.func_CnvtHMS2DD('N51-40.4.')'

however I need to do this :

select longitude , latitude, func_CnvtHMS2DD(longitude) from tbl_location

but i get this error :
'func_CnvtHMS2DD' is not a recognized built-in function name.



[quoted text, click to view]
Re: my first attemp at a function... one error left, but not sure why changliw NO[at]SPAM online.microsoft.com
9/21/2007 6:17:09 AM
Hi Aussie and Rhys,
The reason is that Scalar-valued functions must be invoked by using at
least the two-part name of the function.
You may refer to the section "Function Invocation" in this article:
CREATE FUNCTION (Transact-SQL)
http://msdn2.microsoft.com/en-us/library/ms186755.aspx

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
AddThis Social Bookmark Button