创建表列日期格式 dd-mm-yyyy SQL Server

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20158641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:14:20  来源:igfitidea点击:

Create Table column date format dd-mm-yyyy SQL Server

sqlsql-server

提问by Rajiv

I have a table where there are 3 columns to store different dates for different purposes. I would like to store the date in dd-mm-yyyy. Below is the code:

我有一个表,其中有 3 列用于存储不同目的的不同日期。我想将日期存储在dd-mm-yyyy. 下面是代码:

    create table PoojaDetails
(
    PoojaDetailsID int identity constraint pk_PoojaDetailsID  Primary Key, 
    ReceiptNo AS 'PB' + '/' + cast(datepart(yy,getdate()) as varchar(25)) + '/' + RIGHT('000000000' + CAST(PoojaDetailsID AS VARCHAR(10)), 9) ,
    ReceiptDate date not null constraint df_ReceiptDate default convert(date,getdate()),
    FirstName varchar(100) not null,
    LastName varchar(100) not null,
    TelNo bigint,
    Star char(50) not null,
    Rasi char(50) not null,
    Gothram char(100) not null,
    PoojaDietyMasterID int not null,
    Schedule char(1) not null constraint df_schedule default 'F',
    PoojaDate date not null constraint df_pdate default convert(date, '29122013'),
    PayMode bit not null,
    isDonate bit not null constraint df_isDonate default 1,
    DonateAmount float(10),
    ChequeNo int,
    BankName varchar(255),
    ChequeDated date,
    UserID int,
    SupID int,
    ChangeDate date,
    Remarks varchar(255),
    isPrint char(1) constraint df_isPrint default 'N',
    isDeleted bit not null constraint df_isDeleted default 1
)

I would like to have the format for:
ReceiptDate
PoojaDate
ChequeDate
ChangeDate

我想要的格式为:
ReceiptDate
PoojaDate
ChequeDate
ChangeDate

Thanks :)

谢谢 :)

回答by Raj

Your best bet will be to store the date (if you are using SQL 2008, you should use the DATEdatatype) in the universal format of yyyymmddin the database and then use

您最好的选择是以数据库DATE中的通用格式存储日期(如果您使用的是 SQL 2008,则应使用数据类型)yyyymmdd,然后使用

CONVERT(Date,YourColumn,105)

when reading the data, to get it in the format you desire.

在读取数据时,以您想要的格式获取它。

回答by The Hungry Dictator

CONVERT(VARCHAR(10), ReceiptDate, 105) AS [DD-MM-YYYY]
CONVERT(VARCHAR(10), PoojaDate, 105) AS [DD-MM-YYYY]
CONVERT(VARCHAR(10), ChequeDate , 105) AS [DD-MM-YYYY]

use this linkfor referrence....

使用此链接作为参考....