如何在不使用oracle

时间:2018-07-17 12:03:11

标签: sql oracle oracle12c

我有一个transaction表,其中有一列trans_mode,用于存储交易的交易模式。交易模式包含以下值之一。

    trans_mode
    web
    app
    APP
    APP
    wEb

还有更多的可能性,因为trans_mode是varchar列。 我想写一个简单的select语句,如

select count(trans_mode) from tab where trans_mode='web';
or 
select count(trans_mode) from tab where trans_mode='Web';

现在,我不想使用任何条件,例如upper(trans_mode)='WEB'lower(trans_mode)='web'any regular expression,并且所需输出是忽略大小写的web个事务的计数。 / p>

我认为我们可以在定义trans_mode列时使用“列格式”或使用其他方法来实现相同的目的。但是不确定。请提出建议。

3 个答案:

答案 0 :(得分:4)

您不能使用“列格式”来执行此操作,但是可以更改当前会话的NLS行为:

ALTER SESSION SET nls_comp = Linguistic;
ALTER SESSION SET nls_sort = binary_CI;

select *
from tab
where trans_mode = 'app';

导致:

TRANS_MODE
----------
app       
APP       
APP       

在线示例:https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=a0196bf8031e5c612b9a9f6a2de870e4

如果您需要进行比较以遵守特定于语言的比较规则(例如ßSS在德语中应被视为相同),您也可以这样做:

insert into tab values ('große');
insert into tab values ('GROSSE');

ALTER SESSION SET nls_comp = Linguistic;
ALTER SESSION SET nls_sort = XGerman_CI;

select *
from tab
where trans_mode = 'große'

返回:

TRANS_MODE
----------
große     
GROSSE    

从Oracle 12开始。 2 ,您还可以使用collate选项:

select *
from tab
where trans_mode = 'app' collate binary_ci

答案 1 :(得分:0)

使用COLLATE:

 2018-07-17 19:16:55,490 WARN org.apache.hadoop.yarn.webapp.GenericExceptionHandler: INTERNAL_SERVER_ERROR
javax.ws.rs.WebApplicationException
    at com.sun.jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:886)
    at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:834)
    at org.apache.hadoop.yarn.server.resourcemanager.webapp.RMWebAppFilter.doFilter(RMWebAppFilter.java:84)
    at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:795)
    at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.apache.hadoop.http.lib.StaticUserWebFilter$StaticUserFilter.doFilter(StaticUserWebFilter.java:109)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.apache.hadoop.http.HttpServer2$QuotingInputFilter.doFilter(HttpServer2.java:1192)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.apache.hadoop.http.NoCacheFilter.doFilter(NoCacheFilter.java:45)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.apache.hadoop.http.NoCacheFilter.doFilter(NoCacheFilter.java:45)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

将返回web和wEb

答案 2 :(得分:0)

在Oracle 12.2中,您可以在列级别使用collate binary_ci

CREATE TABLE transactions (
  id          NUMBER,
  trans_mode     VARCHAR2(4 CHAR) COLLATE BINARY_CI,
  CONSTRAINT t1_pk PRIMARY KEY (id)
);

insert into transactions values(1,'web');
insert into transactions values(2,'app');
insert into transactions values(1,'Web');
insert into transactions values(1,'weB');

commit;

select count(0) from transactions where trans_mode = 'web' ;

参考链接: https://oracle-base.com/articles/12c/column-level-collation-and-case-insensitive-database-12cr2#column-level