Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 undetermined string's length

Author  Topic 

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2008-07-28 : 15:43:45
Hello,

Suppose I have a query.
SELECT TOP 5 ID,UNT FROM P
WHERE LEFT(RIGHT(NO,7),3)='080'

But the result of LEFT(RIGHT(NO,7),3) could be '80' or '080'.

So how to modify the query?

Thanks

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-07-28 : 15:47:12
WHERE LEFT(RIGHT(NO,7),3) in ('080','80')

If this database is your design, be sure to read this:

http://weblogs.sqlteam.com/jeffs/archive/2008/06/11/golden-rule-of-data-manipulation.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2008-07-28 : 16:00:41
Can it have multiple values for this?

WHERE LEFT(RIGHT(NO,7),3) in ('0008','008','08','8')
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-07-28 : 16:03:25
Here's an idea: try it! Don't ask some stranger on some forum to test things for you, try it yourself, that's the best way to learn.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-07-28 : 16:07:30
LEFT(x, 3) cannot equal '0008'

You could also do: WHERE LEFT('000' + RIGHT(NO, 7), 3) = '080'
Or maybe (not sure if this meets your requirements): WHERE LEFT(RIGHT(NO,7),3) LIKE '%80'
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-07-28 : 16:24:52
or ... you could simply explain your requirements. It's always useful for everyone involved to have the requirements spelled out clearly and completely.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2008-07-28 : 16:28:39
Well, actually the right side is a variable.
I don't know its length.Its maximum length is 3.
I tried this query but can't run.


WHERE LEFT(RIGHT(NO,7),3) in ('00'+@x,'0'+@x,@x)

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-28 : 21:08:03
[code]
WHERE LEFT(RIGHT(NO,7), len(@VARIABLE)) = @VARIABLE
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -