请教:如何根据程序提交的参数来和数据表中的记录计算并显示其值?

chuxinfo 2008-01-03 09:01:29
我想根据程序提交的参数来和数据表中的记录计算并显示其值?
例如:
我有一张学校班级表(Class_info)
class_id class_name class_inyear class_outyar
1 十班 1995 1998
2 十二班 1995 1998
3 十五班 1996 1999
4 十八班 1997 2000
5 二十班 1998 2001
6 三十班 1999 2002
'------------------------------------------------
class_inyear和class_outyar为(int型)代表入学和毕业年份,入学1995也就是1995/09;毕业1998也就是1998/07
假如我现在程序传过来的参数为:1998/09
那么我现在想计算1998/09这个学期位于在校的各班级整个在校学习期间的第几学期
(一年级为:1-2学期)(二年级为:3-4学期)(三年级为:5-6学期),如果毕业的班级就不显示计算其值了
其SQL运算后显示结果应是这样(class_term为我要计算的1998/09位于该年级的第几学期,毕业或没来的就不管了):
class_id class_name class_inyear class_outyear class_term
3 十五班 1996 1999 5
4 十八班 1997 2000 3
5 二十班 1998 2001 1
'-----------------------------------------------
我总结的一点规律:判断程序传过来的参数1998/09,计算出是09月份还是03月份(我只有这两个固定值,因没学年有两个学期)
如果月份是09则class_term=(参数年份1998-入学年份)*2+1 否则class_term=(参数年份1998-入学年份)*2
但具体怎么操作我就不知了.
根据程序传过来的参数计算在校班级是:
select * from Class_info where CONVERT(nvarchar,class_outyear) + '/07' >= '1998/09') AND CONVERT(nvarchar,class_inyear) + '/09' <= '1998/09'

但是我该如何生成class_term呢?请求各位帮忙!
...全文
74 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuxinfo 2008-01-03
  • 打赏
  • 举报
回复
谢谢
dawugui 2008-01-03
  • 打赏
  • 举报
回复
create table tb(class_id int, class_name varchar(10), class_inyear int, class_outyar int)
insert into tb values(1, '十班' , 1995, 1998 )
insert into tb values(2, '十二班', 1995, 1998 )
insert into tb values(3, '十五班', 1996, 1999 )
insert into tb values(4, '十八班', 1997, 2000 )
insert into tb values(5, '二十班', 1998, 2001 )
insert into tb values(6, '三十班', 1999, 2002 )
go
declare @dt as varchar(7)
set @dt = '1998/09'

select * , class_term =
case
when class_outyar > cast(left(@dt , charindex('/',@dt) - 1) as int) and substring(@dt , charindex('/',@dt) + 1 , 2) = '09' then (cast(left(@dt , charindex('/',@dt) - 1) as int) - class_inyear) * 2 + 1
when class_outyar > cast(left(@dt , charindex('/',@dt) - 1) as int) and substring(@dt , charindex('/',@dt) + 1 , 2) = '03' then (cast(left(@dt , charindex('/',@dt) - 1) as int) - class_inyear) * 2
end
from tb where class_outyar > cast(left(@dt , charindex('/',@dt) - 1) as int) and cast(left(@dt , charindex('/',@dt) - 1) as int) >= class_inyear

/*
class_id class_name class_inyear class_outyar class_term
----------- ---------- ------------ ------------ -----------
3 十五班 1996 1999 5
4 十八班 1997 2000 3
5 二十班 1998 2001 1

(3 行受影响)
*/

drop table tb
chuxinfo 2008-01-03
  • 打赏
  • 举报
回复
把参数假设成1998/09,SQL语句应为
select *,
case Right('1998/09',2)
when '03' then (Left('1998/09',4)-class_inyear)*2+1
else (Left('1998/09',4)-class_inyear)*2
end as class_term
from Class_info where
CONVERT(nvarchar,class_outyear) + '/07' >= '1998/09' AND
CONVERT(nvarchar,class_inyear) + '/09' <= '1998/09'
对吗?
chuxinfo 2008-01-03
  • 打赏
  • 举报
回复
在这儿只需要把程序传过来的参数假设为1998/09就行了,另外好象要使用到if 条件 then ***,
也可以SQL的结果只显示class_term,也就是
select (if *** then *** ) as class_term from Class_info where CONVERT(nvarchar,class_outyear) + '/07' >= '1998/09') AND CONVERT(nvarchar,class_inyear) + '/09' <= '1998/09'
一者仁心 2008-01-03
  • 打赏
  • 举报
回复

select *,
case 参数月份
when '03' then (参数年份-入学年份)*2+1
else (参数年份-入学年份)*2
end as class_term
from Class_info where
CONVERT(nvarchar,class_outyear) + '/07' >= '1998/09' AND
CONVERT(nvarchar,class_inyear) + '/09' <= '1998/09'

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧