当我在 mysql 中执行查询时,我试图打印如下输出。
Name Salary Sal_in_Words
Mohan 45000 Rupees Forty Five Thousand Only
Salary 列的值为 45000,在第三列中,第二列中的值通过 Query 转换为单词。
我找到了一些文章,其中在 Oracle 中我们可以使用以下查询获得上述结果:
select Salary, (' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.')) Sal_in_Words from employee
输出:
Name Salary Sal_in_Words
Suresh 45000 Rupees Forty Five Thousand Only
在 MySQL 中,我尝试了 LPAD、RPAD,但它们只是将字符串添加到结果中,而不是将单词转换为字符串。
我找到了一些教程,但所有这些都解释了“TO_CHAR(日期)”。
有什么办法吗?
最佳答案
MySQL中没有简单的函数,需要借助函数自己写一个函数才能实现这个结果。
检查以下..它对我有用.. Reference
DELIMITER $$
CREATE FUNCTION `number_to_string`(n INT) RETURNS varchar(100)
BEGIN
-- This function returns the string representation of a number.
-- It's just an example... I'll restrict it to hundreds, but
-- it can be extended easily.
-- The idea is:
-- For each digit you need a position,
-- For each position, you assign a string
declare ans varchar(100);
declare dig1, dig2, dig3 int; -- (one variable per digit)
set ans = '';
set dig3 = floor(n / 100);
set dig2 = floor(n / 10) - dig3*10;
set dig1 = n - (dig3*100 + dig2*10);
if dig3 > 0 then
case
when dig3=1 then set ans=concat(ans, 'one hundred');
when dig3=2 then set ans=concat(ans, 'two hundred');
when dig3=3 then set ans=concat(ans, 'three hundred');
when dig3=4 then set ans=concat(ans, 'four hundred');
when dig3=5 then set ans=concat(ans, 'five hundred');
when dig3=6 then set ans=concat(ans, 'six hundred');
when dig3=7 then set ans=concat(ans, 'seven hundred');
when dig3=8 then set ans=concat(ans, 'eight hundred');
when dig3=9 then set ans=concat(ans, 'nine hundred');
else set ans = ans;
end case;
end if;
if dig2 = 1 then
case
when (dig2*10 + dig1) = 10 then set ans=concat(ans,' ten');
when (dig2*10 + dig1) = 11 then set ans=concat(ans,' eleven');
when (dig2*10 + dig1) = 12 then set ans=concat(ans,' twelve');
when (dig2*10 + dig1) = 13 then set ans=concat(ans,' thirteen');
when (dig2*10 + dig1) = 14 then set ans=concat(ans,' fourteen');
when (dig2*10 + dig1) = 15 then set ans=concat(ans,' fifteen');
when (dig2*10 + dig1) = 16 then set ans=concat(ans,' sixteen');
when (dig2*10 + dig1) = 17 then set ans=concat(ans,' seventeen');
when (dig2*10 + dig1) = 18 then set ans=concat(ans,' eighteen');
when (dig2*10 + dig1) = 19 then set ans=concat(ans,' nineteen');
else set ans=ans;
end case;
else
if dig2 > 0 then
case
when dig2=2 then set ans=concat(ans, ' twenty');
when dig2=3 then set ans=concat(ans, ' thirty');
when dig2=4 then set ans=concat(ans, ' fourty');
when dig2=5 then set ans=concat(ans, ' fifty');
when dig2=6 then set ans=concat(ans, ' sixty');
when dig2=7 then set ans=concat(ans, ' seventy');
when dig2=8 then set ans=concat(ans, ' eighty');
when dig2=9 then set ans=concat(ans, ' ninety');
else set ans=ans;
end case;
end if;
if dig1 > 0 then
case
when dig1=1 then set ans=concat(ans, ' one');
when dig1=2 then set ans=concat(ans, ' two');
when dig1=3 then set ans=concat(ans, ' three');
when dig1=4 then set ans=concat(ans, ' four');
when dig1=5 then set ans=concat(ans, ' five');
when dig1=6 then set ans=concat(ans, ' six');
when dig1=7 then set ans=concat(ans, ' seven');
when dig1=8 then set ans=concat(ans, ' eight');
when dig1=9 then set ans=concat(ans, ' nine');
else set ans=ans;
end case;
end if;
end if;
return trim(ans);
END$$
DELIMITER ;
如果出现以下错误..
#1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
运行这个查询:
SET GLOBAL log_bin_trust_function_creators = 1;
然后在Mysql中创建一个函数:
调用函数就像下面的命令:
SELECT number_to_string( 666 );
您将获得如下输出:
number_to_string( 666 )
six hundred sixty six
希望这对其他人有帮助!
关于mysql - 在 MYSQL 结果中将数字转换为单词!使用查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16435879/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.