7

I am trying to use CASE in a SQL Select statement that will allow me to get results where I can utilize the length of one string to produce the resutls of another string. These are for non-matched records from two data sets that share a common ID, but variant Data Source.

Case statement is below:

Select Column1, Column2, 
Case
When Column1 = 'Something" and Len(Column2) = '35' Then Column1 = "Something Else" and substring(Column2, 1, 35)
End as Column3
From  dbo.xxx

When I run it I get the following error:

Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '='.

3
  • 3
    That SQL doesn't make sense. Can you give some example data and show what you expected to happen?
    – Mark Byers
    Commented May 1, 2012 at 23:32
  • This is the exact sql: Select Data_Source, CustomerID, CASE WHEN Data_Source = 'Test1' and Len(CustomerName) = '35' Then Data_Source = 'Test2' and substring(CustomerName, 1, 35) End AS CustomerName From dbo.xx Commented May 1, 2012 at 23:48
  • 1
    Data_Source CustomerID CustomerName Test xxx xxx PLC, (LONDON BR Test1 xxx xxx PLC(LONDON BR2) Commented May 1, 2012 at 23:51

2 Answers 2

10

You need to have a value for each WHEN, and ought to have an ELSE:

Select Data_Source, CustomerID,
  CASE
    WHEN Data_Source = 'Test1' and Len(CustomerName) = 35 THEN 'First Value'
    WHEN Data_Source = 'Test2' THEN substring(CustomerName, 1, 35)
    ELSE 'Sorry, no match.'
    END AS CustomerName
  From dbo.xx

FYI: Len() doesn't return a string.

EDIT: A SQL Server answer that addresses some of the comments might be:

declare @DataSource as Table ( Id Int Identity, CustomerName VarChar(64) )
declare @VariantDataSource as Table ( Id Int Identity, CostumerName VarChar(64) )
insert into @DataSource ( CustomerName ) values ( 'Alice B.' ), ( 'Bob C.' ), ( 'Charles D.' )
insert into @VariantDataSource ( CostumerName ) values ( 'Blush' ), ( 'Dye' ), ( 'Pancake Base' )

select *,
  -- Output the CostumerName padded or trimmed to the same length as CustomerName.  NULLs are not handled gracefully.
  Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) ) as Clustermere,
  -- Output the CostumerName padded or trimmed to the same length as CustomerName.  NULLs in CustomerName are explicitly handled.
  case
    when CustomerName is NULL then ''
    when Len( CustomerName ) > Len( CostumerName ) then Substring( CostumerName, 1, Len( CustomerName ) )
    else Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) )
    end as 'Crustymore'
  from @DataSource as DS inner join
    @VariantDataSource as VDS on VDS.Id = DS.Id
7
  • The ELSE part isn't mandatory in Transact-SQL CASE (if that was what you meant by ought to).
    – Andriy M
    Commented May 2, 2012 at 10:02
  • @AndriyM - It was a "best practices" suggestion.
    – HABO
    Commented May 2, 2012 at 11:26
  • I am not trying to use multiple WHEN in my statement, the intent is to manipulate data in records where the ID is similar but the Data Source (Column1) is variant so the length returned is the same. Of course adding ELSE is textbook, but not to be distracted from what I am trying to accomplish. Does anyone know why I would get that error when adding the '=' after my THEN? Or a solution that provides that allows me to use WHEN in this fashion. Any other suggestion is appreciated. Commented May 2, 2012 at 15:52
  • @user1368436 - As I tried to explain, the THEN provides a value to be returned as the value of the CASE expression. You cannot have an assignment in the THEN.
    – HABO
    Commented May 2, 2012 at 15:58
  • Ok so then what would you suggest? I need to manipulate data in records where the ID is similar, but the Data Source (Column1) is variant so the same length is returned? Commented May 2, 2012 at 16:02
2
Select 
    Column1, 
    Column2, 
    Case 
      When Column1 = 'Something' and Len(Column2) = 35 
      Then 'Something Else' + substring(Column2, 1, 35) 
    End as Column3 
From dbo.xxx

Update your query on

  1. use '+' for string concat
  2. len() returns int, no need to use ''
  3. remove "Column1 =" in the case when condition
  4. replace "" with ''

Hope this help.

2
  • Hi Seanbun, thanks for your suggestion. I am not looking to concatenate any data here. Length is required to manipulate the data so that return values are the same length. I am looking to understand why the '=' is causing an error after my THEN or a suggestion how I can use WHEN to facilitate records with the same ID, variant Data Source to match the return length of a specific field. Commented May 2, 2012 at 16:00
  • Would 'Else' help? like below or could you declare a variable @Result to store your value and return at the end? Select Column1, Column2, Case When Column1 = 'Something' and Len(Column2) = 35 Then 'Something Else' + substring(Column2, 1, 35) ELSE Column1 End as Column3 From dbo.xxx
    – seanbun
    Commented May 2, 2012 at 23:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.