Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old April 1st, 2005, 11:43 AM
mayde78 mayde78 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 83 mayde78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 23 m 54 sec
Reputation Power: 5
String Manipulation in Query Result

I have the following rows in a table, i have to write a query which returns
results as shown

Table Values Result
-------------- ----------------
100 --> 100
100N --> 100N
100NCA ---> 100N(CA)
200J ---> 2000J
2000CA ---> 2000(CA)

Logic: Check last two characters of string, if both are alphabets,
then add ( ) before and after and return...for anything else just return original

How can i write such a query?

Thanks in advance

Reply With Quote
  #2  
Old April 2nd, 2005, 06:04 AM
yogaboy yogaboy is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Near London
Posts: 112 yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 37 m 29 sec
Reputation Power: 5
Try this, I tested it with your inputs and it seemed ok

eg
SELECT dbo.fnCheckLastTwoChars('1000j')...

Code:
CREATE FUNCTION fnCheckLastTwoChars
(
@myString varchar(20)
)
RETURNS varchar(20)
AS
BEGIN
	--declare variables
	DECLARE @my2ndRight char(1), @my1stRight char(1),@myLeft varchar(20)
	DECLARE @myLength smallint, @myNewString varchar(20)
	--store the different parts of the string
	SET @myLength = (LEN(@myString) - 1)
	SET @my2ndRight = RIGHT(@myString,1)
	SET @my1stRight = SUBSTRING(@myString,(@myLength),1)
	SET @myLeft = LEFT(@myString,@myLength - 1)
	
	IF IsNumeric(@my1stRight) = 1
		BEGIN
		SET @myNewString = @myString
		END
	ELSE
		BEGIN
	 	IF IsNumeric(@my2ndRight) = 0
			BEGIN
	   		SET @myNewString = (@myLeft + '(' + @my1stRight + @my2ndRight + ')')
			END
	  	ELSE
	   		BEGIN
	 		SET @myNewString = @myString
			END
		END
RETURN @myNewString
END

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > String Manipulation in Query Result


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT