如何在使用sql的表中的两个日期之间插入星期六和星期日?

问题描述:

说开始日期是2010年8月4日 结束日期是2011年8月4日 我需要周六和周日的日期在表格中插入... 需要帮助如何在使用sql的表中的两个日期之间插入星期六和星期日?

+0

哪个rdbms?微软的SQL服务器,MySQL的,甲骨文... – RichardTheKiwi 2011-04-12 10:00:30

使用一个明确的日期格式,如'2010-08-04'。这种排序方式无论是日期还是文本,每个人都知道它意味着4月8日,而不是4月8日。

我喜欢用calendar table这样的查询。这将选择日期。

select cal_date 
from calendar 
where cal_date between '2010-08-04' and '2011-08-04' 
and (day_of_week = 'Sat' or day_of_week = 'Sun'); 

像这样的东西会将它们插入到表中。 (根据表格而定)

insert into your_table_name 
select cal_date 
from calendar 
where cal_date between '2010-08-04' and '2011-08-04' 
and (day_of_week = 'Sat' or day_of_week = 'Sun'); 
+2

大家都知道它是04年8月4日,除了SQL Server,这将返回4这个查询:'设置语言英国 选择DATEPART(月,CONVERT(datetime,'2010-08 -04' ))'。用于转换为日期时间的明确的仅限日期的格式不包括组件之间的破折号,例如, ''20100804'' – 2011-04-12 10:48:53

+0

@Damien_The_Unbeliever:你说得对。格式'yyyy-mm-dd'只是SQL标准。 – 2011-04-12 11:18:52