您的位置:程序门 -> ms-sql server -> 基础类



01jan  02feb 03oct范围计算问题急!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


01jan 02feb 03oct范围计算问题急![已结贴,结贴人:czyoooo]
发表于:2007-06-22 11:36:08 楼主
数据
id     fdate
1       01jun
2       02feb
3       21jun
4       03jan
5       25nov
6       30dec
。。。。
要求查询出,01jan-0jun   的数据怎么做比较好!
发表于:2007-06-22 11:50:201楼 得分:90
create   table   mt(
id   int   ,   fdate   nvarchar(20))
insert   into   mt   select   1,       '01jun '
union   select   2,       '02feb '
union   select   3,       '21jun '
union   select   4,       '03jan '
union   select   5,       '25nov '
union   select   6,       '30dec '


declare   @s   nvarchar(20)
set   @s= '01jan '
declare   @e   nvarchar(20)
set   @e= '01jun '


select   id   ,fdate   from   mt
where   convert(datetime,   stuff(fdate,3,0, '   ')+ '   1900 ')   between  
convert(datetime,   stuff(@s,3,0, '   ')+ '   1900 ')   and   convert(datetime,   stuff(@e,3,0, '   ')+ '   1900 ')

id                     fdate                                
-----------   --------------------  
1                       01jun
2                       02feb
4                       03jan

(3   row(s)   affected)

发表于:2007-06-22 11:52:062楼 得分:0
.....日期字段这样表达.up
发表于:2007-06-22 11:55:493楼 得分:10
select   convert(datetime,   stuff( '21jun ',3,0, '   ')+ '   1900 ')  

从字符串转换为   datetime   时发生语法错误。

--为什么我这里出现语法错误的呢?

看了联机帮助
sql   server   可以识别以下列格式括在单引号   ( ')   中的日期和时间:  

字母日期格式(例如, 'april   15,   1998 ')
数字日期格式(例如, '4/15/1998 '、 'april   15,1998 ')
未分隔的字符串格式(例如 '19981207 '、 'december   12,   1998 ')
但是我用   'april   15,1998 '也会出错..
发表于:2007-06-22 12:29:184楼 得分:0
楼上的你的 'april   15,1998 '   逗点是中文的逗点


select   convert(datetime, 'april   15,1998 ')
发表于:2007-06-22 12:46:085楼 得分:0
select   convert(datetime, 'april   15,1998 ')   也错..
发表于:2007-06-22 13:02:036楼 得分:0
我机器上可以啊,可能是和机器环境有关吧,我装的是英文操作系统,英文数据库,系统的默认字体也是英文
------------------------------------------------------  
1998-04-15   00:00:00.000

(1   row(s)   affected)

发表于:2007-06-22 15:02:127楼 得分:0
我的是中文操作系统也有类似的错误情况,不知道怎么解决!
发表于:2007-06-25 11:04:218楼 得分:0
lwl0606(寒泉)   (   )   :
            自己后来解决了,你的方法有漏洞,就是碰到闰年的情况,两月分可能天数有变化,转换的时候就错误。
           
create   view   dbo.bp_bpdd_bpi
as
select   bpdd.precode,bpdd.productid,bpdd.productrelation1,bpdd.productrelation2,bpdd.exchangekind,(bpdd.rate*bpdd.qvalue)   as   qvalue,bpdd.tax,bpdd.saleprice,bpdd.profit,bpdd.remark,
bp.customername,bp.creattime,bpi.place1,bpi.place2,bpi.place3,bpi.place4,   bpi.place5,   bpi.carrier1
,bpi.carrier2,bpi.carrier3,bpi.carrier4,bpi.class1,bpi.class2,bpi.class3,bpi.class4,   bpi.flight1,   bpi.flight2,bpi.flight3,bpi.flight4,   bpi.date1,isnull(bpi.tourcode, ' ')   as   tourcode   ,bp.grup,bpdd.inprice,bpdd.outprice
  ,   case   substring(bpi.date1,3,3)
                when   'jan '     then     '190001 '+substring(date1,1,2)
                when   'feb '     then     '190002 '+substring(date1,1,2)
                when   'mar '     then     '190003 '+substring(date1,1,2)
                when   'apr '     then     '190004 '+substring(date1,1,2)
                when   'may '     then     '190005 '+substring(date1,1,2)
                when   'jun '     then     '190006 '+substring(date1,1,2)
                when   'jul '     then     '190007 '+substring(date1,1,2)
                when   'aug '     then     '190008 '+substring(date1,1,2)
                when   'sep '     then     '190009 '+substring(date1,1,2)
                when   'oct '     then     '190010 '+substring(date1,1,2)
                when   'nov '     then     '190011 '+substring(date1,1,2)
                when   'dec '     then     '190012 '+substring(date1,1,2)
  end   as   dateorder     from       billpayproductdtl   bpdd   inner   join   billpayproduct   bp   on  
    bp.billno=bpdd.billno     inner   join   billpayiticket   bpi
on   bpi.bookpid=bpdd.bookpid   and     len(bpi.date1)=5
union   all    
select   bpdd.precode,bpdd.productid,bpdd.productrelation1,bpdd.productrelation2,bpdd.exchangekind,(bpdd.rate*bpdd.qvalue)   as   qvalue,bpdd.tax,bpdd.saleprice,bpdd.profit,bpdd.remark,
bp.customername,bp.creattime,bpoi.place1,bpoi.place2,bpoi.place3,bpoi.place4,   bpoi.place5,   bpoi.carrier1
,bpoi.carrier2,bpoi.carrier3,bpoi.carrier4,bpoi.class1,bpoi.class2,bpoi.class3,bpoi.class4,   bpoi.flight1,   bpoi.flight2,bpoi.flight3,bpoi.flight4,   bpoi.date1,isnull(bpoi.tourcode, ' ')     as   tourcode   ,   bp.grup,bpdd.inprice,bpdd.outprice
,case   substring(bpoi.date1,3,3)
                when   'jan '     then     '190001 '+substring(date1,1,2)
                when   'feb '     then     '190002 '+substring(date1,1,2)
                when   'mar '     then     '190003 '+substring(date1,1,2)
                when   'apr '     then     '190004 '+substring(date1,1,2)
                when   'may '     then     '190005 '+substring(date1,1,2)
                when   'jun '     then     '190006 '+substring(date1,1,2)
                when   'jul '     then     '190007 '+substring(date1,1,2)
                when   'aug '     then     '190008 '+substring(date1,1,2)
                when   'sep '     then     '190009 '+substring(date1,1,2)
                when   'oct '     then     '190010 '+substring(date1,1,2)
                when   'nov '     then     '190011 '+substring(date1,1,2)
                when   'dec '     then     '190012 '+substring(date1,1,2)
  end   as   dateorder   from       billpayproductdtl   bpdd   inner   join   billpayproduct   bp   on  
    bp.billno=bpdd.billno     inner   join   billpayoutiticket   bpoi
on   bpoi.bookpid=bpdd.bookpid   and     len(bpoi.date1)=5
    这样就变成字符串比较了,比较笨不过办法可行!


快速检索

最新资讯
热门点击