I need to run a simple select statement for a column called AddrZip to show all records that contain '1/2 ' after the first space in the column. In Access 2007 it would be:
**Left([Names],InStr(1,[Names]," ")-1)
, but can't find out how to do it in SQL 2005. All help will be appreciated.
views:
40answers:
2
A:
Try this -
select * from table where addrZip like '%\ 1/2%' escape '\'
Sachin Shanbhag
2010-10-22 16:49:08
A:
First, look for records with a ' '
:
CHARINDEX(' ', [AddrZip]) > 0
Then look for records with a '1/2'
occurring after the ' '
CHARINDEX('1/2', [AddrZip], CHARINDEX(' ', [AddrZip])) > 0
SELECT *
FROM ( SELECT *
FROM [Addresses]
WHERE CHARINDEX(' ', [AddrZip]) > 0
) x
WHERE CHARINDEX('1/2', [x].[AddrZip], CHARINDEX(' ', [AddrZip])) > 0
This "simplified" version may work:
SELECT *
FROM [Addresses]
WHERE CHARINDEX(' ', [AddrZip]) > 0
AND CHARINDEX('1/2', [x].[AddrZip], CHARINDEX(' ', [AddrZip])) > 0
If you want to find occurrences of '1/2'
that are immediately preceded by a ' '
where the ' '
is the very first space in the string, then use the following code:
SELECT *
FROM [Addresses]
WHERE CHARINDEX(' ', [AddrZip]) > 0
AND CHARINDEX(' ', [AddrZip]) = CHARINDEX(' 1/2', [x].[AddrZip])
Avoid LIKE
operators if at all possible. They are notoriously slow.
Brad
2010-10-22 16:52:46
@Brad - Just a thought. This matches even strings 'sss 11/2' which is not what OP wants
Sachin Shanbhag
2010-10-22 17:02:16
@Sachin, why not? I see a '1/2' and a ' ' before it? What that not the OP's specifications? The OP did not say that the '1/2' needed to **immediately** follow the ' '.
Brad
2010-10-22 17:03:59
@Brad - Yes, you naybe right. There is no mention of 1/2 following a space immediately. I have assumed the 1/2 follows space thing. Lets see what OP really wants. ;)
Sachin Shanbhag
2010-10-22 17:07:58