| Author |
Topic  |
|
|
Ethan135
Starting Member
3 Posts |
Posted - 01/10/2013 : 08:02:06
|
Hi everyone. I'm new here and also new to SQL. I'm trying to set up a situation where I take off the last two characters of all the values in my 'objectname' field. But if the objectname field is 'report123' or 'report321' then I want it to remove the last 3 characters. I keep getting a syntax error on the If statement. I'm not sure what im doing wrong.
UPPER(LEFT(objectname,IF(objectname = 'Report123' AND 'Report321', len(objectname)-3,len(objectname)-2))) AS Objectname,
thanks!
|
|
|
sunitabeck
Flowing Fount of Yak Knowledge
5152 Posts |
Posted - 01/10/2013 : 08:06:45
|
Use a case expression like shown below:UPPER(LEFT(objectname,
CASE
WHEN objectname = 'Report123' OR objectname = 'Report321'
THEN LEN(objectname)-3
ELSE LEN(objectname)-2
END )) If you have many such special cases, you can use the IN clause as in objectname in ('Report123','Report321') instead of individual equality expressions and OR operator. |
 |
|
|
Ethan135
Starting Member
3 Posts |
Posted - 01/10/2013 : 08:22:31
|
quote: Originally posted by sunitabeck
Use a case expression like shown below:UPPER(LEFT(objectname,
CASE
WHEN objectname = 'Report123' OR objectname = 'Report321'
THEN LEN(objectname)-3
ELSE LEN(objectname)-2
END )) If you have many such special cases, you can use the IN clause as in objectname in ('Report123','Report321') instead of individual equality expressions and OR operator.
That works perfectly. Can you explain why an IF isn't used here? |
 |
|
|
nigelrivett
Flowing Fount of Yak Knowledge
United Kingdom
3328 Posts |
Posted - 01/10/2013 : 08:59:46
|
An if statement is a control of flow instruction. It doesn't return a value.
========================================== Cursors are useful if you don't know sql. SSIS can be used in a similar way. Beer is not cold and it isn't fizzy. |
 |
|
|
Ethan135
Starting Member
3 Posts |
Posted - 01/10/2013 : 09:00:18
|
quote: Originally posted by nigelrivett
An if statement is a control of flow instruction. It doesn't return a value.
========================================== Cursors are useful if you don't know sql. SSIS can be used in a similar way. Beer is not cold and it isn't fizzy.
Thank you for your help |
 |
|
|
RajanThan
Starting Member
India
7 Posts |
Posted - 01/11/2013 : 00:17:42
|
select LEFT(objectname,LEN(objectname)-3) from tablename
Rajan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
48076 Posts |
Posted - 01/11/2013 : 00:20:56
|
quote: Originally posted by RajanThan
select LEFT(objectname,LEN(objectname)-3) from tablename
Rajan
what about conditional logic OP explained?
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
shan007
Starting Member
USA
16 Posts |
Posted - 01/11/2013 : 03:50:59
|
If cannot be used in the way as you used, If can validate boolean expression, it would validate True or False, according to the state we've to form the code. Your script with IF statement can be written as follows:
IF @objectname='Report123' or @objectname='Report321' select UPPER(Left(@objectname,len(@objectname)-3)) else select UPPER(left(@objectname,LEN(@objectname)-2))
this is not a best practice for this case, as it has dup code, but still i'd like to explain how IF statement can be used.
============================== I'm here to learn new things everyday.. |
 |
|
| |
Topic  |
|