2015-04-19 54 views
1

我有一個SQL數據庫,它有一個字符串列。裏面的數據是數字,如..用貨幣對SQL字符串列排序

$12,000,394.09 
$56,874.94 
$110,339,384.11 

這是字符串列,但我想它與最低的頂部進行排序。是否有可能做到這一點?現在我只試過簡單的..

SELECT * FROM sales ORDER BY saleamount DESC 

任何幫助將是偉大的。由於

@布賴恩 - 錯誤

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended  
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450) 
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399) 
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059) 
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522) 
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257) 
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587) 
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:210) 
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:30) 
    at oracle.jdbc.driver.T4CStatement.executeForDescribe(T4CStatement.java:762) 
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925) 
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111) 
    at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:1309) 
    at oracle.jdbc.driver.OracleStatementWrapper.executeQuery(OracleStatementWrapper.java:422) 
    at TestServ.processRequest(TestServ.java:37) 
    at TestServ.doGet(TestServ.java:116) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) 
    at java.lang.Thread.run(Thread.java:745) 
+0

你用什麼Db引擎? –

+1

爲什麼不將列類型更改爲數字,並且不存儲貨幣或將其存儲在不同的列中 –

+0

我正在使用Oracle SQL Developer。我正在使用Java來檢索內容。 – user2703946

回答

3

你可以用regexp_replace做到這一點只保留數字字符和小數點,並to_number的結果轉換爲數字:

select * 
from sales 
order by to_number(regexp_replace(string_num_field, '[^0-9.]+', '')) 

小提琴:http://sqlfiddle.com/#!4/7d46e/2/0

+0

在sql小提琴看起來不錯,但它試圖運行它時會拋出大量錯誤文本。 – user2703946

+0

「錯誤的大文本」是什麼意思? –

+0

我將錯誤添加到第一篇文章。 – user2703946

1

如果所有的字符串都相同墊 - 從一個貨幣符號,逗號分隔符(如適用)和兩位小數 - 那麼你可以使用以下命令:

order by length(saleamount) desc, 
     saleamount desc 

如果你想最小的值,然後再:

order by length(saleamount) asc, 
     saleamount asc 
0

這裏是沒有正則表達式的一個老式的解決方案:

select amt_usd 
     , to_number(translate(amt_usd, '1234567890.,$', '1234567890.')) amt 
from t23 
order by 2 
/ 

工作SQL Fiddle

當然,如果用戶在列中輸入了£或其他內容,就會中斷。從這個角度來看,正則表達式更安全。