problem description
to use a custom function in sqlserve to achieve uppercase output of Thai baht, you write a custom function, but you always make mistakes because you don"t understand the syntax rules of Thai baht.
the environmental background of the problems and what methods you have tried
tried to use the sql custom function to deal with the values one by one, and there was a problem.
found that js has a function to implement this function, which can not be modified simply because numerous groups of sql cannot be modified:
https://github.com/jojoee/bah.
related codes
/ / Please paste the code text below (do not replace the code with pictures)
/*
step:
1. validate
2. sanitize
3. split
4. proceed
5. grammar
6. combine
*/
const defaultResult = ""
const singleUnitStrs = [
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
const placeNameStrs = [
"",
"",
"",
"",
"",
"",
""
]
/**
* @param {number[]} nums
* @returns {string}
*/
function num2Word (nums) {
let result = ""
const len = nums.length
const maxLen = 7
if (len > maxLen) {
// more than million
const overflowIndex = len - maxLen + 1
const overflowNums = nums.slice(0, overflowIndex)
const remainingNumbs = nums.slice(overflowIndex)
return num2Word(overflowNums) + "" + num2Word(remainingNumbs)
} else {
for (let i = 0; i < len; iPP) {
const digit = nums[i]
if (digit > 0) {
result += singleUnitStrs[digit] + placeNameStrs[len - i - 1]
}
}
}
return result
}
/**
* @param {string} str
* @returns {string}
*/
function grammarFix (str) {
let result = str
// ""
result = result.replace("", "")
// ""
result = result.replace("", "")
// ""
const neungLen = 5
if (result.length > neungLen &&
result.length - result.lastIndexOf("") === neungLen) {
result = result.substr(0, result.length - neungLen) + ""
}
return result
}
/**
* Combine baht and satang
* and also adding unit
*
* @param {string} baht
* @param {string} satang
* @returns {string}
*/
function combine (baht, satang) {
let result = ""
if (baht === "" && satang === "") {
result = defaultResult
} else if (baht !== "" && satang === "") {
result = baht + "" + ""
} else if (baht === "" && satang !== "") {
result = satang + ""
} else {
result = baht + "" + satang + ""
}
return result
}
/**
* Change number to Thai pronunciation string
*
* @param {number} num
* @returns {string}
*/
function bahttext (num) {
let result = defaultResult
// 1. validate: invalid number
if (isNaN(num)) return result
// 1. validate: more than
if (num >= Number.MAX_SAFE_INTEGER) return result
// 2. sanitize: ????
// 3. split: baht and satang
// e.g. 432.21 >> 432, 21
// @todo optimize
/** @type {string} */
const bahtStr = Math.floor(num).toString()
/** @type {string} */
const satangStr = Math.round(num % 1 * 100).toString()
// 3. split: convert number array
// @todo optimize it
/** @type {number[]} */
const bahtArr = Array.from(bahtStr).map(Number)
/** @type {number[]} */
const satangArr = Array.from(satangStr).map(Number)
// 4. proceed
let baht = num2Word(bahtArr)
let satang = num2Word(satangArr)
// 5. grammar
baht = grammarFix(baht)
satang = grammarFix(satang)
// 6. combine
result = combine(baht, satang)
return result
}
if (typeof module !== "undefined" &&
module.exports != null) {
module.exports = bahttext
}
-js
what result do you expect? What is the error message actually seen?
which god can provide relevant ideas or reference materials?
can define a custom function in sql and output the uppercase amount in Thai, not necessarily referring to the js code above.
Thank you.