无法在本机查询中使用“:”作为分隔符
Posted
技术标签:
【中文标题】无法在本机查询中使用“:”作为分隔符【英文标题】:Unable to use ':' as delimeter in native query 【发布时间】:2020-11-25 07:38:01 【问题描述】:使用“:”作为分隔符将时间与日期分开。当我们在 sql 中运行查询时,它的工作原理。在 JPA 中,jpa 认为这个“:”分隔符作为命名参数,如何克服这个问题。
查询:
select (case when SPLIT_PART(
SPLIT_PART(inq.suggest_time::text, ' ', 2),
':', 1
)::int <= 10 and
SPLIT_PART(
SPLIT_PART(inq.suggest_time::text, ' ', 2),
':', 2
)::int < 30
then CONCAT(
(SPLIT_PART(
SPLIT_PART(inq.suggest_time::text, ' ', 1),
'-', 1
)::int)::text,
'-',
(SPLIT_PART(
SPLIT_PART(inq.suggest_time::text, ' ', 1),
'-', 2
)::int)::text,
'-',
(SPLIT_PART(
SPLIT_PART(inq.suggest_time::text, ' ', 1),
'-', 3
)::int + 1)::text)
else (SPLIT_PART(inq.suggest_time::text, ' ', 1))::text
end) as dateValue,
count(inq.suggest_time )
from inquiry_table inq
where inq.assigned_to= 63
and (inq.suggest_time is not null)
and inq.inquiry_status IN (select ld.lookup_detail_id
from lookup_detail ld
where ld.lookup_detail IN ('New','In Process'))
group by dateValue
ORDER BY dateValue;
应用程序出错:
Caused by: org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is org.hibernate.QueryException: Not all named parameters have been set: [:text, :int] [select (case when SPLIT_PART(SPLIT_PART(inq.suggest_time::text, ' ', 2), ':', 1)::int <= 10 and SPLIT_PART(SPLIT_PART(inq.suggest_time::text, ' ', 2), ':', 2)::int < 30 then CONCAT((SPLIT_PART(SPLIT_PART(inq.suggest_time::text, ' ', 1), '-', 1)::int)::text, '-',(SPLIT_PART(SPLIT_PART(inq.suggest_time::text, ' ', 1), '-', 2)::int)::text, '-',(SPLIT_PART(SPLIT_PART(inq.suggest_time::text, ' ', 1), '-', 3)::int + 1)::text) else (SPLIT_PART(inq.suggest_time::text, ' ', 1))::text end) as dateValue,count(inq.suggest_time ) from inquiry inq where inq.assigned_to= 63 and (inq.suggest_time is not null) and inq.inquiry_status IN (select ld.lookup_detail_id from lookup_detail ld where ld.lookup_detail IN ('New','In Process')) group by dateValue ORDER BY dateValue]
【问题讨论】:
很可能是演员::text
或 ::int
而不是分隔符。尝试将其更改为例如cast (inq.suggest_time as text)
让你的混淆层快乐
与您的问题无关,但 split_part()
怪物看起来很可怕。你到底想在那里做什么?我很确定有更好的方法来格式化time
值。
你试过用反斜杠转义冒号吗?
谢谢大家...我尝试用反斜杠冒号。它工作正常。是的查询似乎很乱。会尽量简化。
【参考方案1】:
转义 ::text 和 ::int 中的 :: 如下所示:
\\:\\:text
\\:\\:int
参考:https://***.com/a/29161044/6294804
【讨论】:
以上是关于无法在本机查询中使用“:”作为分隔符的主要内容,如果未能解决你的问题,请参考以下文章
使用 regexp_substr 在 Oracle 中按空格和字符拆分字符串作为分隔符