在NVarChar列中指定位置和字符

时间:2012-01-20 03:55:23

标签: sql-server select character

我正在尝试查询列的第11个字符为'G'或'W'的记录数以及日期约束,但是没有太多运气。这是我正在尝试的:

SELECT * st_req_no, dt_order 
FROM shipment_info  
WHERE dt_order >= '10/01/2010' 
AND (SELECT CHARINDEX('G', st_req_no, 11) OR CHARINDEX('W', st_req_no, 11))) 
ORDER BY dt_order

有人可以告诉我我做错了什么吗?谢谢!

2 个答案:

答案 0 :(得分:3)

Select * 
from shipment_info
where dt_order >= '10/01/2010'
    AND SUBSTRING(st_req_no, 11, 1) IN ('G', 'W')
order by dt_order

答案 1 :(得分:-1)

Select      * st_req_no
            , dt_order 
from        shipment_info 

WHERE       (CHARINDEX('G',st_req_no,10) = 11 OR CHARINDEX('W',st_req_no,10) = 11)
        AND dt_order >= '10/01/2010' 

order by dt_order