display month as a calendar using sql

---sql server 2005 顯示一個月的數據,如果沒有空的也要顯示出來
declare @T table([geovinddate] Datetime,[workcontent] nvarchar(8),[worker] nvarchar(50))
Insert @T
select '2012-8-11',N'遲到','geovindu' union all
select '2012-8-12',N'早退','geovin'

;with Date
as
(select cast('2012-08-01' as datetime) Date
union all
select Date+1 from Date where Date+1<'2012-9-1')
select
cast(a.Date as varchar(50))+' '+ DATENAME(weekday, a.Date), [workcontent]=isnull([workcontent],''),[worker]=isnull([worker],'')
from
Date a
left join
@T b on a.Date=b.[geovinddate]

---sql server 2005:display current month as a calendar using sql
;with monthDates
as
(
select DATEADD(month, datediff(month, 0, getdate()),0) as d
,DATEPART(week, DATEADD(month, datediff(month, 0, getdate()),0)) as w
union all
select DATEADD(day, 1, d)
,DATEPART(week, DATEADD(day, 1, d))
from monthDates
where d < DATEADD(month, datediff(month, 0, getdate())+1,-1)
)

select max(case when datepart(dw, d) = 1 then datepart(d,d) else null end) as [Sun]
,max(case when datepart(dw, d) = 2 then datepart(d,d) else null end) as [Mon]
,max(case when datepart(dw, d) = 3 then datepart(d,d) else null end) as [Tue]
,max(case when datepart(dw, d) = 4 then datepart(d,d) else null end) as [Wed]
,max(case when datepart(dw, d) = 5 then datepart(d,d) else null end) as [Thu]
,max(case when datepart(dw, d) = 6 then datepart(d,d) else null end) as [Fri]
,max(case when datepart(dw, d) = 7 then datepart(d,d) else null end) as [Sat]
from monthDates
group by w

---
DECLARE @Year int, @Month int, @LastDay int;
SET @Year = 2013;
SET @Month = 5;
SET @LastDay = DAY(DATEADD(m, 1, CAST(@Year AS varchar) + '-' +
CAST(@Month AS varchar) + '-01') - 1);

WITH dates AS (
SELECT *, DOW = DATEPART(WEEKDAY, Date), WN = DATEPART(WEEK, Date)
FROM (
SELECT
Date = CAST(CAST(@Year AS varchar) + '-' +
CAST(@Month AS varchar) + '-' +
CAST(number AS varchar) AS datetime)
FROM master..spt_values
WHERE type = 'P'
AND number BETWEEN 1 AND @LastDay
) s
)
SELECT
Sun = MAX(CASE days.DOW WHEN 1 THEN dates.Date END),
Mon = MAX(CASE days.DOW WHEN 2 THEN dates.Date END),
Tue = MAX(CASE days.DOW WHEN 3 THEN dates.Date END),
Wed = MAX(CASE days.DOW WHEN 4 THEN dates.Date END),
Thu = MAX(CASE days.DOW WHEN 5 THEN dates.Date END),
Fri = MAX(CASE days.DOW WHEN 6 THEN dates.Date END),
Sat = MAX(CASE days.DOW WHEN 7 THEN dates.Date END)
FROM (SELECT DISTINCT DOW FROM dates) days
CROSS JOIN (SELECT DISTINCT WN FROM dates) weeks
LEFT JOIN dates ON weeks.WN = dates.WN AND days.DOW = dates.DOW
GROUP BY weeks.WN

---sql server 2000 顯示一個月的數據,如果沒有空的也要顯示出來
declare @d table(geovinddate datetime)
declare @date datetime
set @date='2012-08-01'
while @date<'2012-09-1'
begin
insert @d select @date
set @date=dateadd(dd,1,@date)
end
declare @t table(geovinddate datetime,workcontent varchar(20))
insert @t select '2012-8-11','遲到'
insert @t select '2012-8-12','早退'
select cast(d.geovinddate as varchar(50))+' '+ DATENAME(weekday, d.geovinddate) as '發生日期', isnull(t.workcontent,'') as '備注' from @d d left join @t t on d.geovinddate=t.geovinddate --空值不顯示

---sql server 2000
DECLARE @Month AS INT  --Set the MONTH for which you want to generate the Calendar.
DECLARE @Year AS INT  --Set the YEAR for which you want to generate the Calendar.

DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME
set  @Month = 5
set  @Year = 2013
set @StartDate= CONVERT(VARCHAR,@Year) + RIGHT('0' + CONVERT(VARCHAR,@Month),2) + '01'
set @EndDate= DATEADD(DAY,-1,DATEADD(MONTH,1,@StartDate));

SELECT
SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 1
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Sunday
,SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 2
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Monday
,SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 3
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Tuesday
,SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 4
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Wednesday
,SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 5
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Thursday
,SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 6
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Friday
,SUM(CASE WHEN DATEPART(DW, DATEADD(DD,number,@StartDate)) = 7
THEN DATEPART(DAY, DATEADD(DD,NUMBER,@StartDate)) END) AS Saturday
FROM master.dbo.spt_values v
WHERE DATEADD(DD,number,@StartDate) BETWEEN @StartDate
AND DATEADD(DAY,-1,DATEADD(MONTH,1,@StartDate))
AND v.type = 'P'
GROUP BY DATEPART(WEEK, DATEADD(DD,number,@StartDate)) 
时间: 2024-10-22 05:18:53

display month as a calendar using sql的相关文章

从SQL SERVER 向ORACLE 8迁移的技术实现方案

oracle|server  不知道从哪里得到这个文档,有用就放上来了 -gwb  数据库端SQL语法的迁移以下为常用的SQL语法迁移,包括数据类型.ID列向SEQUENCE迁移.表(主键.外键.CHECK.UNIQUE.DEFAULT.INDEX).游标.存储过程.函数.触发器.常用SQL语法与函数几个方面,考虑SQL SERVER的实际情况,没有涉及ORACLE特有的PACKAGE.EXCEPTION等.在以下的描述中,将SQL SERVER的TRANSACT-SQL简称为T-SQL.在OR

Java日期时间 java.util.Calendar和GregorianCalendar分析

Java的java.util.Calendar用于日期和时间的运算.无论任何时候,只要你有一些轻微操作(除了仅仅表示日期和时间)需要执行,那么就使用这个类.(原文:Whenever you have something slightly more advanced than just representing a date and time, this is the class to use.) java.util.Calendar类是抽象的,意味着你不能对其进行实例化.这么设计的原因是这个世界

java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类

java中的日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类 今天花了好大一点时间把java的日期类做了一下整理,哈 1. Date类(该类现在很少用了) l Date类对象的创建: n 创建一个当前时间 //默认是创建一个代表系统当前日期的Date对象 Date d = new Date(); n 创建一个我们指定的时间的Date对象: 下面是使用带参数的构造方法,可以构造指定日期的Date类对象,Date

Java中Date和Calendar常用方法_java

在java中用到的最多的时间类莫过于 java.util.Date了, 由于Date类中将getYear(),getMonth()等获取年.月.日的方法都废弃了,所以要借助于Calendar来获取年.月.日.周等比较常用的日期格式 注意:以下代码均已在jdk1.6中测试通过,其他版本可能使用不同,请注意!  Date与String的互转用法 /** * Date与String的互转用法,这里需要用到SimpleDateFormat */ Date currentTime = new Date()

学习Java的Date、Calendar日期操作_java

本文介绍了Java开发过程中日期相关操作,分享的代码如下: package jse; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; /** * 常用日期操作 * * @a

Java Date Time 教程-java.util.Calendar和GregorianCalendar

目录 实例化GregorianCalendar 获取年.月.日等 增加和减少年.月.日等 陷阱和误区 月份的陷阱 星期几的误区 JavaDoc中的更多细节 Java的java.util.Calendar用于日期和时间的运算.无论任何时候,只要你有一些轻微操作(除了仅仅表示日期和时间)需要执行,那么就使用这个类.(原文:Whenever you have something slightly more advanced than just representing a date and time,

calendar-Calendar.MONTH 返回错的值

问题描述 Calendar.MONTH 返回错的值 我想从 Calendar 中获取当前的月份. 我使用下面的两种方法: 第一: int gcmonth = Calendar.getInstance().get(Calendar.MONTH); 第二: Calendar c = (Calendar)Calendar.getInstance(); int gcmonth = c.get(Calendar.MONTH); 这两种方法都返回 2 (March), 但是我想获取 8 (September

Java中Date类和Calendar类的常用实例小结_java

前言写后台程序的时候,经常需要存储当前服务器的时间戳,使用时间戳也很方便,客户端和服务器端都可以根据自己的需求做各自的转换 在php里,获取当前时间戳使用time()函数,格式化输出通过Date()函数即可,Java里相对复杂一些,这里简单介绍一下 Java语言中的Calendar,Date,DateFormat组成了Java标准的一个基本但是非常重要的部分.日期是商业逻辑计算一个关键的部分,所有的开发者都应该能计算未来的日期,定制日期的显示格式,并将文本数据解析成日期对象 获取UNIX时间戳在

【新手求教sql小问题,大大们帮忙】

问题描述 两个问题我想找某个日期字段的数据..年份等于2007的要怎么写呢?还有某个日期字段的数据,月份=当前月的..谢谢大大们!! 解决方案 解决方案二:.....whereyear(getdate()='2007'.....wheremonth(日期字段)=month(getdate())解决方案三:SQL里边有个判断日期,时间,年份相差多少的函数,儿歌相差0则相等,函数名忘了,明天到公司再看看~~解决方案四:selectDateName(Month,getdate()) 用DateName