If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Convert MSSQL TSQL to a Formula

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-23-09, 05:13
GWilliy GWilliy is offline
Registered User
 
Join Date: Jan 2003
Location: Nottinghamshire, UK
Posts: 364
Convert MSSQL TSQL to a Formula

Hi Folks

First time I've posted in the Excel section, I normally float around in the SQL Server Section.

I have a TSQL Statement that returns an Insurance Policy Number in a Business Format from a Database SQL Format and wonder if anyone could tell me how I could do the same thing in an Excel Formula.

The System Policys are represented as a String made up of three sections

1-5 numbers (1 to 19,999)
A Character a-z (A to Z)
1-2 numbers (0 to 99)

in that Order

I would like to flip the cell value with a formula into

1-2 numbers (0 to 99)
A Character a-z (A to Z)
1-5 numbers (1 to 19,999)

The TSQL I use to do this is:-

Code:
 
SET @PatInd = PATINDEX('%[a-z]%',@InPolicy)
SELECT SUBSTRING(@InPolicy,@PatInd +1,LEN(@InPolicy)- @PatInd)
+
SUBSTRING(@InPolicy,@PatInd,1)
+
LEFT(@InPolicy,@PatInd -1)

where PatIndex is TSQL for returning the integer position of a charcter within a String

examples Policys are
Code:
 
System Business
501A80 80A501
1000G80 80G1000
10001K80 80K10001
10000K80 80K10000


Any help muchly appreciated

GW
__________________
"Everything should be made as simple as possible, but not simpler." - Albert Einstein
"Everything should be made as complex as possible, so I look Cleverer." - Application Developer
Reply With Quote
  #2 (permalink)  
Old 02-24-09, 08:28
GWilliy GWilliy is offline
Registered User
 
Join Date: Jan 2003
Location: Nottinghamshire, UK
Posts: 364
Mmmmm 25 Views and no takers?

Is this a difficult one ?
__________________
"Everything should be made as simple as possible, but not simpler." - Albert Einstein
"Everything should be made as complex as possible, so I look Cleverer." - Application Developer
Reply With Quote
  #3 (permalink)  
Old 02-24-09, 10:55
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
No equivalent to PATINDEX in Excel. You'll probably have to create a UDF in VBA and loop through the characters, testing (for example) the ASCII value until you find the first alpha character.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On