Microsoft SQL Server 2016 (RTM) - 13.0.1601.5 (X64)
Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows 10 Pro 6.3 (Build 14393)
- Server Collation: SQL_Latin1_General_CP1_CI_AS
- Language: English (United States)
I'm using internal getdate()
function to automatically fill a DateCreated
column of DATE
datatype.
As a matter of fact when a new product is entered I can see date values are being created in table in MM/dd/YYYY
format (i.e. 3/18/2017
).
Why is that? Is there a way to force getdate()
to use the ISO 8061 format?
CREATE TABLE [dbo].[Products]
(
[ID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Description] NVARCHAR (500) NOT NULL,
[IsDeleted] BIT NOT NULL,
[IsApproved] BIT NOT NULL,
[CategoryID] INT NOT NULL,
[UserID] NVARCHAR (MAX) NOT NULL,
[DateCreated] DATE DEFAULT (getdate()) NULL,
[DateExpire] DATE CONSTRAINT [CONSTRAINT_NAME] DEFAULT (dateadd(month,(1),getdate())) NULL,
[DateLastModified] DATE DEFAULT (getdate()) NULL,
CONSTRAINT [PK_dbo.Products]
PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_dbo.Products_dbo.Categories_CategoryID]
FOREIGN KEY ([CategoryID]) REFERENCES [dbo].[Categories] ([ID])
);
SYSDATETIME()
instead ofGETDATE()
- and theDATE
datatype does not have any format when stored - it's a 3-byte binary value in the table. The formatting only comes into play when you're displaying the value in e.g. Management Studio or an application of yours – marc_s