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.
Author |
Topic |
kt
Yak Posting Veteran
88 Posts |
Posted - 2013-09-11 : 15:10:31
|
Hi,this query returned the results belowselect RIGHT(t_item,5) as ENo ,REPLACE(t_item,RIGHT(t_item,5),'') as dItem from tcbwhere t_item like '%/E%'---and dItem = 'abc'Results:ENo dItem123 abc456 defg789 xyzbut when i add the condition and dItem = 'abc' then error:Invalid column name 'delItem'.Thanks |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-11 : 15:17:50
|
You cannot use the aliases you define in your select list (In this case dItem) anywhere else in the query except in the order by clause. So to achieve what you want, you have to reproduce the expression in the where clause as well.select RIGHT(t_item,5) as ENo ,REPLACE(t_item,RIGHT(t_item,5),'') as dItem from tcbwhere t_item like '%/E%'and RIGHT(t_item,5) as ENo ,REPLACE(t_item,RIGHT(t_item,5),'') = 'abc' |
|
|
kt
Yak Posting Veteran
88 Posts |
Posted - 2013-09-11 : 15:24:30
|
tried and got error Incorrect syntax near the keyword 'as'.where it was point to this lineand RIGHT(t_item,5) as ENo ,REPLACE(t_item,RIGHT(t_item,5),'') = 'abc' |
|
|
kt
Yak Posting Veteran
88 Posts |
Posted - 2013-09-11 : 15:30:00
|
it is not working either.i got this working with thisselect ENofrom ( select RIGHT(t_item,5) as ENo ,REPLACE(t_item,RIGHT(t_item,5),'') as dItem from tcb where t_item like '%/E%' ) xwhere dItem ='abc'thanks all |
|
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-09-11 : 15:31:10
|
quote: Originally posted by James K You cannot use the aliases you define in your select list (In this case dItem) anywhere else in the query except in the order by clause. So to achieve what you want, you have to reproduce the expression in the where clause as well.select RIGHT(t_item,5) as ENo ,REPLACE(t_item,RIGHT(t_item,5),'') as dItem from tcbwhere t_item like '%/E%'and RIGHT(t_item,5) as ENo ,REPLACE(t_item,RIGHT(t_item,5),'') = 'abc'
|
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-11 : 15:44:25
|
Thanks MuMu! Sorry kt!!Can I get away by claiming that what Mumu posted is what I really meant? |
|
|
kt
Yak Posting Veteran
88 Posts |
Posted - 2013-09-11 : 17:12:37
|
worked thank you for of your help James and MuMu , have a great day |
|
|
|
|
|
|
|