函数将字符串拆分为小数?

编程入门 行业动态 更新时间:2024-10-12 16:21:45
本文介绍了函数将字符串拆分为小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在尝试编写一个 SQL Server 2005 函数,该函数获取一个字符串作为参数并从中创建一个包含十进制值的表.

I'm currently trying to write a SQL Server 2005 function, which gets a string as parameter and creates a table with decimal values out of it.

问题是,我必须根据参数定义十进制类型.这个不工作的片段应该证明这个想法:

The problem is, that I have to define the decimal type based on parameters. This not working snippet should demonstrate the idea:

CREATE FUNCTION [dbo].[ufn_ParseDecimal] ( @Sequence VARCHAR(max), @Delim CHAR(1), @Prec INT, @Scale INT ) RETURNS @DecimalList TABLE ( fValue decimal(@Prec, @Scale) )

任何想法,如何做到这一点?

Any ideas, how this could be done?

推荐答案

这是一个将任何文本字符串解析为值表的通用函数...您可以轻松地使用它来完成您想要完成的工作:

This is a generic function to parse any text string into a table of values... You can easily use it to do what you are trying to accomplish:

ALTER FUNCTION [dbo].[ParseTextString] (@S Text, @delim VarChar(5)) Returns @tOut Table (ValNum Integer Identity Primary Key, sVal VarChar(8000)) As Begin Declare @dLLen TinyInt -- Length of delimiter Declare @sWin VarChar(8000) -- Will Contain Window into text string Declare @wLen Integer -- Length of Window Declare @wLast TinyInt -- Boolean to indicate processing Last Window Declare @wPos Integer -- Start Position of Window within Text String Declare @sVal VarChar(8000) -- String Data to insert into output Table Declare @BtchSiz Integer -- Maximum Size of Window Set @BtchSiz = 7900 -- (Reset to smaller values to test routine) Declare @dPos Integer -- Position within Window of next Delimiter Declare @Strt Integer -- Start Position of each data value within Window -- ------------------------------------------------------------------------- If @delim is Null Set @delim = '|' If DataLength(@S) = 0 Or Substring(@S, 1, @BtchSiz) = @delim Return -- --------------------------- Select @dLLen = Len(@delim), @Strt = 1, @wPos = 1, @sWin = Substring(@S, 1, @BtchSiz) Select @wLen = Len(@sWin), @wLast = Case When Len(@sWin) = @BtchSiz Then 0 Else 1 End, @dPos = CharIndex(@delim, @sWin, @Strt) -- ------------------------------------ While @Strt <= @wLen Begin If @dPos = 0 -- No More delimiters in window Begin If @wLast = 1 Set @dPos = @wLen + 1 Else Begin Set @wPos = @wPos + @Strt - 1 Set @sWin = Substring(@S, @wPos, @BtchSiz) -- ---------------------------------------- Select @wLen = Len(@sWin), @Strt = 1, @wLast = Case When Len(@sWin) = @BtchSiz Then 0 Else 1 End, @dPos = CharIndex(@delim, @sWin, 1) If @dPos = 0 Set @dPos = @wLen + 1 End End -- ------------------------------- Set @sVal = LTrim(Substring(@sWin, @Strt, @dPos - @Strt)) Insert @tOut (sVal) Values (@sVal) -- ------------------------------- -- Move @Strt to char after last delimiter Set @Strt = @dPos + @dLLen Set @dPos = CharIndex(@delim, @sWin, @Strt) End Return End

更多推荐

函数将字符串拆分为小数?

本文发布于:2023-11-29 22:33:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   字符串   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!