生小孩的sql问题,大家来啊.

money888888 2006-08-25 09:50:57
数据库中的数据是这样的
a
100
100.01
100.02
100.01.01
100.01.02
100.01.03
...
想要的显示结果是
a
100
100
100
100.01
100.01
100.01
...
也就是去掉最后一个.和他后边的数 通过一个函数生成一个这样的新视图最好 这个函数怎么写呢?下边是拿asp写的 不知道怎么改成sql的
if instr(str,".")<>0 then
dim strarr
strarr=split(str,".")
str=left(str,len(str)-len(strarr(ubound(strarr))))
else
str=str
end if


CSDN论坛浏览器:浏览、发帖、回复、结贴自动平均给分,下载地址:http://CoolSlob.ys168.com
...全文
808 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
张三大胖 2006-08-29
  • 打赏
  • 举报
回复
create function F_R(@Str varchar(100))
returns varchar(100)
as
begin
declare @r varchar(100)
select @r=left(@str,len(@str)-charindex('.',reverse(@str)))
return @r
end
go
yongyupost2000 2006-08-26
  • 打赏
  • 举报
回复
测试:
declare @t table( a varchar(100))
insert @t select '100'
union all select '100.01'
union all select '100.02'
union all select '100.01.01'
union all select '100.01.02'
union all select '100.01.03'

select reverse(stuff(reverse(a),1,charindex('.',reverse(a)),'')) from @t

结果:


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
100
100
100
100.01
100.01
100.01

(所影响的行数为 6 行)
kingtoo009 2006-08-26
  • 打赏
  • 举报
回复
帮顶..接分...
------------------------------------------------------------------------------------
100M.Net空间+50M企业邮局=60元/年
100M.Net空间+国际顶级域名=100元/年
国际顶级域名.com.net.cn=50元/年
本站申请域名可绑定免费10M Asp.Net空间
1000M.Net空间 + 100M MsSql数据库 + 1000M企业邮局 + 顶级域名=600元/年
数据库 企业邮局 网站推广 整机租用 美国空间 网站建设 均有售
还有很多优惠套餐提供给各个用户层.
有意者可联系电话:021-64802212 传真:021-64802212
咨询信箱:info@kingtoo.com 咨询OICQ:68311305,379620139 81778640
vfssqs 2006-08-25
  • 打赏
  • 举报
回复
create table bb(
col varchar(20)
)
insert bb values('100')
insert bb values('100.01')
insert bb values('100.01.01')
insert bb values('100.01.01.01')

select case when charindex('.',col,charindex('.',col)+1)=0
then col
else subString(col,0,charindex('.',col,charindex('.',col)+1))
end
from bb
xyxfly 2006-08-25
  • 打赏
  • 举报
回复
做个广告
http://community.csdn.net/Expert/topic/4974/4974603.xml?temp=.6206171
这个帖子放在那里没人回啊,追加100分,放在这里怕被删了 -_-
子陌红尘 2006-08-25
  • 打赏
  • 举报
回复
方法不行 把那些象100这样不带.的数据都删除了
----------------------------------------------------------------------------------
SQL语句中已经单独处理了,如下:

select a from 表 where charindex('.',a)=0
union all
......
specialsoldier 2006-08-25
  • 打赏
  • 举报
回复
恩 确实要用到4个基本函数
xyxfly 2006-08-25
  • 打赏
  • 举报
回复
create table test
(
number varchar(100)
)
insert test
select '100' union all
select '100.01' union all
select '100.02' union all
select '100.01.01' union all
select '100.01.02' union all
select '100.01.03'
select * from test
go
create function gaibian
(
@char char(1),
@str varchar(255)
)
returns varchar(255) as
begin
if charindex(@char,@str)>0
select @str=left(@str,len(@str)-charindex(@char,reverse(@str)))
return @str
end
go
create view tview as
select dbo.gaibian('.',number) as aaa from test
go
select * from tview
xyxfly 2006-08-25
  • 打赏
  • 举报
回复
create function gaibian
(
@char char(1),
@str varchar(255)
)
returns varchar(255) as
begin

if charindex(@char,@str)>0
select @str=left(@str,len(@str)-charindex(@char,reverse(@str)))


return @str
end
money888888 2006-08-25
  • 打赏
  • 举报
回复
:libin_ftsafe(子陌红尘:当libin告别ftsafe)() 信誉:105的方法不行 把那些象100这样不带.的数据都删除了
九斤半 2006-08-25
  • 打赏
  • 举报
回复
-- 建表
create table test(col varchar(10))
insert test
select 'a' union all
select '100' union all
select '100.01' union all
select '100.02' union all
select '100.01.01' union all
select '100.01.02' union all
select '100.01.03'
select * from test
go

-- 建函数
create function F_R(@Str varchar(100))
returns varchar(100)
as
begin
declare @r varchar(100)
select @r=left(@str,len(@str)-charindex('.',reverse(@str)))
return @r
end
go

select dbo.F_R(col) from test

drop function F_R
drop table test
子陌红尘 2006-08-25
  • 打赏
  • 举报
回复
select a from 表 where charindex('.',a)=0
union all
select reverse(stuff(reverse(a),1,charindex('.',reverse(a)),'')) from 表 where charindex('.',a)>0
money888888 2006-08-25
  • 打赏
  • 举报
回复
我想就是写一个自定义的转换函数 然后通过这个函数生成一个视图,这个我没接触过
九斤半 2006-08-25
  • 打赏
  • 举报
回复
create table test(col varchar(10))
insert test
select 'a' union all
select '100' union all
select '100.01' union all
select '100.02' union all
select '100.01.01' union all
select '100.01.02' union all
select '100.01.03'

select * from test
select left(col,len(col)-charindex('.',reverse(col))) from test

drop table test
money888888 2006-08-25
  • 打赏
  • 举报
回复
这个表里边的数据是不确定的 有很多个这样的
union all select '100'
union all select '100.01'
union all select '100.02'
union all select '100.01.01'
union all select '100.01.02'
也就是想把a这个字段 所有数据都转了 存到一个新视图里
fcuandy 2006-08-25
  • 打赏
  • 举报
回复
declare @t table(n varchar(100))
insert @t
select 'a'
union all select '100'
union all select '100.01'
union all select '100.02'
union all select '100.01.01'
union all select '100.01.02'
union all select '100.01.03'
select case when charindex('.',n)>0 then
reverse(stuff(reverse(n),1,charindex('.',reverse(n)),''))
else
n
end
from @t
xyxfly 2006-08-25
  • 打赏
  • 举报
回复
这个导出excel,搜一搜,代码一大把一大把的

比如搜asp.net导出excel
datagrid导出excel
money888888 2006-08-25
  • 打赏
  • 举报
回复
怎么在查出结果的页面里加段代码 加个按钮 让他可以导出excel
郭大侠_ 2006-08-25
  • 打赏
  • 举报
回复
老菜鸟都写得很好了
xyxfly 2006-08-25
  • 打赏
  • 举报
回复
为什么同样的数据 同样的表 同样的语句查出来的结果顺序不一样呢?


自己指定order by
加载更多回复(1)

34,593

社区成员

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

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