简体   繁体   English

访问查询与SQL Server 2008查询

[英]Access query vs SQL Server 2008 query

I'm trying to convert this query from an Access db to work in SQL Server 2008 and got stuck. 我试图将此查询从Access数据库转换为可在SQL Server 2008中工作并卡住。 I know there are differences, but I'm not sure what's going on and apparently I'm not very good at this. 我知道有差异,但是我不确定发生了什么,显然我不太擅长此事。 Any help would be appreciated. 任何帮助,将不胜感激。

Access 访问

Mid(Trim([SC01039]),
InStrRev(Trim([SC01039])," ")+3,4)
AS ProductType

SQL Server 2008 (This is what I tried changing it to) SQL Server 2008 (这是我尝试将其更改为的内容)

Substring(RTrim(LTrim([SC01039])),
Right(RTrim(LTrim([SC01039])),
CHARINDEX(' ',RTrim(LTrim([SC01039]))))+3,4) 
AS ProductType

The error I receive is 我收到的错误是

"Conversion failed when converting the nvarchar value 'L318' to data type int." “将nvarchar值'L318'转换为数据类型int时转换失败。”

Why is it trying to convert the resulting text into an integer? 为什么要尝试将结果文本转换为整数? My assumption is the query is saying, "Find the position of the space closest to the right of the string, now add 3 to that position and give me the next 4 characters." 我的假设是查询的意思是:“找到最靠近字符串右边的空间的位置,现在在该位置加3,然后给我接下来的4个字符。” It seems to be a problem with the data, not the query (what does it do if there is no space? Or the string is null?) I dunno... 似乎是数据问题,而不是查询问题(如果没有空间,怎么办?或者字符串为null?),我不知道...

Did I even get close? 我什至没有接近? :P :P

There are mixed characters in SC01039, but this is a sample: SC01039中包含混合字符,但这是一个示例:

SC01039         :     Expected Output
-------------   :     ---------------
QC   06999911   :     9999
SW   12FMT116   :     FMT1
26RMF399        :     RMF3
08              :     [empty]
[empty]         :     [empty]

Apparently it is always 4 characters, starting 3 to the right of the first space found (searching for the space from right-to-left). 显然,它始终是4个字符,从找到的第一个空格的右边开始3个字符(从右到左搜索空格)。 Some data in SC01039 have multiple spaces (ie: 09 TVR 012 2, in this case it doesn't return anything and I believe that is OK). SC01039中的某些数据具有多个空格(例如:09 TVR 012 2,在这种情况下,它不返回任何内容,我相信可以)。

You problem is with Right(RTrim(LTrim([SC01039])), part of your code. Why do you need it? Because what I've shown below should do the trick. 您的问题出在代码的一部分Right(RTrim(LTrim([SC01039])), 。为什么需要它呢?因为下面显示的内容可以解决这个问题。

Try this: 尝试这个:

Substring(RTrim(LTrim([SC01039])),
CHARINDEX(' ',RTrim(LTrim([SC01039])))+3,4) 
AS ProductType

The Substring is expecting SUBSTRING(expression, start, length) and you provided the second parameter with a nvarchar; 子字符串期望SUBSTRING(expression, start, length)并且您为第二个参数提供了nvarchar。

Right(RTrim(LTrim([SC01039])),
CHARINDEX(' ',RTrim(LTrim([SC01039]))))+3

hence your error. 因此你的错误。

Create this function (you don't have to put it helps with readability 创建此功能(您不必放置它有助于提高可读性

CREATE FUNCTION InStrRev(@Val nvarchar(max), @substr nvarchar(max))
RETURNS int
AS
BEGIN
RETURN CASE WHEN CHARINDEX(@substr, RTrim(LTrim(Reverse(@Val)))) = 0 THEN 0 ELSE LEN(@Val) - CHARINDEX(@substr, RTrim(LTrim(Reverse(@Val)))) + 1 END  -- add one because you want a start position
END

and use it in your code like this 并在这样的代码中使用它

Substring(RTrim(LTrim([SC01039])), dbo.InStrRev([SC01039], ' ') + 3, 4) AS ProductType

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM