将 Varchar 转换为 Ascii
我正在尝试将 VARCHAR 字段的内容转换为可以由第三方轻松引用的唯一数字。
如何将 varchar 转换为等效的 ascii 字符串?在 TSQL 中? ASCII() 函数转换单个字符,但如何转换整个字符串?
我尝试过使用
CAST(ISNULL(ASCII(Substring(RTRIM(LTRIM(PrimaryContactRegion)),1,1)),'')AS VARCHAR(3))
+ CAST(ISNULL(ASCII(Substring(RTRIM(LTRIM(PrimaryContactRegion)),2,1)),'')AS VARCHAR(3))
....但这很乏味,看起来很愚蠢,而且如果我有很长的字符串,它就不起作用。或者如果更好的话我该如何在 SSRS 中做同样的事情?
I'm trying to convert the contents of a VARCHAR field to be unique number that can be easily referenced by a 3rd party.
How can I convert a varchar to the ascii string equivalent? In TSQL? The ASCII() function converts a single character but what can I do to convert an entire string?
I've tried using
CAST(ISNULL(ASCII(Substring(RTRIM(LTRIM(PrimaryContactRegion)),1,1)),'')AS VARCHAR(3))
+ CAST(ISNULL(ASCII(Substring(RTRIM(LTRIM(PrimaryContactRegion)),2,1)),'')AS VARCHAR(3))
....but this is tedious, stupid looking, and just doesn't really work if I had long strings. Or if it is better how would I do the same thing in SSRS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情:
OUTPUT:
just to test it out:
OUTPUT:
另外,您可能想使用这个:
将所有 ASCII 值强制转换为 3 位数字,我不确定根据您的使用情况是否有必要。
每个字符使用 3 位数字输出:
try something like this:
OUTPUT:
just to test it out:
OUTPUT:
Also, you might want to use this:
to force all ASCII values into 3 digits, I'm not sure if this is necessary based on your usage or not.
Output using 3 digits per character:
好吧,我认为解决这个问题的速度会非常慢,但我想你可以这样做:
我没有使用带有数据库引擎的电脑,所以我无法真正测试这段代码。如果有效,那么您可以基于此创建 UDF。
Well, I think that a solution to this will be very slow, but i guess that you could do something like this:
I'm not in a pc with a database engine, so i can't really test this code. If it works, then you can create a UDF based on this.