SQL 查询语句的写法

来源:百度知道 编辑:UC知道 时间:2024/06/22 07:34:24
这个是远程服务器中的一张表,我想用一条查询语句提取上月NAME为12开头的数据,请问下语句怎么写?

name money YEAR MONTH
11000001 12540 2008 8
11200003 11587 2008 8
11200006 12588 2008 9
11300010 11154 2008 9
11300012 13587 2008 9

select * form 表
where [YEAR]=case month(getdate())
when 1 then year(getdate())-1
else year(getdate())
end
and [MONTH]=case month(getdate())
when 1 then 12
else month(getdate())-1
end
and name like '?12%'

select * from 表 where name like '12%'
and
(
( MONTH=month(getdate())-1 and YEAR=year(getdate()) and month(getdate())-1>0)
or
( MONTH=12 and YEAR=year(getdate())-1 and month(getdate())-1=0 )
)

其中
getdate()是获取当前时间:年月日小时分钞,
month(getdate())获取当前月
year(getdate())获取当前年
相信你能看懂

SELECT * FROM biao WHERE month < DATEADD(month, -1,GetDate()) and name
like '12%'

select * form 表 where name like '12%'
你试试