如何在全年的数据库中每周输入日期 [英] How to enter dates in weekly manner in database for the whole year

查看:66
本文介绍了如何在全年的数据库中每周输入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按周模式将数据输入到全年的表格中,如下所示:

表格名称:-Date_week

周date1 date2

w1 2011年2月1日2011年8月1日
w2 2011年9月1日2011年1月15日
w3 2011年1月16日2011年2月1日
.
.
.
.
wi 25/12/2011 31/12/2011


为此,我尝试如下:

I want to enter data into a table of the whole year in weekly pattern as follows:

Table name:- Date_week

week date1 date2

w1 02/01/2011 08/01/2011
w2 09/01/2011 15/01/2011
w3 16/01/2011 22/01/2011
.
.
.
.
wi 25/12/2011 31/12/2011


For this I tried as follows:

w varchar2(3);
d1 varchar2(10):= ''02/01/2011'';
d2 varchar2(10):=to_char(to_date(d1+7));
d3 varchar2(10):=''31/12/2011'' ;
i integer:=1;

while d1<>d3 loop
    insert into Date_week(week,date1,date2)values(wi,d1,d2);
    d1:=to_char(to_date(d2+1));
    d2:=to_char(to_date(d1+7));
    i:=i+1;
end loop;



然后调用此函数,但出现错误.

谁能帮帮我.
在此先感谢.



Then called this function but I get an error.

Can any one help me out.
Thanks in advance.

推荐答案

首先想到的是:不要将日期作为字符串存储(或尝试使用).请改用日期"字段.然后在绝对需要时(而不是之前)转换为字符串以呈现给用户.
这消除了所有to_date和to_char函数调用,使数据库更小,代码更明显,并且意味着将来您可以更轻松地使用date字段进行比较.

这也将解决您的问题:默认情况下,oracle中的所有日期都略有奇数:"DD-MON-YY",因此您的初始转换将失败两个月份:不好的月份(因为是数字)和不好的年份(因为有四位数)
The first thing that springs to mind is: don''t store (or try to work with) dates as strings. Use Date fields instead. Then convert to a string when you absolutely need to, for presentation to the user, and not before.
This gets rid of all your to_date and to_char function calls, and makes you database smaller, your code more obvious, and means that in future you can use the date fields for comparison more easily.

It would also get rid of your problem: by default all dates in oracle are slightly odd: "DD-MON-YY" so your initial conversion will fail on two counts bad month (because it is numeric) and bad year (because it has four digits)


这篇关于如何在全年的数据库中每周输入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆