SQL谜题(加减符号替代)

问题:将以下字符串”.1.2.3.4.5.6.7.8.9 = 1“中的符号点(.)更改为符号加(+)或符号(-),有多少种方法?请用SQL解决此问题
计算过程:

CREATE
TABLE #(VAL INT) INSERT INTO # SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 CREATE TABLE #1(VAL CHAR(1)) INSERT INTO #1 SELECT '+' UNION ALL SELECT '-' ; WITH A AS( SELECT #1.VAL VAL1,#.VAL FROM # JOIN #1 ON 1=1) SELECT RTRIM(A1.VAL1)+RTRIM(A1.VAL)+ RTRIM(A2.VAL1)+''+RTRIM(A2.VAL)+ RTRIM(A3.VAL1)+''+RTRIM(A3.VAL)+ RTRIM(A4.VAL1)+''+RTRIM(A4.VAL)+ RTRIM(A5.VAL1)+''+RTRIM(A5.VAL)+ RTRIM(A6.VAL1)+''+RTRIM(A6.VAL)+ RTRIM(A7.VAL1)+''+RTRIM(A7.VAL)+ RTRIM(A8.VAL1)+''+RTRIM(A8.VAL)+ RTRIM(A9.VAL1)+''+RTRIM(A9.VAL) Result FROM A A1,A A2,A A3,A A4,A A5,A A6,A A7,A A8,A A9 WHERE A1.VAL=1 AND A2.VAL=2 AND A3.VAL=3 AND A4.VAL=4 AND A5.VAL=5 AND A6.VAL=6 AND A7.VAL=7 AND A8.VAL=8 AND A9.VAL=9 AND CASE WHEN A1.VAL1='-' THEN -A1.VAL ELSE A1.VAL END+ CASE WHEN A2.VAL1='-' THEN -A2.VAL ELSE A2.VAL END+ CASE WHEN A3.VAL1='-' THEN -A3.VAL ELSE A3.VAL END+ CASE WHEN A4.VAL1='-' THEN -A4.VAL ELSE A4.VAL END+ CASE WHEN A5.VAL1='-' THEN -A5.VAL ELSE A5.VAL END+ CASE WHEN A6.VAL1='-' THEN -A6.VAL ELSE A6.VAL END+ CASE WHEN A7.VAL1='-' THEN -A7.VAL ELSE A7.VAL END+ CASE WHEN A8.VAL1='-' THEN -A8.VAL ELSE A8.VAL END+ CASE WHEN A9.VAL1='-' THEN -A9.VAL ELSE A9.VAL END=1

执行结果:

转载于:https://www.cnblogs.com/martintuan/p/4917018.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值