473,836 Members | 1,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 309900
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(@deli m, @dt) - 1),
SUBSTRING(@dt, CHARINDEX(@deli m, @dt) + 1,
CHARINDEX(@deli m, @dt,
CHARINDEX(@deli m, @dt) + 1) -
CHARINDEX(@deli m, @dt) - 1) ,
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@deli m, REVERSE(@dt)) + 1, LEN(@dt))
ELSE IF LEN(@dt) - LEN(REPLACE(@dt , @delim, SPACE(0))) = 1
SELECT SUBSTRING(@dt, 1, CHARINDEX(@deli m, @dt) - 1),
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@deli m, 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
umeshchander
1 New Member
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(ve r, 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(@deli m, @dt) - 1),
SUBSTRING(@dt, CHARINDEX(@deli m, @dt) + 1,
CHARINDEX(@deli m, @dt,
CHARINDEX(@deli m, @dt) + 1) -
CHARINDEX(@deli m, @dt) - 1) ,
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@deli m, REVERSE(@dt)) + 1, LEN(@dt))
ELSE IF LEN(@dt) - LEN(REPLACE(@dt , @delim, SPACE(0))) = 1
SELECT SUBSTRING(@dt, 1, CHARINDEX(@deli m, @dt) - 1),
SUBSTRING(@dt, LEN(@dt)+ 1 -
CHARINDEX(@deli m, 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
4338
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 refreshing the page. I know I could write a form, submit it and go to another page. I've searched the web and I see something about HTTP Status code of 204
1
6428
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 IIf(InStr(,"*")>0,Left(,InStr(,"*")-1),) AS ShortName FROM corp_info Which gives a return value for the whole of ItemName if there is no star in it, or the portion up to the star if there is a star
1
25824
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 helful to you: /*************************************************************************** Description: Looks for a string inside another string and returns an integer that correspond to the position of first ocurrence.
19
3206
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
11
4292
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 thisVal OR thatVal AMONG("X", "Y", "Z").... The main thing is getting the allowed values in one place where the next guy can
6
7530
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 a "Connection Failed" error.
3
43839
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
1944
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 data. It’s because when actual page load in web browser it shows “-“ until loading is not completed after that it gives actual value after successful completion of loading of the page. Script I have written is as follows : <%...
1
2618
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 the teble. <% Response.Write("<SPAN class='tt'>Corn Futures (CZ)</SPAN>") Const REMOTE_FILE_URL="http://www.cmegroup.com/trading/commodities/grain-and-oilseed/corn_quotes_globex.html" Call ShowRemoteFile
0
9813
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10832
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5815
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.