SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Len and If Statement help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Ethan135
Starting Member

3 Posts

Posted - 01/10/2013 :  08:02:06  Show Profile  Reply with Quote
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

5155 Posts

Posted - 01/10/2013 :  08:06:45  Show Profile  Reply with Quote
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.
Go to Top of Page

Ethan135
Starting Member

3 Posts

Posted - 01/10/2013 :  08:22:31  Show Profile  Reply with Quote
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?
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 01/10/2013 :  08:59:46  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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.
Go to Top of Page

Ethan135
Starting Member

3 Posts

Posted - 01/10/2013 :  09:00:18  Show Profile  Reply with Quote
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
Go to Top of Page

RajanThan
Starting Member

India
7 Posts

Posted - 01/11/2013 :  00:17:42  Show Profile  Reply with Quote
select

LEFT(objectname,LEN(objectname)-3) from tablename

Rajan
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/11/2013 :  00:20:56  Show Profile  Reply with Quote
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/

Go to Top of Page

shan007
Starting Member

USA
17 Posts

Posted - 01/11/2013 :  03:50:59  Show Profile  Reply with Quote
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..
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000