SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Instr convert from access to sql server


Instr convert from access to sql server

Author
Message
Krasavita
Krasavita
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1973 Visits: 1894
How can I convert from access to sql server

NA: Right([Field6],Len([Field6])-InStr(1,[Field6],"Unit")+1)
andrewd.smith
andrewd.smith
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1560 Visits: 3232
The direct translation is:

RIGHT([Field6], LEN([Field6]) - CHARINDEX('Unit', [Field6]) + 1)



However, the the following is likely to be slightly more efficient.
Just change the final argument of the SUBSTRING function to be the maximum length of the [Field6] field.

SUBSTRING([Field6], CHARINDEX('Unit', [Field6]), 100)


Krasavita
Krasavita
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1973 Visits: 1894
Thank you, so

NA: Right([Field6],Len([Field6])-InStr(1,[Field6],"Unit")+1)

is equeal to this

RIGHT([Field6], LEN([Field6]) - CHARINDEX('Unit', [Field6]) + 1)


or this
SUBSTRING([Field6], CHARINDEX('Unit', [Field6]), 100)

right?

Your recomendation to use this:SUBSTRING([Field6], CHARINDEX('Unit', [Field6]), 100) right?
Krasavita
Krasavita
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1973 Visits: 1894
Also, how can I convert this:Len(Left([Field3],InStr(1,[Field3]," or")-1)) Thank you
andrewd.smith
andrewd.smith
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1560 Visits: 3232
If you use the SUBSTRING function, just set the 3rd argument of the SUBSTRING function to be the same as the maximum number of characters that the [Field6] column can contain.

SUBSTRING([Field6], CHARINDEX('Unit', [Field6]), 100)




Also, how can I convert this:Len(Left([Field3],InStr(1,[Field3]," or")-1)) Thank you


A translation of this Access expression is:

LEN(SUBSTRING([Field3], 1, CHARINDEX(' or', [Field3]) - 1))


DPhillips-731960
DPhillips-731960
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1250 Visits: 801
yulichka (3/6/2009)
Also, how can I convert this:Len(Left([Field3],InStr(1,[Field3]," or")-1)) Thank you


Why would you pull the LEN of a LEFT that uses a position value you will end up with in the LEN anyhow?

This:

Len(Left([Field3],InStr(1,[Field3]," or")-1))

is the same result as:

InStr(1,[Field3]," or")-1

?

So your solution would be to take the charindex of your search -1...
DPhillips-731960
DPhillips-731960
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1250 Visits: 801
andrewd.smith (3/6/2009)
...
A translation of this Access expression is:

LEN(SUBSTRING([Field3], 1, CHARINDEX(' or', [Field3]) - 1))



Or simply:

CHARINDEX(' or', [Field3]) - 1


Krasavita
Krasavita
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1973 Visits: 1894
Thank you
andrewd.smith
andrewd.smith
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1560 Visits: 3232
dphillips,

Yes, you are probably right that using CHARINDEX alone may be a more exact translation of the Access behaviour. It's a while since I used Access so I can't remember how Access deals with trailing whitespace. However, the 2 expressions don't always give the same result in T-SQL because trailing whitespace is not counted by the T-SQL LEN function, as demonstrated by the following example.

DECLARE @s varchar(100)
SELECT @s = 'Option1 or Option2' /* NB 3 spaces between 'Option1' and 'or' */

SELECT SUBSTRING(@s, 1, CHARINDEX(' or', @s) - 1),
LEN(SUBSTRING(@s, 1, CHARINDEX(' or', @s) - 1)),
CHARINDEX(' or', @s) - 1



SubField Length CharIndex
----------------------- ----------- -----------
Option1 7 9

DPhillips-731960
DPhillips-731960
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1250 Visits: 801
True. Access does not strip spaces. So it depends on desired outcome.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search