>>>>>>>>>>数据库中字段需要更改,求合适的解决方案<<<<<<<<<<

her886 2010-08-10 09:06:51
现在数据库中字段是这样的

Price varchar(150)

Price字段表示价格,数据库中有好多记录 大体如下所示

ID Price
1 100元/件
2 80元/卷
3 电议
4 优惠
5
6 面议
7 咨询

因为数据类型定义为varchar,且允许为空,数据输入比较杂乱,无法计算出商品的价格,现在想在原来的基础上对价格字段
进行更改,加一个价格但位列Unit,且要保证有价格的信息不能丢失,处理后的效果如下

Price float
Unit varchar(20)

ID Price Unit
1 100 件
2 80 卷
3
4
5
6
7

如上,没有价格的或纯文字型的 让它为空 有价格的 讲价格和单位拆分开 存入两列
目的是为了能够对商品的价格进行计算 没输入价格的 在读取时提示需要咨询价格

小弟现在无处下手,因为数据库中有上万条记录,不可能手动一一更改,求高手帮忙,无论SQL语句或存储过程 只要能解决有价格的实现计算功能就行

...全文
148 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
her886 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 wxf163 的回复:]
SQL code
UPDATE tB
SET Price =substring (Price,0,case when ISNUMERIC(Price)=0 then patindex('%[^0-9]%',Price) else Price end), unit=
case when Price like '%/%' then substring( Price,CHARINDEX('/'……
[/Quote]
谢谢啊 !
王向飞 2010-08-10
  • 打赏
  • 举报
回复
UPDATE tB
SET Price =substring (Price,0,case when ISNUMERIC(Price)=0 then patindex('%[^0-9]%',Price) else Price end), unit=
case when Price like '%/%' then substring( Price,CHARINDEX('/',Price,0)+1,LEN(Price)-CHARINDEX('/',Price,0))
else NULL end
from tB

SELECT * FROM tB

再改改
her886 2010-08-10
  • 打赏
  • 举报
回复
问题基本解决了 谢谢大家 马上结贴
her886 2010-08-10
  • 打赏
  • 举报
回复
纯数字的 好像没有抽取出来
her886 2010-08-10
  • 打赏
  • 举报
回复
10#的方法


select ProductID Price,case when patindex('%[0-9]%',Price)>=0
then subString(price,patindex('%[0-9 .]%',Price)-1,patindex('%[^0-9 .]%',Price))
else null end price1
from dbo.Product where ProductID=58164

select ProductID,Price from dbo.Product where ProductID=58164


Price price1
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------
58164

(1 行受影响)

ProductID Price
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------
58164 3950.00

(1 行受影响)
王向飞 2010-08-10
  • 打赏
  • 举报
回复
--> 测试数据:@t

drop table tB
CREATE TABLE tB ([ID] int,[Price] varchar(8),unit varchar(8))
insert tB
select 1,'100元/件',NULL union all
select 2,'80元/卷',NULL union all
select 3,'电议' ,NULL union all
select 4,'优惠',NULL union all
select 5,null,NULL union all
select 6,'面议',NULL union all
select 7,'咨询',NULL

UPDATE tB
SET Price =substring (Price,0,patindex('%[^0-9]%',Price)), unit=
case when Price like '%/%' then substring( Price,CHARINDEX('/',Price,0)+1,LEN(Price)-CHARINDEX('/',Price,0))
else NULL end
from tB

SELECT * FROM tB

------------------------------------
ID Price unit
1 100 件
2 80 卷
3 NULL
4 NULL
5 NULL NULL
6 NULL
7 NULL



更新的
her886 2010-08-10
  • 打赏
  • 举报
回复
谢谢 大家 10楼的方法能够把数字抽取出来了 能不能让抽取出来的数字 添加的新的价格字段中呢
jaydom 2010-08-10
  • 打赏
  • 举报
回复

if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id int, price varchar(20))
insert into tb
select 1, '100元/件' union all
select 2, '80元/卷' union all
select 3, '电议' union all
select 4, '优惠' union all
select 5, null union all
select 6, '面议' union all
select 7, '咨询'

select id,Price= case when PATINDEX('%[^0-9]%',price)-1 > 0 then LEFT(price,CHARINDEX('/',price)-2)
else '' end,
Unit= case when PATINDEX('%[^0-9]%',price)-1 > 0 then right(price,len(price)-CHARINDEX('/',price))
else '' end
from tb

id Price Unit
1 100 件
2 80 卷
3
4
5
6
7
飘零一叶 2010-08-10
  • 打赏
  • 举报
回复

IF OBJECT_ID('[TB]') IS NOT NULL
DROP TABLE [TB]
CREATE TABLE TB
(
ID INT,PRICE NVARCHAR(150)
)

INSERT INTO TB
SELECT 1,'100元/件' UNION
SELECT 2,'80元/卷' UNION
SELECT 3,'电议' UNION
SELECT 4,'优惠' UNION
SELECT 5,'' UNION
SELECT 6,'面议' UNION
SELECT 7,'咨询'

IF OBJECT_ID('[TB_T]') IS NOT NULL
DROP TABLE [TB_T]
CREATE TABLE [TB_T]
(
ID INT ,
PRICE FLOAT,
UNIT VARCHAR(80)
)

INSERT INTO TB_T
SELECT ID
,SUBSTRING(PRICE,0,(case when CHARINDEX('/',PRICE)-1<=0 then 0 else CHARINDEX('/',PRICE)-1 end)) as price
,SUBSTRING(PRICE,case when CHARINDEX('/',PRICE)=0 then LEN(PRICE)+1 else CHARINDEX('/',PRICE)+1 end ,LEN(PRICE)) as unit
FROM TB

SELECT * FROM TB_T

ID PRICE UNIT
1 100 件
2 80 卷
3 0
4 0
5 0
6 0
7 0
希望对楼主有所帮助。
情殇无限 2010-08-10
  • 打赏
  • 举报
回复
if object_id('tb') is not null
drop table tb
go
create table tb(id int identity primary key,price varchar(10))

insert into tb select '100.5元/件' union all
select '80元/卷' union all
select '电议' union all
select '优惠' union all
select '咨询' union all
select '面议' union all
select ''

select id,price,case when patindex('%[0-9]%',Price)>0
then subString(price,patindex('%[0-9 .]%',Price)-1,patindex('%[^0-9 .]%',Price))
else null end price1
from tb
王向飞 2010-08-10
  • 打赏
  • 举报
回复
试试10楼
her886 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wxf163 的回复:]
SQL code
--> 测试数据:@t
declare @t table([ID] int,[Price] varchar(8))
insert @t
select 1,'100元/件' union all
select 2,'80元/卷' union all
select 3,'电议' union all
select 4,'优惠' union all
select 5,nul……
[/Quote]
谢谢回答 我主要的目的是为了 能够把像“100元/件”中的钱数读取出来,存到新增的字段中 以便计算 数据库中也有输入规范的 纯数字型如"100” "80.5" 只要能把有数字的读出来 便于计算就好 价格单位有没有都不是很重要
王向飞 2010-08-10
  • 打赏
  • 举报
回复
o(︶︿︶)o 唉

[Quote=引用 9 楼 her886 的回复:]
引用 7 楼 shutao917 的回复:
SQL code
if object_id('tb') is not null
drop table tb
go
create table tb(id int identity primary key,price varchar(10))

insert into tb select '100元/件' union all
select '……
[/Quote]
昵称被占用了 2010-08-10
  • 打赏
  • 举报
回复
取数字的函数好多人写过的,==就会有人贴的了
王向飞 2010-08-10
  • 打赏
  • 举报
回复
--> 测试数据:@t
declare @t table([ID] int,[Price] varchar(8))
insert @t
select 1,'100元/件' union all
select 2,'80元/卷' union all
select 3,'电议' union all
select 4,'优惠' union all
select 5,null union all
select 6,'面议' union all
select 7,'咨询'


select ID,substring (Price,0,patindex('%[^0-9]%',Price)), unit=
case when Price like '%/%' then substring( Price,CHARINDEX('/',Price,0)+1,LEN(Price)-CHARINDEX('/',Price,0))
else NULL end
from @t



这样就好了
her886 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shutao917 的回复:]
SQL code
if object_id('tb') is not null
drop table tb
go
create table tb(id int identity primary key,price varchar(10))

insert into tb select '100元/件' union all
select '80元/卷' union all
……
[/Quote]
有的价格输入 后边没有“/” 这点忘说了,不好意思啊 太急了
没有"/"的是不是不能拆分读取了啊?
王向飞 2010-08-10
  • 打赏
  • 举报
回复
--> 测试数据:@t
declare @t table([ID] int,[Price] varchar(8))
insert @t
select 1,'100元/件' union all
select 2,'80元/卷' union all
select 3,'电议' union all
select 4,'优惠' union all
select 5,null union all
select 6,'面议' union all
select 7,'咨询'


select ID,Price, unit=
case when Price like '%/%' then substring( Price,CHARINDEX('/',Price,0)+1,LEN(Price)-CHARINDEX('/',Price,0))
else NULL end
from @t







====================

ID Price unit
1 100元/件 件
2 80元/卷 卷
3 电议 NULL
4 优惠 NULL
5 NULL NULL
6 面议 NULL
7 咨询 NULL

情殇无限 2010-08-10
  • 打赏
  • 举报
回复
if object_id('tb') is not null
drop table tb
go
create table tb(id int identity primary key,price varchar(10))

insert into tb select '100元/件' union all
select '80元/卷' union all
select '电议' union all
select '优惠' union all
select '咨询' union all
select '面议' union all
select ''
go

alter table tb add unit varchar(10);
go
update tb
set unit=case when ISnumeric(left(price,1))=1
then subString(price,charindex('/' ,price)+1,len(price)) else '' end,
price=case when ISnumeric(left(price,1))=1
then subString(price,1,charindex('/' ,price)-2) end

alter table tb alter column price decimal(9,2)
select * from tb
her886 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 haiwer 的回复:]
数字可以写个函数抽取,单位则太多变化,需要查数据分别写
[/Quote]
能不能给个抽取数字的例子呢 只要能把有数字的抽取出来就好
her886 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 haiwer 的回复:]
语句不会词义分析,需要你自己写语句实现
建议加两个字段,价格和单位,原来的字段不要改变,这样问题简单化
[/Quote]
因为价格都是商家自己输入的 记录很多 新加两个字段后 如何将原来价格中的内容拆分开存入新加的两个字段中呢 有没有能够实现的语句啊
加载更多回复(3)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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