简体   繁体   English

系统年份 YYYY 加上 coulmn 的值 MMDD 在 C# 中作为“dd-MMM-yyyy”插入到数据库中

[英]System year YYYY plus coulmn's value MMDD insert into Database as 'dd-MMM-yyyy' in C#

I am to read text file where a column of purchase date with format MMDD is available.我要阅读文本文件,其中有一列格式为 MMDD 的购买日期可用。 It is being read and inserted all records successfully.正在读取并成功插入所有记录。 But now i want to insert "system year" with "purchase date" as "dd-MMM-yyyy" into date column of database.但是现在我想将“系统年份”和“购买日期”作为“dd-MMM-yyyy”插入到数据库的日期列中。 Example: If purchase date is "0812" and System Year is '19', then date will be "12-AUG-19".示例:如果购买日期为“0812”且系统年份为“19”,则日期将为“12-AUG-19”。 How can i do this ?我怎样才能做到这一点 ?

As of Oracle, you'd apply a few transformations :从 Oracle 开始,您将应用一些转换

  • concatenate value you have (0812) with this year (which is what extract does)将您拥有的值 (0812) 与今年连接起来(这就是extract作用)
  • apply to_date to it, with appropriate format mask ( mmddyyyy )to_date应用到它,并使用适当的格式掩码( mmddyyyy
  • apply to_char to that value, with desired/final format mask ( dd-mon-yyyy )to_char应用于该值,使用所需/最终格式掩码( dd-mon-yyyy

Something like this:像这样的东西:

SQL> select to_char(to_date('0812' || extract(year from sysdate), 'mmddyyyy'), 'dd-mon-yyyy') result from dual;
                             ----
                             this is what you have, MMDD

RESULT
-----------
12-aug-2019

SQL>

If you are looking for C# code then, you can first Parse , then create a required DateTime and, finally, format the DateTime :如果您正在寻找C# 代码,则可以先Parse ,然后创建所需的DateTime ,最后格式化DateTime

  string source = @"0812";
  int SystemYear = 19;

  // Parsed date, note, that year is DateTime.Now.Year, not required SystemYear
  DateTime date = DateTime.ParseExact(source, "MMdd", CultureInfo.InvariantCulture);

  date = new DateTime(
    CultureInfo.InvariantCulture.Calendar.ToFourDigitYear(SystemYear), // SystemYear
    date.Month,                                                        // same Month
    date.Day);                                                         // same Day

  // "12-AUG-19"
  string result = date.ToString("dd'-'MMM'-'yy", CultureInfo.InvariantCulture).ToUpper();

It depends what is data format in your tables column.这取决于表列中的数据格式是什么。 If it's type of Date, then showing 12-AUG-19 is just a display issue, and has nothing to do with inserting.如果它是日期类型,那么显示12-AUG-19只是一个显示问题,与插入无关。 If it's varchar column, then you first need to read your date from source fe:如果它是 varchar 列,那么您首先需要从源 fe 读取您的日期:

var source = "0812";
source += DateTime.Now.Year;
Console.WriteLine(source);
var date = DateTime.ParseExact(source, "MMddyyyy", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(date);

So now you have date as DateTime object, and it depends on your DB structure if you will post it as string to DB, or as proper Date column.所以现在你有日期作为DateTime对象,这取决于你的数据库结构,如果你将它作为string到数据库,或者作为适当的日期列。

In Oracle you can insert a date using many different formats, so it would not be necessary to have the format 'DD-mon-YY' to insert the value in a date column.在 Oracle 中,您可以使用多种不同的格式插入日期,因此不必使用“DD-mon-YY”格式来在日期列中插入值。 The TO_DATE function uses the current year value by default to complete the format: TO_DATE 函数默认使用当前年份值来完成格式:

SQL> SELECT TO_DATE('0812', 'MMDD')from dual;

TO_DATE('0812','M
-----------------
12/08/19 00:00:00

And this is an example of what I mean:这是我的意思的一个例子:

SQL> CREATE TABLE DATE_SAMPLE ( DATE_VALUE DATE);

Table DATE_SAMPLE creado.

SQL> INSERT INTO DATE_SAMPLE SELECT TO_DATE('0812', 'MMDD')from dual;
1 fila insertadas.

SQL> SELECT TO_CHAR(DATE_VALUE, 'DD-mon-YY') from DATE_SAMPLE;

TO_CHAR(DATE_VALUE,'DD-MON-YY')                                            
---------------------------------------------------------------------------
12-ago-19

You can insert a date specifying the format in the TO_DATE function and read it in the desired format using the TO_CHAR function.您可以在 TO_DATE 函数中插入指定格式的日期,并使用 TO_CHAR 函数以所需格式读取它。

TO_DATE 迄今为止

TO_CHAR TO_CHAR

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

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