Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2006-05-30 : 05:39:27
|
| Hello,I'm trying to write a function which will give me, among other things, the current year.I'm brand new to SQL so don't really know how to use functions properly yet, although from my VB knowledge, the structure looks very similar.I've tried SELECT GETDATE() but it gives me an error about getdate not being valid inside a function.How can I get this to work please ?Cheers,J. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-30 : 05:45:52
|
"I'm trying to write a function which will give me, among other things, the current year."You don't really need to write a function to do this. select year(getdate()) "I've tried SELECT GETDATE() but it gives me an error about getdate not being valid inside a function."you can't use getdate() inside a function.There are alternative method1. pass the getdate() into your function select dbo.your_function(getdate()) 2. create a view with getdate and use the view in your functioncreate view your_viewasselect getdate() as current_datetimecreate function current_year()returns intasbegin return (select year(current_datetime) from your_view)endselect dbo.current_year() KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-30 : 05:58:48
|
| orSelect datepart(year, getdate())MadhivananFailing to plan is Planning to fail |
 |
|
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2006-05-30 : 07:45:15
|
| Hi again,Can you please let me know how I would approach the following :I want to write a function (is this the best way?) which will :1) Pass in a person's reference2) Lookup to a table (tblXYZ)2) Pull the values for this person from fields A and B3) Compare the two, and let me know whether A > B or B > AWhat would be the best way to approach this ?Any advice would be greatly appreciated as I'm literally brand new to SQL.Thank you,J. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-30 : 08:48:20
|
quote: Originally posted by Jonny1409 Hi again,Can you please let me know how I would approach the following :I want to write a function (is this the best way?) which will :1) Pass in a person's reference2) Lookup to a table (tblXYZ)2) Pull the values for this person from fields A and B3) Compare the two, and let me know whether A > B or B > AWhat would be the best way to approach this ?Any advice would be greatly appreciated as I'm literally brand new to SQL.Thank you,J.
You have to post the relevant table structure, some sample data and the result you want KH |
 |
|
|
|
|
|
|
|