I have two SQL queries:
A.
SELECT (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) +
upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v)) ) AS userCompareStr
FROM atable ;
and
B.
SELECT (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) +
upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v)) ) AS userCompareStr
FROM atable WHERE userCompareStr='GAPYLE1111' ;
I have the following code:
Dim sql As String
Dim conn As OleDbConnection
Dim cmd As OleDbDataAdapter
Dim ds As DataSet
Dim tbl As DataTable
conn = " something here "
cmd = New OleDbDataAdapter(sql, conn)
ds = New DataSet
cmd.Fill(ds)
tbl = New DataTable
tbl = ds.Tables(0)
Near as I can tell it seems to work when sql is set to string A, but not when it’s set to string B.
This leads me to suspect that there is something wrong with the clause
WHERE userCompareStr=’GAPYLE1111′
Can I not use the alias userCompareStr in this way? I can’t find any examples of this kind of use, but I do find analogous use when alias is used for table name — and I don’t see anything against that kind of us.
You have three options.
1) repeat what you did in the select in the where
2) Use a common table expression
3) Inline query see Maziar Taheri’s answer
As an aside I hope ‘GAPYLE1111’ doesn’t come from user input, otherwise you’re exposing yourself to SQL Injection attacks. Use parameterized queries instead