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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL - Current Year

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 method
1. pass the getdate() into your function
select dbo.your_function(getdate())


2. create a view with getdate and use the view in your function

create view your_view
as
select getdate() as current_datetime

create function current_year()
returns int
as
begin
return (select year(current_datetime) from your_view)
end

select dbo.current_year()




KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-05-30 : 05:58:48
or

Select datepart(year, getdate())


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 reference
2) Lookup to a table (tblXYZ)
2) Pull the values for this person from fields A and B
3) Compare the two, and let me know whether A > B or B > A

What 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.
Go to Top of Page

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 reference
2) Lookup to a table (tblXYZ)
2) Pull the values for this person from fields A and B
3) Compare the two, and let me know whether A > B or B > A

What 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

Go to Top of Page
   

- Advertisement -