在sql中优化查询

时间:2015-08-21 12:00:03

标签: sql-server

有人可以建议改进这个功能吗?我只想在字符串(100以下)输入时以数字显示数字?

我只是一个初学者,所以非常感谢帮助, 提前致谢

CREATE FUNCTION dbf_strinf_to_num (@word as varchar(50))
    RETURNS varchar(30)

AS
    BEGIN            
    DECLARE @number varchar(30)
    DECLARE @cropword varchar (20) 
    SELECT @cropword = LEFT(@word,5)
    DECLARE @namecount numeric(18,6) 
    SELECT @namecount =  LEN(@word+ ';') - LEN(REPLACE(@word,' ','')) 
        IF @namecount IN(1,2)
            BEGIN
            IF @namecount = 1
              BEGIN
                SELECT @number =                                                 
                    (
                      SELECT CASE 
                        WHEN @cropword LIKE '%ONE%'
                            THEN 1
                        WHEN @cropword LIKE '%TWO'
                            THEN 2
                        WHEN @cropword LIKE '%REE'
                            THEN 3
                        WHEN @cropword LIKE '%OUR'
                            THEN 4
                        WHEN @cropword LIKE '%IVE'
                            THEN 5
                        WHEN @cropword LIKE '%SIX'
                            THEN 6
                        WHEN @cropword LIKE '%VEN'
                            THEN 7
                        WHEN @cropword LIKE '%GHT'
                            THEN 8
                        WHEN @cropword LIKE '%INE'
                            THEN 9
                        WHEN @word like '%E%N'
                            THEN 11
                        WHEN @word like '%T%E'
                            THEN 12
                        WHEN @word like '%T%N'
                            THEN 13
                        WHEN @word like '%FO%N'
                            THEN 14
                        WHEN @word like '%F%N'
                            THEN 15
                        WHEN @word like '%S%N'
                            THEN 16
                        WHEN @word like '%SE%N'
                            THEN 17
                        WHEN @word like '%EI%N'
                            THEN 18
                        WHEN @word like '%N%N'
                            THEN 19
                        ELSE 0 END 
                    )
               END
            ELSE
                IF @namecount = 2
                 BEGIN
                   --SELECT @number = dbf_strinf_to_num1(@word) 
                   SELECT @cropword = LEFT(@word,5)
                   SELECT @number =                                                 
                    (
                      SELECT CASE 
                        WHEN @cropword LIKE '%ONE%'
                            THEN 1
                        WHEN @cropword LIKE 'TWE%'
                            THEN 2
                        WHEN @cropword LIKE 'THIR%'
                            THEN 3
                        WHEN @cropword LIKE 'FOUR%'
                            THEN 4
                        WHEN @cropword LIKE 'FIF%'
                            THEN 5
                        WHEN @cropword LIKE 'SIX%'
                            THEN 6
                        WHEN @cropword LIKE 'SEVEN%'
                            THEN 7
                        WHEN @cropword LIKE 'EIGHT%'
                            THEN 8
                        WHEN @cropword LIKE 'NIN%'
                            THEN 9

                        ELSE 0 END 
                    )
                    select @number = @number + dbo.dbf_strinf_to_num(RIGHT(@word,3))
                 END
              END
        ELSE
            BEGIN
               SELECT @number = 'Error! PLease enter valid number(1-100)'
            END
    RETURN (@number)

END

2 个答案:

答案 0 :(得分:2)

最简单的方法是创建一个包含两列的表。 第一个包含单词中的数字,第二列包含数量相等的数字。

Create table NumberToWord
(
    NumberText varchar(50),
    NumberInt int
)

填充表后,您可以进行简单的连接以获得正确的数字转换。

SQL Central的数字大于100的另一个解决方案:http://www.sqlservercentral.com/Forums/Topic794134-149-1.aspx

希望它有所帮助。

答案 1 :(得分:0)

Create table NumberToWord
(
    NumberText varchar(50),
    NumberInt int identity(1,1)
)

我只是添加了身份并且效果更好