• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

SQL Server 2000 database GETDATE() default for field

Participant ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

I have a SQL Server 2000 database where the createDate field has a default value of GETDATE() and the data type is datetime. It is inserting the date just fine, but I do not want to capture the time along with the date. I only want the date to be entered. How can I accomplish this? Thanks!
TOPICS
Advanced techniques

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

Use the convert function to truncate the time from getdate()

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

Something along the lines of this.....

CAST(CONVERT(VARCHAR, GETDATE(), 101) AS datetime)

Phil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

CAST(CONVERT(VARCHAR, GETDATE(), 101) AS datetime)

This would be done in an underlying view or query, right?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

It could be done in a query or in the column "default" statement. The choice is yours.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

never mind - I put it directly into the default value of the field and it's working. This has been a problem for days - thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

Don't forget, since your column is still type datetime, a "date" stripped of its time component is essentially midnight (time 00:00:00, etc.)

Phil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

Added to paross1's comments, I would suggest using a less ambiguous date format and/or a double convert. To guarantee the date string is converted properly.

convert(datetime, convert(varchar, getDate(), 102), 102)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

would you enter this directly into the default value for the field?

convert(datetime, convert(varchar, getDate(), 102), 102)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

IF you only wanted to store the date, then yes you would use that as the column default value.

...
createDate datetime default convert(datetime, convert(varchar, getDate(), 102), 102)

However, as paross1 cautioned be sure you understand you will only be capturing the date from now on. If you change your mind later, and decide you need both the created date and time, you will be seriously out of luck. A safer choice might be to keep the date and time, and use convert in your queries instead.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 18, 2008 Mar 18, 2008

Copy link to clipboard

Copied

LATEST
If you make it the default, you don't have to enter anything.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation