Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Format 1

Status
Not open for further replies.

kp1279

Programmer
Jan 21, 2005
43
GB
Hi

I have a database I am building in Enterprise manager, What I am looking at achieving is having a fields to return the current date.

The field, I have set as Smalldatetime, and have the Default value set to (getdate()).

This returns the whole date and time, however, how do I get this to return just the current year??????

KP
 
YEAR(GETDATE())

or

DATEPART(yy, GETDATE())

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Code:
SELECT DatePart(yyyy, YourSmallDateField)

Check the exact syntax of the function because I can't right now


Borislav Borissov
 
I must be doing something wrong!!

I have tried the

datepart(yy,getdate()) and YEAR(GETDATE())

I have put this into the default value field, but when I create a record, I now get 29/06/1905

WHY?????

I have checked the system date, but that is right, and if I use just getdate() as a default value, thats OK???

Confused or what!
 
You said:

> how do I get this to return just the current year??????

Does it mean you want year (2005) or beginning of year (Jan 01st, 2005)?

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
You put YEAR(GetDate()) in default value of the field that is dateTime type :) you get 2005 days AFTER 01/01/1900, Convert field type to char(4) and then put that function. Other way is put as default value for DateTime fiedl GetDate() and in ALL your queries instead of SELECT YourField use SELECT YEAR(YourField) to get onlythe year of the date.

Borislav Borissov
 
Many thanks to all of you.

I have changed the field from 'smalldatetime' to 'char' and used the default value of year(getdate()), and it works perfect, it now brings in the current year the records are created.

Many thanks again.

KP
[bigcheeks]
 
Suggestion: better use smallint instead of char. Since year value is integer by nature, this makes sense.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top