我的值为(500,850,65.5)为GivenUnitPrice,我想通过用PARSENAME
来分隔各个列来分隔这些值
我已经尝试过
PARSENAME(GivenUnitPrice, 3) as DB,
PARSENAME(GivenUnitPrice, 2) as Owner,
PARSENAME(GivenUnitPrice, 1) as Object
结果是
DB | Owner | Object
NULL | 500 , 850 , 65 | 5
答案 0 :(得分:0)
DECLARE @GivenUnitPrice VARCHAR(100)= '500 , 850 , 65.5'
SELECT PARSENAME(replacE(@GivenUnitPrice,',','.'),4) as DB,PARSENAME(replacE(@GivenUnitPrice,',','.'),3) as Owner,PARSENAME(replacE(@GivenUnitPrice,',','.'),2)+ '.'+PARSENAME(@GivenUnitPrice,1) AS OBJECT
答案 1 :(得分:0)
您可以尝试这个。...
DECLARE @UnitPrice VARCHAR(100)
SET @UnitPrice= '500 , 850 , 65.5'
SELECT PARSENAME(REPLACE(@UnitPrice,',','.'),4) as DB,
PARSENAME(REPLACE(@UnitPrice,',','.'),3) as Owner,
PARSENAME(REPLACE(@UnitPrice, ',', '.'), 2) AS OBJECT,
PARSENAME(REPLACE(@UnitPrice, '', '.'), 1) AS OBJECT
答案 2 :(得分:0)
PARSENAME基本上将。(Dot)视为字符串中的分号。在您的输入字符串中,末尾只有一个。(点),因此您只能在“所有者”和“对象”列中获取值。如果要对此字符串使用PARSENAME,请先用。(点)替换逗号,然后按如下所示应用PARSENAME-
DECLARE @ObjectName NVARCHAR(1000);
SET @ObjectName = '500 , 850 , 65.5';
SET @ObjectName = REPLACE(@ObjectName, ',', '.');
SELECT PARSENAME(@ObjectName, 4) AS Server,
PARSENAME(@ObjectName, 3) AS DB,
PARSENAME(@ObjectName, 2) AS Owner,
PARSENAME(@ObjectName, 1) AS Object;
答案 3 :(得分:0)
您似乎想用逗号分隔字符,但是Parsename()
函数用点字符分隔(例如,十进制数65.5也像分隔整数一样被分隔),因此对您的结果产生错误的结果案件。最好将replace()
,substring()
和charindex()
一起使用:
with t as
(
select replace(replace('500 , 850 , 65.5',')',''),'(','') as GivenUnitPrice
), t2 as
(
select substring(GivenUnitPrice,1,charindex(',',GivenUnitPrice)-1) as db,
substring(GivenUnitPrice,charindex(',',GivenUnitPrice)+1,len(GivenUnitPrice)) as owner_object
from t
)
select db,
substring(owner_object,1,charindex(',',owner_object)-1) as owner,
substring(owner_object,charindex(',',owner_object)+1,len(owner_object)) as object
from t2;
db owner object
500 850 65.5