-1

I'm trying to set a column with a default value or binding of the current date in MSSQL.
I'm currently using GETDATE(), but this gives me the timestamp with the hours, minutes, seconds and milliseconds, I only need the day part (2015-03-05).

Only results I found on the web were these of changing it in the SELECT statement.

Nir Tzezana
  • 2,275
  • 3
  • 33
  • 56
  • cast(getdate() as date), You can't change the setting to get a different result for getdate() – t-clausen.dk Mar 05 '15 at 10:10
  • 1
    The field itself should be defined as type Date, not any other data type that stores the time element. `cast(getdate() as date)` is great, but also see http://stackoverflow.com/questions/2639051/what-is-the-best-way-to-truncate-a-date-in-sql-server for alternatives – Paul Maxwell Mar 05 '15 at 10:43
  • Did you try using the same suggestions you found for SELECT statements in the DEFAULT statement? – Tab Alleman Mar 05 '15 at 14:50
  • Yes, of course, I get an error stating that the input is not valid. – Nir Tzezana Mar 05 '15 at 15:06

6 Answers6

2

If you use it in a date context SQL Server will auto cast it for you

DECLARE @date DATE = GETDATE();

SELECT  @date

-- result: 2015-03-05

or you could simply use a cast

SELECT CAST(GETDATE() as DATE)

EDIT:

I'm still not sure if I get what you want, but if you want to do it as a default constraints it works the same way:

create table #table
(
    id int,
    insertDate date default GETDATE()
)

insert into #table (id) values (1)

select top 1 insertDate from #table

-- result: 2015-03-05
Staeff
  • 4,994
  • 6
  • 34
  • 58
0

If you want to store only date, excluding time you can use this:

CREATE TABLE #dts(id INT IDENTITY, d_date datetime2 DEFAULT CONVERT(char(10), GETDATE(), 126))
INSERT #dts DEFAULT VALUES 
SELECT * FROM #dts

However it will return you zeroes instead of time, as seen here:

id  d_date
-------------------------------
1   2015-03-05 00:00:00.0000000

You can remove unwanted characters using LEFT function:

SELECT id, LEFT(d_date, 10) FROM #dts

It will return you:

id  d_date
--------------
1   2015-03-05

You could achieve this by using varchar as datatype, but i dont think it would be appropiate solution and it's better to format date in select statement. But if you really need it, then this works:

CREATE TABLE #dts(id INT IDENTITY, d_date VARCHAR(10) DEFAULT CONVERT(char(10), GETDATE(), 126))
INSERT #dts DEFAULT VALUES 
SELECT * FROM #dts

Output:

id  d_date
--------------
1   2015-03-05
Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
0

Set your default value on the column to

(CONVERT([date],getdate(),0))

I have used it many times

Fred
  • 5,663
  • 4
  • 45
  • 74
0

The answer to your problem is simple. Change the format of the column that is going to hold the value to a DATE (as opposed to a data type that will hold the time portion, i.e: DATETIME).

Then set the default to GETDATE() and because the destination column is a DATE the time portion will be stripped off for you.

Take this sample code:

CREATE TABLE #temp (id INT, CreatedDate DATE DEFAULT GETDATE())

INSERT INTO #temp ( id)
VALUES ( 1 ),( 2 ),( 3 )

SELECT * FROM #temp

DROP TABLE #temp

Output:

id  CreatedDate
1   2015-03-05
2   2015-03-05
3   2015-03-05
Tanner
  • 22,205
  • 9
  • 65
  • 83
0

In other ways to convert DATETIME to DATE (get only date without time) you can use CAST to DATE format.

SELECT CAST(GETDATE() AS DATE)

In your case you can set default value type DATE without CASTing.

CREATE TABLE #TempTable
(
   Id INT,
   ..... ,
   CurrentDate DATE DEFAULT GETDATE()
)
0

You can use CONVERT() to get date in different formats. From http://www.w3schools.com/sql/func_convert.asp :

CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

gives you the following results:

Nov 04 2011 11:45 PM
11-04-11
11-04-2011
04 Nov 11
04 Nov 2011
04 Nov 2011 11:45:34:243 
lpg
  • 4,897
  • 1
  • 16
  • 16