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
 How to get current year from SQL query?

Author  Topic 

Swati Jain
Posting Yak Master

139 Posts

Posted - 2007-10-22 : 10:21:02
How to get current year from SQL query?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-22 : 10:25:17
year(getdate())


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-23 : 02:11:38
or

Select datepart(year,getdate())


Madhivanan

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

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-23 : 02:14:12
All suggestions above assumes you have the proper datetime set on the sql server where the query is run.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-23 : 03:49:05
Duh !


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-23 : 05:10:26
Hey! Just to be sure...



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-23 : 05:24:27
Time zones, daylight saving (or not), etc. to be considered when using SQL server time ... but also the benefit that its a consistent time for ALL users, whatever time zone THEY are in

Often overlooked is data entered during the 1 hour when the clocks have gone back in the Fall/Autumn ...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-23 : 07:08:02
OP only wanted the current year, will daylight saving changes occurs at Dec 31 or Jan 1 ?




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-23 : 07:10:19
I was just trying to "talk it up"

Mind you ... if server is operating without daylight savings, in order to NOT have the one-hour-lost problem in the fall, then between 11pm and midnight on 31st December there might be a real problem getting a valid year.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-23 : 07:12:09
quote:
Originally posted by Kristen

I was just trying to "talk it up"

Mind you ... if server is operating without daylight savings, in order to NOT have the one-hour-lost problem in the fall, then between 11pm and midnight on 31st December there might be a real problem getting a valid year.


Are you pulling my leg ? Sorry, i come from a time zone where daylight saving is totally foreign to me.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-23 : 07:16:44
Do you not have Summer time then?

In Fall/Autumn the clocks go "back" one hour. This happens at 3am I think, so you get 2:58, 2:59, 2:00, 2:01 ... 2:58, 2:59, 3:00 ...

If you have database synchronisation based on timestamp on records this could obviously cause you to miss some changes.

We therefore set our SQL boxes to NOT adjust for daylight saving time.

Which in turns means that they are an hour different for part of the year. (I can't remember which, but I think they are correct in Summer and one-hour-different in Winter).

So there is a window-of-opportunity within an hour of 31-December-midnight to get the wrong year.

I'm sure this is a viewpoint which is excessively pedantic, but may be useful to be aware of in some circumstances.

Kristen
Go to Top of Page
   

- Advertisement -