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


user functions


user functions

Author
Message
josephmoka
josephmoka
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 27
Write a Stored Procedure utilizing a function that receives a LocationID and determines if the LocationID is an even or
an odd number producing
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26816 Visits: 17557
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 Modens 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)
ccavaco
ccavaco
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 490
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)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17098 Visits: 19122
If you were asked to write a stored procedure, why would you create a function? Hehe


Luis C.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
ccavaco
ccavaco
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 490
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)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17098 Visits: 19122
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
ccavaco
ccavaco
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 490
Where is the Stored Procedure? Lol
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17098 Visits: 19122
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26816 Visits: 17557
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 Modens 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)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17098 Visits: 19122
ccavaco (10/24/2013)
Where is the Stored Procedure? Lol

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


Luis C.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
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