1

I have a varchar column in a SQL Server 2005 table that looks like the following:

Mainly Sunny, 13.7°C
Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h

My goal is to parse out the temperature values, 13.7 and 12 respectively. Is there a series of string functions that can be used to locate and retrieve the first word in each string that contains °C?

4

2 回答 2

3
DECLARE @t TABLE(s VARCHAR(255));

INSERT @t SELECT 'Mainly Sunny, 13.7°C'
UNION ALL SELECT 'Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h';

SELECT RIGHT(LEFT(s, CHARINDEX('°', s)-1), 
  CHARINDEX(' ', REVERSE(LEFT(s, CHARINDEX('°', s)-1)))-1) FROM @t;

因此,作为计算列:

DECLARE @t TABLE
(
  s VARCHAR(255), 
  x AS CONVERT(VARCHAR(255),RIGHT(LEFT(s, CHARINDEX('°', s)-1), 
    CHARINDEX(' ', REVERSE(LEFT(s, CHARINDEX('°', s)-1)))-1)) PERSISTED
);

INSERT @t SELECT 'Mainly Sunny, 13.7°C'
UNION ALL SELECT 'Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h';

SELECT s,x FROM @t;

结果:

Mainly Sunny, 13.7°C                                  13.7
Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h     12

如果您可能有不包含°符号的字符串,那么:

DECLARE @t TABLE
(
  s VARCHAR(255), 
  x AS CONVERT(VARCHAR(255), CASE WHEN CHARINDEX('°', s) > 0 THEN 
    RIGHT(LEFT(s, CHARINDEX('°', s)-1), 
    CHARINDEX(' ', REVERSE(LEFT(s, CHARINDEX('°', s)-1)))-1) END) PERSISTED
);

INSERT @t SELECT 'Mainly Sunny, 13.7°C'
UNION ALL SELECT 'Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h'
UNION ALL SELECT 'No weather to report';

SELECT s,x FROM @t;

结果:

Mainly Sunny, 13.7°C                                  13.7
Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h     12
No weather to report                                  NULL

(如果你想要别的东西而不是NULL,我无法想象是什么,你可以ELSECASE表达式中添加一个。)

此外,为了证明我的解决方案是灵活的,而不引入性能严重的用户定义函数:

DECLARE @SearchString VARCHAR(8000);
SET @SearchString = 'km/h'; -- change this to '°'

DECLARE @t TABLE
(
  s VARCHAR(255)
);

INSERT @t SELECT 'Mainly Sunny, 13.7°C'
UNION ALL SELECT 'Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h'
UNION ALL SELECT 'No weather to report'
UNION ALL SELECT 'Wind 102km/h, 23.5°C, mostly cloudy';

SELECT s, x = CONVERT(VARCHAR(255), CASE WHEN CHARINDEX(@SearchString, s) > 0 THEN 
    RIGHT(LEFT(s, CHARINDEX(@SearchString, s)-1), 
    CHARINDEX(' ', REVERSE(LEFT(s, CHARINDEX(@SearchString, s)-1)))-1) END)
FROM @t;

结果:

Mainly Sunny, 13.7°C                                NULL
Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h   15
No weather to report                                NULL
Wind 102km/h, 23.5°C, mostly cloudy                 102
于 2013-09-04T15:45:24.860 回答
0

感谢@AdamWenger 和@AaronBertrand 提供的出色提示和出色答案。最后,我选择了一些更灵活的东西。我也希望能够找到包含除°C以外的字符串的单词。

我最终使用用户定义的函数来解析包含给定字符串的单词的第一次出现。它可能很乱,这是我自己编写的第一个真正的用户定义函数,但这就是我想出的。

CREATE function [dbo].[firstWordContaining] 
(@BigString varchar(max), @SearchString varchar(max))
returns varchar(max)
as
begin

-- Handle when search string is not in the big string
if (charindex(@SearchString,@BigString) = 0)
return null

-- Handle when search string is at the beginning
if (charindex(@SearchString,@BigString) = 1)
return substring(@BigString, 0, charindex(' ',@BigString))

-- Eliminate all words before the identified word
declare @Partial varchar(max)
select @Partial = substring(
@BigString,
charindex(@SearchString, @BigString) - charindex(' ',reverse(left(@BigString,charindex(@SearchString, @BigString)))) + 2, 
len(@BigString))

declare @Final varchar(max)
select @Final = case 

-- Handle when the search string is in the middle of the big string
when (charindex(' ',@Partial) > 0) then left(@Partial, charindex(' ',@Partial))

-- Handle when the search string is at the end of the big string
else @Partial
end

return @Final

end

所以,如果我这样做select dbo.firstWordContaining('Mainly Sunny, 13.7°C','°C'),我最终会得到13.7°C.

如果我这样做select dbo.firstWordContaining('Partly cloudy, 12°C, Humidity 69%, Wind NE 15km/h','°C'),我最终会得到12°C,.

剩下的标点符号很容易去掉。

于 2013-09-04T17:29:22.023 回答