我有一个在Excel工作簿中运行的本机Oracle查询,并且我将用户提供的值从一个表传递到查询WHERE子句中。
我用M编写了一个快速函数,我认为它会在传入的字符串中添加单引号
(x) =>
let
string_format = "'" & x & "'"
in
string_format我将此函数应用于一个列,然后将该列转换为一个列表,但是任何带有嵌入逗号的字符串都用双引号括起来
text_tbl3 = Table.TransformColumns(text_tbl2,{{"Org_Names", string_format}})
text_ls = Table.ToList(text_tbl3)

很难看到,但是TD,AMERITRADE被这样的双引号和单引号所包围:"'TD,AMERITRADE'“。我希望它显示为'TD,AMERITRADE‘,因此它具有与其他单元格相同的格式,但我找不出是什么原因导致了额外的双引号。
发布于 2020-04-21 09:24:29
引用文本
M中的
快速函数,我认为它会将单引号添加到传入的字符串中
你的功能是正确的。& is the text concatenation operator。

因为您使用的是单个表达式,所以可以通过删除内部的let..in表达式来简化它。(如果不打开advanced editor,就看不到外部的let..in表达式)。
quote_text = (string as text) => "'" & string & "'"注意:您的屏幕截图有额外的引号
您的输入是:
CHASE
CITI
"TD, AMERITRADE"这就是为什么你最终会得到:
'CHASE'
'CITI'
'"TD, AMERITRADE"'您的单元格可能在"TD, AMERITRADE"上有引号,但在其他单元格上没有。
以单个字符串的形式获取逗号分隔的列表
Text.Combine(list, separator=", ")将创建一个类似CSV文件的字符串。

let
list_names = table3[Company],
// equivalent to: list_names = {"CHASE", "CITI", "TD, AMERITRADE"},
without_quotes = Text.Combine(list_names, ", "),
list_quoted = List.Transform(
list_names,
quote_text
),
with_quotes = Text.Combine(list_quoted, ", "),
results = [
list_names = list_names,
list_quoted = list_quoted,
string_without_quotes = without_quotes,
string_with_quotes = with_quotes,
without_equal_to = "string = ""CHASE, CITI, TD, AMERITRADE""",
with_equal_to = "string = ""'CHASE', 'CITI', 'TD, AMERITRADE'"""
]
in
results如何在原生查询中使用该字符串?
我的查询使用SQL,但方法与Oracle相同。
raw_sql_query是您的原始查询。它使用参数@Company
sql_parameters是一种Record类型,它声明您正在使用的所有参数。在这里,我们将字符串与qoute_text函数一起使用。
Value.NativeQuery将为您插入参数。
let
company = "TD, AMERITRADE",
raw_sql_query = "
select * from Table
where Company = @Company
",
sql_parameters = [
Company = quote_text( company )
],
source = Sql.Database(
"localhost",
"Adventure Works"
),
results = Value.NativeQuery(
source,
raw_sql_query,
sql_parameters
)
in
results我们如何测试字符串函数是否正确引用?
首先创建一个新的空白查询。我们调用quote_text()来验证输出。
我使用了一个名为results的Record,这样您就可以在单个屏幕上标记和查看每个值。
manual_quote使用字符串连接运算符将字符串引起来
quote_string( sample_string )将变量插入到文本模板中。两者都返回完全相同的字符串。
模板变得越复杂,Text.Format就变得越简洁。这个函数非常简单,它不是必需的。
您的原始函数
高级编辑器中的函数如下所示:
let
quote_text = (x) =>
let
string_format = "'" & x & "'"
in
string_format
in
quote_text你可以去掉内边的字母
let
quote_text_simple = (string as text) =>
"'" & string & "'"
in
quote_text_simple如何使用可选参数和字符串模板
let
// a custom function to Surround a string with single quotes.
// A optional second argument lets you specify a different character
quote_string = (input_string as text, optional character as text) =>
let
character = if character = null then "'" else character,
template = "#[quote]#[string]#[quote]",
quoted_string = Text.Format(
template,
[
quote = character,
string = input_string
]
)
in
quoted_string
in
quote_stringhttps://stackoverflow.com/questions/61332267
复制相似问题