简体   繁体   English

GETDATE() 插入空格 SQL 服务器

[英]GETDATE() insert with spaces SQL Server

I have three tables in database.我在数据库中有三个表。 I have the column Operation_Date in all of them.我在所有这些列中都有Operation_Date列。 I set the column default value to ( GETDATE() ), and its data type is VARCHAR(50) and I don't have any coding in my C# Windows application that inserts value to Operation_Date .我将列默认值设置为( GETDATE() ),它的数据类型是VARCHAR(50)并且我的 C# Windows 应用程序中没有将值插入到Operation_Date的任何编码。

Issue: When I insert rows to tables the operation_Date separates month, day and year with space like this (MAY 2 2020).问题:当我向表中插入行时, operation_Date 用这样的空格分隔月、日和年(2020 年 5 月 2 日)。 I get two spaces after month and one space after days.我在一个月后得到两个空格,在几天后得到一个空格。

I tried to make a query on SQL Server to insert and the same problem occurs.我试图在 SQL 服务器上进行查询以插入,并出现同样的问题。 I tried changing data type to datetime but with not success.我尝试将数据类型更改为 datetime 但没有成功。

Note: this issue occurs only in two tables.注意:此问题仅出现在两个表中。 And I am pretty sure the three tables have the same settings.而且我很确定这三个表具有相同的设置。

Is there anyway to force a certain date format for the default value in SQL Server?无论如何强制SQL服务器中的默认值使用某种日期格式? And what could make this issue happen?什么会导致这个问题发生?

This is the query I used:这是我使用的查询:

USE [EasyManagementSystem]

INSERT INTO [dbo].[Attendance] ([Employee_No], [Present], [Leave], [Othe_Leave], [Month], [Year])
VALUES ('l-8068', 30, 0, 0, 'MAY', 2020)

Huh?嗯? Why would you be storing a date as a string.为什么要将日期存储为字符串。 That is just wrong.那是错误的。 There is a perfectly good data type for dates, called date .日期有一个非常好的数据类型,称为date

If you want this to be set on input, then just give it a default value.如果您希望在输入时设置它,那么只需给它一个默认值。 Your table creation should look like:您的表创建应如下所示:

create attendance (
    . . .
    operation_date date default getdate()
);

Voila.瞧。 It will just work, If you want the month or year, you can use the month() and year() functions.它会起作用,如果你想要月份或年份,你可以使用month()year()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM