473,372 Members | 1,050 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,372 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 309842
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.