473,473 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Case Syntax

3 New Member
Hi fellow-developers,

I am literally hitting a wall trying to get the syntax right for this case statement.

Could someone please point out where I am going wrong?

Thanks in advance :)

DECLARE @cStr AS varchar(50)
DECLARE @nNum INT

SET @nNum = 0
SET @cStr = 'XX'
SET @nNum = (year(getdate()) - 2007)
print ' num P = ' + cast(@nNum as char)

SELECT CASE
WHEN (@nNum = 0) THEN SET @cStr = @cStr + 'CY'
WHEN (@nNum <> 0) THEN RAISERROR ('xxx', 16, 1)
WHEN (@nNum >= 10) THEN RAISERROR ('yyy.', 16, 1)
WHEN (@nNum < 10) THEN SET @cStr = @cStr + Replicate ('P', @nNum) + 'Y'
END
May 16 '07 #1
3 3315
Motoma
3,237 Recognized Expert Specialist
I think what you really want is this:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @cStr AS varchar(50)
  2. DECLARE @nNum INT
  3.  
  4. SET @nNum = 0
  5. SET @cStr = 'XX'
  6. SET @nNum = (year(getdate()) - 2007)
  7. print ' num P = ' + cast(@nNum as char)
  8.  
  9.  
  10. IF (@nNum <> 0) RAISERROR ('xxx', 16, 1)
  11. IF (@nNum >= 10) RAISERROR ('yyy.', 16, 1)
  12.  
  13. SELECT CASE
  14. WHEN (@nNum = 0) THEN @cStr + 'CY'
  15. WHEN (@nNum < 10) THEN @cStr + Replicate ('P', @nNum) + 'Y'
  16. END
  17.  
You see, you had a SELECT statement with a CASE statement inside. At runtime, this will be replaced with the actual case that it falls under. Your code would have generated the equivalent of
Expand|Select|Wrap|Line Numbers
  1. SELECT SET @cStr = @cStr + 'CY'
  2.  
Which is not a valid SELECT statement.
May 16 '07 #2
SQLNovice
3 New Member
Thank you very much for your response, The procedure works perfectly :) Thanks also for providing an explanation on why an error was being returned.

Regards,
-AM

I think what you really want is this:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @cStr AS varchar(50)
  2. DECLARE @nNum INT
  3.  
  4. SET @nNum = 0
  5. SET @cStr = 'XX'
  6. SET @nNum = (year(getdate()) - 2007)
  7. print ' num P = ' + cast(@nNum as char)
  8.  
  9.  
  10. IF (@nNum <> 0) RAISERROR ('xxx', 16, 1)
  11. IF (@nNum >= 10) RAISERROR ('yyy.', 16, 1)
  12.  
  13. SELECT CASE
  14. WHEN (@nNum = 0) THEN @cStr + 'CY'
  15. WHEN (@nNum < 10) THEN @cStr + Replicate ('P', @nNum) + 'Y'
  16. END
  17.  
You see, you had a SELECT statement with a CASE statement inside. At runtime, this will be replaced with the actual case that it falls under. Your code would have generated the equivalent of
Expand|Select|Wrap|Line Numbers
  1. SELECT SET @cStr = @cStr + 'CY'
  2.  
Which is not a valid SELECT statement.
May 16 '07 #3
Motoma
3,237 Recognized Expert Specialist
You are welcome. I am glad everything worked out for you.

Thank you very much for your response, The procedure works perfectly :) Thanks also for providing an explanation on why an error was being returned.

Regards,
-AM
May 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dave | last post by:
I am getting a syntax error on Select Case Now Case > "2/2/2004 12:30:00 AM" response.redirect "votingover.asp" case else m=0 End Select
9
by: Kevin | last post by:
Hi, I am getting a syntax error Microsoft VBScript compilation error '800a03ea' Syntax error On the code below. The error references the "End Select" line Can anyone help me with what I am...
3
by: Michael Hill | last post by:
Is this a valid sql statement: SELECT case field1 when 'first' then 1 when 'second' then 1 else null end FROM mytable
8
by: Penny | last post by:
Hi all, My browser throws this Select Case block back at me pointing out a syntax error on the line: 'Case < 251', between the word 'Case' and the '<' symbol. *************************** ...
2
by: Marco Lazzeri | last post by:
What about a SELECT syntax like that? SELECT CASE WHEN bar.foo_id IS NULL THEN bar.* ELSE foo.* FROM foo, bar Anyway, I need a SELECT query that gets data from a FIRST TABLE if a specific...
15
by: Benny Raymond | last post by:
I'm confused as to how fallthrough is limited in switch. For example the following works: string switch_test = "a"; switch (switch_test) { case "a": case "b": case "c": doSomething(a);
25
by: CJM | last post by:
I'm getting a syntax error with a Select Case statement: Select Case CSng(rs.fields("Field1")) Case 0 Response.Write "Test1" Case Is < 0 <<< Syntax Error...
2
enfuego
by: enfuego | last post by:
Hey kids, got a problem. Here's the code: Rev.techCompany CASE WHEN techCompany LIKE 'North_IR' THEN 'NorthIR' WHEN techCompany LIKE 'South_IR' THEN 'SouthIR' WHEN techCompany LIKE...
2
by: arunmenon | last post by:
Dear Folks, I have an query which something looks like this select ILTRDJ AS YEAR, DATEPART(MONTH,ILTRDJ) AS MONTH, FMSTR.IMITM AS CODE,FMSTR.IMSRP6_UDC AS DESCRIPTION, FTRANS.ILDCT = CASE...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.