[英]SQL View SUBSTRING and CHARINDEX
I have a query in SQL 2008 我在SQL 2008中有一个查询
SELECT [orde_reference],
SUBSTRING([orde_reference],
CHARINDEX('/', [orde_reference]) + 1,
LEN([orde_reference])) AS batch
FROM Orders
That returns the following 返回以下内容
orde_reference: 27777/2012/1
batch: 2012/1
However I need the batch to just be the characters after the last / (there are always 2 x '/' in the varchar 但是我需要批处理只是最后一个/之后的字符(varchar中总是有2 x'/'
orde_reference: 27777/2012/1
batch: 1
Any help would be appreciated. 任何帮助,将不胜感激。
Cheers Mim 干杯
Use REVERSE twice ;) 两次使用REVERSE;)
SELECT [orde_reference]
,REVERSE(SUBSTRING(reverse([orde_reference]), 0, CHARINDEX('/', reverse([orde_reference])))) AS batch
from (values(
' 27777/2012/123'
)) as O([orde_reference])
As there are always 2 /
you can pass the optional third argument to CHARINDEX
( start_location
) to tell it to start looking after the first one. 由于总是2
/
您可以将可选的第三个参数传递给CHARINDEX
( start_location
),以告诉它开始照看第一个参数。
SELECT [orde_reference],
SUBSTRING([orde_reference],
CHARINDEX('/', [orde_reference],1 + CHARINDEX('/', [orde_reference])) + 1,
LEN([orde_reference])) AS batch
FROM Orders
You would be better off storing these components individually however in separate columns. 您最好将这些组件单独存储在单独的列中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.