SQL scalar value function: Lowercase amount to uppercase

Source: Internet
Author: User
Tags scalar

Our daily development Business system, as a statistical report, especially financial statements, showing the amount of Chinese is often encountered.

There are a number of ways to convert case, and the following is a solution to this problem from a database function.

Effect

Call: SELECT dbo. [FN_CONVERTRMB] (192.4)

The specific functions are as follows:

-- =============================================
--Invoke: SELECT dbo. [FN_CONVERTRMB] (192.4)
--Create date:2015-01-06
--Description: Return Chinese amount
-- =============================================
CREATE FUNCTION [dbo]. [FN_CONVERTRMB] (@num NUMERIC (14, 2))
RETURNS VARCHAR (100)
As
BEGIN
DECLARE @n_data VARCHAR (20),
@c_data VARCHAR (100),
@n_str VARCHAR (10),
@i INT
SET @n_data =right (SPACE) +cast (CAST (ABS (@num *100) as BIGINT) as VARCHAR (20)), 14)
SET @c_data = ' '
SET @i=1
While @i<=14
BEGIN
SET @n_str =substring (@n_data, @i, 1)
IF @n_str <> '
BEGIN
IF Not ((SUBSTRING (@n_data, @i, 2) = ' 00 ')
OR ((@n_str = ' 0 ')
and ((@i=4)
OR (@i=8)
OR (@i=12)
OR (@i=14))))
SET @[email protected]_data+substring (' 0 a three-Woolu qi Ba Jiu ', CAST (@n_str as INT) +1, 1)
IF not ((@n_str = ' 0 ')
and (@i<>4)
and (@i<>8)
and (@i<>12))
SET @[email protected]_data+substring (' Qian Bai hundred million thousand hundred thousand Gather round corner ', @i, 1)
IF SUBSTRING (@c_data, LEN (@c_data)-1, 2) = ' billions '
SET @c_data =substring (@c_data, 1, LEN (@c_data)-1)
END
SET @[email protected]+1
END
IF @num <0
SET @c_data = ' (negative number) ' [Email protected]_data
IF @num =0
SET @c_data = ' 0 Circle '
IF @n_str = ' 0 '
SET @[email protected]_data+ ' whole '
RETURN (@c_data)
END

If friends think that may be used later, welcome reprint, of course, can also recommend, thank you.

PS: This blog post is written using Windows Live Writer 2012, and the format effect may not be good.

SQL scalar value function: Lowercase amount to uppercase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.