Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

user functions Expand / Collapse
Author
Message
Posted Wednesday, October 23, 2013 8:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 1:25 PM
Points: 6, Visits: 22
Write a Stored Procedure utilizing a function that receives a LocationID and determines if the LocationID is an even or
an odd number producing
Post #1507634
Posted Wednesday, October 23, 2013 8:19 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
josephmoka (10/23/2013)
Write a Stored Procedure utilizing a function that receives a LocationID and determines if the LocationID is an even or
an odd number producing


This sounds like homework. We like to help people around here, but we don't like to do homework. We will be happy to assist you but if we do your homework you don't learn anything.

What have you tried so far?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1507657
Posted Thursday, October 24, 2013 10:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 15, 2014 9:43 AM
Points: 32, Visits: 401
CREATE FUNCTION ODD_or_Even
(@Digit nvarchar(3))
RETURNS varchar(4)
AS BEGIN declare @Return varchar(30)
select @return = CASE WHEN
CAST(@Digit as decimal)/CAST(2 AS decimal)NOT LIKE '%.0%' THEN 'Odd' ELSE 'Even' END

return
@return
end



SELECT dbo.ODD_or_Even(3)
Post #1508172
Posted Thursday, October 24, 2013 11:05 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:29 PM
Points: 3,513, Visits: 7,565
If you were asked to write a stored procedure, why would you create a function?


Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1508180
Posted Thursday, October 24, 2013 11:49 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 15, 2014 9:43 AM
Points: 32, Visits: 401
Lol, I was half way there, Write a Stored Procedure utilizing a "function".


CREATE FUNCTION ODD_or_Even
(@Digit nvarchar(3))
RETURNS varchar(4)
AS BEGIN declare @Return varchar(30)
select @return = CASE WHEN
CAST(@Digit as decimal)/CAST(2 AS decimal)NOT LIKE '%.0%' THEN 'Odd' ELSE 'Even' END

return
@return
end

CREATE PROCEDURE AmIOddorEven

@Digit nvarchar (3) = '3'

AS



SELECT dbo.ODD_or_Even(@Digit)
Post #1508199
Posted Thursday, October 24, 2013 12:00 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:29 PM
Points: 3,513, Visits: 7,565
Now, there's a small problem. What happens if you ever get a value like 'a23'?
You should use a numeric datatype, and for even/odd numbers problem, the best type could be int.

To find out if a number is even or odd, the classic way to go is with a modulo operator.
WITH Tally(n) AS(
SELECT TOP 11 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) - 1
FROM sys.all_columns)
SELECT n, CASE WHEN n % 2 = 1 THEN 'Odd' ELSE 'Even' END
FROM Tally




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1508203
Posted Thursday, October 24, 2013 12:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, August 15, 2014 9:43 AM
Points: 32, Visits: 401
Where is the Stored Procedure? Lol
Post #1508205
Posted Thursday, October 24, 2013 12:03 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:29 PM
Points: 3,513, Visits: 7,565
If this is homework and want to impress your instructor, you should take a look at this article.
http://www.sqlservercentral.com/articles/T-SQL/91724/



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1508207
Posted Thursday, October 24, 2013 12:48 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
Honestly, the function posted is fraught with issues. There is all kinds of casting/converting both explicitly and implicitly.

If we are going to demonstrate how to do somebody's homework we should actually go to the next level and so them the right way to do this. Scalar functions are notoriously poor regarding performance. Instead of a scalar function converting this to an Inline Table Valued Function (iTVF) would be orders of magnitude better than the scalar function proposed.

Something like this.

CREATE FUNCTION ODD_or_Even
(
@Digit int
)
RETURNS table with schemabinding
AS
return
select case when @Digit % 2 = 0 then 1 else 0 end as IsEven

OK. So now we have a function but so what? We could easily create a select statement and stick that after a create procedure. That would fulfill the requirements but misses the point entirely. How about if we create a stored proc that will return the numbers from 1 to x and state if each of them is Odd or Even. This is easy now that we made our function an iTVF instead of a scalar.

create procedure GetOddOrEvenValues
(
@NumResults int
) as

WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)

select *
from cteTally t
cross apply dbo.ODD_or_Even(t.N)
where t.N <= @NumResults

Alright. Now we have a stored proc that is utilizing a function.

All that is left is to call our newly created stored proc.

exec GetOddOrEvenValues 123 --change the 123 to any positive number and it will return that many rows.



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1508225
Posted Thursday, October 24, 2013 12:59 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:29 PM
Points: 3,513, Visits: 7,565
ccavaco (10/24/2013)
Where is the Stored Procedure? Lol

There's none, it was just an example on how to do things correctly



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1508229
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse