假设有如下一张名为sell的表
s_id  s_time                       s_pay
1     2008-09-17 10:06:20.123      60
1     2008-10-17 11:23:20.123      30
2     2008-12-15 11:24:56.123      60
3     2008-12-17 11:25:23.124      90我想查处2008年12月的相关信息利用通配符要怎么写?
select * from sell where s_time like  后面就不会写了,试了很多,都不对.
还有一个问题
也是上面的sell表,我见表时,是这样写的
create table sell
(
    s_id int permary key identity,
    s_time datetime default getdate(),
    s_pay float
)
go我想让s_time这个字段里的数据,添加进去的时候只显示年月日就可以了,具体的时间是不显示.
在建表时,s_time字段应该怎么写?

解决方案 »

  1.   

    对于日期字段的匹配,建议用Datediff()函数。
      

  2.   

    select * from sell where convert(varchar,s_time like,120) like '2008-12-%'
    把比较的日期变成字符就行了
      

  3.   

    DATEDIFF函数计算两个日期之间的小时、天、周、月、年等时间间隔总数。
    Datediff()函数不是这个功能么?
    我的就想实现当我添加一天记录的时候,自动添加当前的年月日即可~1楼的哥们能说的清楚点么..我有点不太理解,谢谢
      

  4.   


    if object_id('[sell]') is not null drop table [sell]
    go
    create table [sell]([s_id] int,[s_time] datetime,[s_pay] int)
    insert [sell]
    select 1,'2008-09-17 10:06:20.123',60 union all
    select 1,'2008-10-17 11:23:20.123',30 union all
    select 2,'2008-12-15 11:24:56.123',60 union all
    select 3,'2008-12-17 11:25:23.124',90select * 
    from [sell] 
    where datediff(m,'2008-12-01',s_time)=0 --只比较月份--结果:
    s_id        s_time                                                 s_pay       
    ----------- ------------------------------------------------------ ----------- 
    2           2008-12-15 11:24:56.123                                60
    3           2008-12-17 11:25:23.123                                90(所影响的行数为 2 行)
      

  5.   

    如果只保存年月日,可以把s_time定义为字符型,不过这样做没什么意义
    s_time varchar(10) default convert(varchar(10),getdate(),120)