February 16, 2016 at 10:56 am
I am trying to extract the 5-digit number out of a field that is formatted in the following manner:
XXXX-###-[highlight="#ffff11"]#####[/highlight]-#########-#
I need to extract the 5-digit number (highlighted) after the second '-' . I'd like to use SUBSTRING if possible in SQL. Help???
February 16, 2016 at 11:05 am
You have several options. You can play with substring and charindex if the first items have variable lengths, or you could split the whole string and take the part you need.
For the splitter, check this article: http://www.sqlservercentral.com/articles/Tally+Table/72993/
SELECT SUBSTRING( String, CHARINDEX( '-', String + '--', CHARINDEX( '-', String + '-') + 1) + 1, 5), --If the first 2 items have variable lengths
SUBSTRING( String, 10, 5) --If it's a fixed position
FROM (VALUES('XXXX-###-#####-#########-#'))x(String)
SELECT Item
FROM (VALUES('XXXX-###-#####-#########-#'))x(String)
CROSS APPLY dbo.DelimitedSplit8K( String, '-')
WHERE ItemNumber = 3
February 16, 2016 at 11:46 am
When I add the line: SUBSTRING(String, 10, 5)
It is giving me the following error: SQL API: [SQLExecDirectW], SQL RETURN: [-1], SQL STATE: [42000], SQL NATIVE ERROR: [-131], SQL MESSAGE: [[Sybase][ODBC Driver][Sybase lQ] Syntax error near ')' on line 27]
Any ideas?
February 16, 2016 at 12:24 pm
trvlbabie (2/16/2016)
When I add the line: SUBSTRING(String, 10, 5)It is giving me the following error: SQL API: [SQLExecDirectW], SQL RETURN: [-1], SQL STATE: [42000], SQL NATIVE ERROR: [-131], SQL MESSAGE: [[Sybase][ODBC Driver][Sybase lQ] Syntax error near ')' on line 27]
Any ideas?
To fix that error, go to a Sybase forum. The syntax that I showed is only guaranteed to work on SQL Server, because this is a SQL Server forum.
February 16, 2016 at 2:18 pm
I'm new to this so I wasn't sure - appreciate that you were so patient and helpful...not.
February 16, 2016 at 2:39 pm
trvlbabie (2/16/2016)
I'm new to this so I wasn't sure - appreciate that you were so patient and helpful...not.
I'll give you some advice since you say that your new to this. Pay attention to what you're doing. You wouldn't take a Corolla to a Volkswagen repair shop just because it is your first car.
I could have searched the syntax for the substring function in sybase, but it's not my job. I'm not getting paid for this and most people helping on forums aren't being paid either. So try to keep your immature responses for yourself.
If you have problems, the first thing you should do is to read the manual, then ask a co-worker and then ask online.
The logic is there, if you try to understand the code, you can fix it. If you don't understand it, don't use it.
February 16, 2016 at 3:22 pm
@trvlbabie, he WAS patient and helpful. what an ungrateful and rude person you are. luckily people like you are very rare on these forums.
February 16, 2016 at 4:13 pm
trvlbabie (2/16/2016)
I'm new to this so I wasn't sure - appreciate that you were so patient and helpful...not.
You must like pork chops. 🙂
The following will help. http://bfy.tw/4IPS
--Jeff Moden
Change is inevitable... Change for the better is not.
February 16, 2016 at 4:36 pm
Jeff Moden (2/16/2016)
trvlbabie (2/16/2016)
I'm new to this so I wasn't sure - appreciate that you were so patient and helpful...not.You must like pork chops. 🙂
The following will help. http://bfy.tw/4IPS
hahahahah - internet points to you for the day.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
February 16, 2016 at 8:56 pm
and most people helping on forums aren't being paid
Huh?!?! I just thought my SSC paycheck was really late. Aw man!
-- Itzik Ben-Gan 2001
February 16, 2016 at 9:30 pm
Alan.B (2/16/2016)
and most people helping on forums aren't being paid
Huh?!?! I just thought my SSC paycheck was really late. Aw man!
Maybe you haven't done the correct posts. I've just been paid for two of them.
February 17, 2016 at 6:48 am
trvlbabie (2/16/2016)
When I add the line: SUBSTRING(String, 10, 5)It is giving me the following error: SQL API: [SQLExecDirectW], SQL RETURN: [-1], SQL STATE: [42000], SQL NATIVE ERROR: [-131], SQL MESSAGE: [[Sybase][ODBC Driver][Sybase lQ] Syntax error near ')' on line 27]
Any ideas?
I suspect that us is because you can't use the VALUES constructor like Luis did using sybase. If what you are looking for however is the 10th through 15th characters you can use the SUBSTRING function ad Luis demonstrated. Otherwise you can work it out using Sybase's CHARINDEX or PATINDEX functions.
-- Itzik Ben-Gan 2001
February 18, 2016 at 1:42 pm
Jeff Moden (2/16/2016)
trvlbabie (2/16/2016)
I'm new to this so I wasn't sure - appreciate that you were so patient and helpful...not.You must like pork chops. 🙂
The following will help. http://bfy.tw/4IPS
I think this is the best thing I can take away from this thread, I have discovered "lmgtfy"
----------------------------------------------------
Viewing 13 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply