注册 登录
编程论坛 SQL Server论坛

以文本类型保存的日期,在排序是如何按升序进行排序???

foshan 发布于 2007-03-22 16:04, 2883 次点击
我的数据库MYDATE字段是以文本型的形式保存日期(****年*月)的,在进行按升序进行排序时,10月、11月、12月排在1月的上面,而不是排在9月的下面,但我又不想更改MYDATE字段的数据类型为 datetime ,在这种情况下如何才能实现排序是按升序进行排序的???
SqlCommand cmdSel = new SqlCommand("select mydate,max(case Wjlx when 1 then wjlj else '' end) as N'A',max(case Wjlx when 2 then wjlj else '' end) as N'AA',max(case Wjlx when 3 then wjlj else '' end) as N'AAA',max(case Wjlx when 4 then wjlj else '' end) as N'AAAA' from baobiao group by mydate", conn1); //创建DataAdapter(数据适配器)

da1.SelectCommand = cmdSel;

只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录

14 回复
#2
棉花糖ONE2007-03-22 17:08
在进行按升序进行排序时,10月、11月、12月排在1月的上面,而不是排在9月的下面,
#3
Kendy1234562007-03-22 17:19
sql语句中 最后加上这个
order by case when len(mydate) = 7 then left(mydate,5)+'0'+ right(mydate,2) else mydate end
因为只用过英文版 不知道汉字是否算双字节。 如果汉字双字节
order by case when len(mydate) = 9 then left(mydate,6)+'0'+ right(mydate,3) else mydate end

#4
棉花糖ONE2007-03-22 17:23
汉字是按双字节的
#5
foshan2007-03-22 17:47
谢谢!但加了上述语句上去,还是10月、11月、12月排在1月的上面,是不是因为数据表的MYDATE字段的数据类型是 文本 而不是 日期 导致的???
SqlCommand cmdSel2 = new SqlCommand("select mydate,max(case Wjlx when 1 then wjlj else '' end) as N'A',max(case Wjlx when 2 then wjlj else '' end) as N'AA',max(case Wjlx when 3 then wjlj else '' end) as N'AAA',max(case Wjlx when 4 then wjlj else '' end) as N'AAAA' from baobiao where MYDATE LIKE '%2006%' group by mydate order by case when len(mydate) = 9 then left(mydate,6)+'0'+ right(mydate,3) else mydate end", conn1); //创建DataAdapter(数据适配器)

da1.SelectCommand = cmdSel2;

只有本站会员才能查看附件,请 登录

[此贴子已经被作者于2007-3-22 17:48:51编辑过]

#6
foshan2007-03-23 09:28
谢谢!经测试:
用 order by case when len(mydate) = 9 then left(mydate,6)+'0'+ right(mydate,3) else mydate end 不行!

用 order by case when len(mydate) = 7 then left(mydate,5)+'0'+ right(mydate,2) else mydate end 就解决这个问题了。

如此说来, 岂不是 汉字 不是 按双字节的,而是按单字节的 ???



#7
Kendy1234562007-03-23 11:02
看结果好像是这样。。。
#8
xiyou4192007-03-24 18:51
其实也可以在输入数据时将"2007年1月20"输入成"2007年01月20日"
这样就可以在"2007年10月20"之前排列了!
#9
xiyou4192007-03-24 19:03
对了,还有一点,按这样说,那在SQL中,汉字是单字节的,这个不 符合啊.
看下面这个例子:
declare @text char(50)
set @text='2007年1月'
set @text=left(@text,5) + '0' + right(@text,2)
print @text
输出结果就不对了,换成这样就对了:
declare @text varchar(50)
set @text='2007年1月'
set @text=left(@text,5) + '0' + right(@text,2)
print @text

这个是什么原因?
#10
初学Delphi2007-03-26 13:52

我这边汉字的时候单字节就对了
if exists(select * from sysobjects where name='table1')
drop table table1
create table table1(id int identity(1,1) primary key,
shijian varchar(50))
insert into table1 select '2007年3月' union all select '2007年11月' union all select '2007年2月'
select cast((case len(shijian) when 8 then REPLACE (REPLACE(shijian,'年',''),'月','02') else REPLACE (REPLACE(shijian,'年','0'),'月','01') end) as datetime) as 时间 from table1
换成10就不对了

#11
棉花糖ONE2007-03-26 15:04

汉字是双字节的 ,关键是len函数(还有left,right等)不是按字节来取的是按一个一个字来取的,我搞了很久才搞清楚,怎么回事

[此贴子已经被作者于2007-3-26 17:13:41编辑过]

#12
Kendy1234562007-03-26 15:32
righe是什么dd 抓住机会bs一下楼上
#13
foshan2007-03-26 17:26

还有一个问题请教:如果是以下形式的字段(按字段内容的 最后四位数 进行排序),应该如何进行升序排列啊?谢谢!

只有本站会员才能查看附件,请 登录

#14
棉花糖ONE2007-03-26 18:37
order by right(rtrim(column),4)

[此贴子已经被作者于2007-3-26 18:38:34编辑过]


#15
foshan2007-03-27 12:54
谢谢!可行!
1