---------------------------------------------------------------- -- Author :SQL77(只为思齐老) -- Date :2010-01-19 20:13:31 -- Version: -- Microsoft SQL Server 2000 - 8.00.194 (Intel X86) -- Aug 6 2000 00:57:48 -- Copyright (c) 1988-2000 Microsoft Corporation -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3) -- ---------------------------------------------------------------- --> 测试数据:#A if object_id('tempdb.dbo.#A') is not null drop table #A go create table #A([B] varchar(7)) insert #A select '091228' union all select '0912281' union all select '0912288' --------------开始查询--------------------------select '091228'+ MAX(CASE WHEN RIGHT(B,LEN(B)-6) ='' THEN '' ELSE LTRIM(CONVERT(INT,RIGHT(B,LEN(B)-6))+1) END) from #A ----------------结果---------------------------- /* (所影响的行数为 3 行) ------------------ 0912289(所影响的行数为 1 行) */
select '091228'+ MAX(CASE WHEN RIGHT(B,LEN(B)-6) ='' THEN '' ELSE LTRIM(CONVERT(INT,RIGHT(B,LEN(B)-6))+1) END) from #A where b like '091228%' 我现在,当没有记录时..结果成了NULL,而不是091228.请问这要怎么解决?
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
-- Author :SQL77(只为思齐老)
-- Date :2010-01-19 20:13:31
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#A
if object_id('tempdb.dbo.#A') is not null drop table #A
go
create table #A([B] varchar(7))
insert #A
select '091228' union all
select '0912281' union all
select '0912288'
--------------开始查询--------------------------select
'091228'+
MAX(CASE WHEN RIGHT(B,LEN(B)-6) ='' THEN ''
ELSE LTRIM(CONVERT(INT,RIGHT(B,LEN(B)-6))+1) END) from #A
----------------结果----------------------------
/* (所影响的行数为 3 行)
------------------
0912289(所影响的行数为 1 行)
*/
'091228'+
MAX(CASE WHEN RIGHT(B,LEN(B)-6) ='' THEN ''
ELSE LTRIM(CONVERT(INT,RIGHT(B,LEN(B)-6))+1) END) from #A where b like '091228%'
我现在,当没有记录时..结果成了NULL,而不是091228.请问这要怎么解决?