473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Instr() Equivalent in SQL Server

DTB
I am trying to convert a complex function from Oracle to SQL Server
and have come across Oracle's Instr() function. I see SQL Server has
CHARINDEX() which is similar, however it does not provide some key
functionality I need. Here is an example of the Oracle code:

if Instr( sTg , cDelim, 1, 3 ) > 0 then

sd := SubStr( sTg, Instr( sTg , cDelim, 1, 1 ) + 1, Instr( sTg,
cDelim, 1, 2 ) - Instr( sTg , cDelim, 1, 1 ) - 1)

end if;

Has anybody converted anything similar to this within SQL Server? Any
help is GREATLY appreciated!

Thanks.
Jul 20 '05 #1
5 309850
You can use SUBSTRING in T-SQL as well. Check out CHARINDEX & SUBSTRING
functions in SQL Server Books Online. If you need specific help, please post
an example string, with a brief explanation of the logic to be applied along
with the expected result.

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #2
Thank you, Here is a more detailed example of the code I need to
convert. The combo if SUBSTRING and CHARINDEX are not working
sufficiently enough with this scenario.

varchar2 sTg := '01/01/1999'
char cDelim := '/'

if Instr( sTg , cDelim, 1, 3 ) > 0 then
-- if there are more than two, return null
return null;
elsif Instr( sTg , cDelim, 1, 2 ) > 0 then
-- else parse the date
sMonth := SubStr( sTg, 1, Instr( sTg , cDelim, 1, 1 ) - 1 );
sDay := SubStr( sTg, Instr( sTg , cDelim, 1, 1 ) + 1, Instr(
sTg , cDelim, 1, 2 ) - Instr( sTg , cDelim, 1, 1 ) - 1);
sYear := SubStr( sTg, Instr( sTg , cDelim, 1, 2 ) + 1);
elsif Instr( sTg , cDelim, 1, 1 ) > 0 then
sMonth := SubStr( sTg, 1, Instr( sTg , cDelim, 1, 1 ) - 1 );
sYear := SubStr( sTg, Instr( sTg , cDelim, 1, 1 ) + 1);
sDay := '';
else
return null;
end if;

The expected result would be to parse out the month, day and year. The
string may come in as any of the following: 10/01/1999, 10/1999,
10.01.99, 10 01 99, etc...

Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
You can do this in several ways. The most common ones are using LEFT, RIGHT
functions. However, since I initially suggested you use SUBSTRING &
CHARINDEX, here is a solution using only those functions:

DECLARE @dt VARCHAR(15), @delim CHAR(1)
SET @dt = '10/09/1999'
SET @delim = '/'
IF LEN(@dt) - LEN(REPLACE(@dt, @delim, SPACE(0))) = 2
SELECT SUBSTRING(@dt, 1, CHARINDEX(@delim, @dt) - 1),
SUBSTRING(@dt, CHARINDEX(@delim, @dt) + 1,
CHARINDEX(@delim, @dt,
CHARINDEX(@delim, @dt) + 1) -
CHARINDEX(@delim, @dt) - 1) ,
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@delim, REVERSE(@dt)) + 1, LEN(@dt))
ELSE IF LEN(@dt) - LEN(REPLACE(@dt, @delim, SPACE(0))) = 1
SELECT SUBSTRING(@dt, 1, CHARINDEX(@delim, @dt) - 1),
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@delim, REVERSE(@dt)) + 1, LEN(@dt))
ELSE
SELECT @dt

Note that you can use CASE expressions, if you need it as a single SQL
statement. As mentioned before, if you need to re-write this using LEFT,
RIGHT etc, please post back.

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #4
Thanks Anith! I will try this and see if it gives me what I need.
Thank you!

David
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
HI Anith,
I am new to this SQL so could u please also resolve one of my problem.
Could u please change the following oracle code in SQL so that i can proceed with other functionality.

Hope the group will help me out.

start_pos = INSTR(ver, '.', 1, 1) -1;
end_pos = INSTR(ver, '.', 1, 2) +3;
ver_core = RTRIM(SUBSTR(ver, start_pos, end_pos - start_pos), CHR(10));

ver_ctry = SUBSTR(ver, start_pos, end_pos - start_pos);
ver := ver_core || '/' || ver_ctry || '/' || ver_trans;


You can do this in several ways. The most common ones are using LEFT, RIGHT
functions. However, since I initially suggested you use SUBSTRING &
CHARINDEX, here is a solution using only those functions:

DECLARE @dt VARCHAR(15), @delim CHAR(1)
SET @dt = '10/09/1999'
SET @delim = '/'
IF LEN(@dt) - LEN(REPLACE(@dt, @delim, SPACE(0))) = 2
SELECT SUBSTRING(@dt, 1, CHARINDEX(@delim, @dt) - 1),
SUBSTRING(@dt, CHARINDEX(@delim, @dt) + 1,
CHARINDEX(@delim, @dt,
CHARINDEX(@delim, @dt) + 1) -
CHARINDEX(@delim, @dt) - 1) ,
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@delim, REVERSE(@dt)) + 1, LEN(@dt))
ELSE IF LEN(@dt) - LEN(REPLACE(@dt, @delim, SPACE(0))) = 1
SELECT SUBSTRING(@dt, 1, CHARINDEX(@delim, @dt) - 1),
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@delim, REVERSE(@dt)) + 1, LEN(@dt))
ELSE
SELECT @dt

Note that you can use CASE expressions, if you need it as a single SQL
statement. As mentioned before, if you need to re-write this using LEFT,
RIGHT etc, please post back.

--
- Anith
( Please reply to newsgroups only )
Jul 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: wk6pack | last post by:
Hi, I have a problem and not quite how to go about solving it. I have a form written in asp. I wish to submit the form and have the server return back to the same page without actually...
1
by: Robin Hammond | last post by:
I am trying to upsize a database to SQL server (on which I am a novice). In Access as part of a much more complex query I had the following (from sql view) SELECT...
1
by: Icaro | last post by:
Hi everybody, I was looking for an equivalent ORACLE INSTR Function in MSSQL but I don´t found it and I don´t know if it exist so I must to write it and this is the code. Maybe it will be...
19
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
11
by: (Pete Cresswell) | last post by:
I'd like to avoid coding a long set of IF... OR IF...statements. I can do a Select Case, but that would only let me case out on a single value. What I'm looking for is something like If...
6
by: Patrick A | last post by:
All - I have the following in the Field row of a column in a query that runs fine against a local table: MSA: InStr(!!,!) I'm now trying to run the query against a linked DB2 table. I get...
3
by: Brad Markisohn | last post by:
Is there a C# equivalent to VB's instr function to determine if the contents of one string are present in another? Brad
3
by: vtpvp82 | last post by:
I am having problem in getting response from remote server for that I have written following script response got correctly but the data on the page is not displayed it gives “–“ in place of grid...
1
by: vtp82 | last post by:
can anybody tell me how can i get response from remote server. I am having problem in getting response from remote server for that I have written following script. But its not showing values in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.