I have table called FinalForgotten which only contains one field called aname. The field could either look like Smith John or Smith,John. So both last and first name are in same field and delimited by either space or comma. The defense field contains three fields: first_name,last_name,middle_initial. The first_name field will contain data that matches exactly a piece a data IN aname field (e.g. John). And the last_name field will contain data that matches exactly a piece of data IN aname field (e.g. Smith). I'm trying to get all the FinalForgotten aname records with a middle initial into a new table (e.g. Smith,John S). The defense table is what has this middle initial.
This would work:
SELECT left([aname],InStr(1,[aname],",")-1) & " "& right([aname],Len(aname)-InStr(1,[aname],",")) & " "& summary_judgment.middle_initial AS fullnameINTO FinalForgottenWithMiddle FROM FinalForgotten INNER JOIN summary_judgment ON((left(FinalForgotten.aname,InStr(1,FinalForgotten.[aname],",")-1))=summary_judgment.last_name) AND((right(FinalForgotten.aname,Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],","))=summary_judgment.first_name));
But it will return "invalid procedure call" should FinalForgotten contain a field that doesn't have a comma like:
Smith John.
Hence, to address this, I tried to factor whether a comma was in the field or not:
SELECT left([aname], IIF(instr([aname], ",") = 0, InStr(1,[aname]," ")-1),InStr(1,[aname],",")-1) & ", " & right([aname], IIF(instr([aname], ",") = 0,Len(aname)-InStr(1,[aname]," "),Len(aname)-InStr(1,[aname],",") & " " & defense_final.middle_initial AS fullname INTO FinalForgottenWithMiddle
FROM FinalForgotten INNER JOIN defense_final ON
((right(FinalForgotten.aname,IIF(instr([aname], ",") = 0,Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname]," ")),Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],","))=defense_final.first_name))
AND
((left(FinalForgotten.aname,,IIF(instr([aname], ",") = 0,InStr(1,FinalForgotten.[aname]," ")-1)),InStr(1,FinalForgotten.[aname],",")-1))=defense_final.last_name);
This gives me a "missing operator syntax" error and highlights the word AS.
Thanks for response.
There seems to be quite a few missing parentheses.
SELECT left(
[aname],
IIF(instr([aname], ",") = 0,
InStr(1,[aname]," ")-1,
InStr(1,[aname],",")-1
)
)
& ", " &
right(
[aname],
IIF(instr([aname], ",") = 0,
Len(aname)-InStr(1,[aname]," "),
Len(aname)-InStr(1,[aname],",")
)
)
& " " &
defense_final.middle_initial AS fullname
INTO FinalForgottenWithMiddle
FROM FinalForgotten
INNER JOIN defense_final
ON
right(FinalForgotten.aname,
IIF(instr([aname], ",") = 0,
Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname]," "),
Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],",")
)
)=defense_final.first_name
AND
left(FinalForgotten.aname,
IIF(instr([aname], ",") = 0,
InStr(1,FinalForgotten.[aname]," ")-1,
InStr(1,FinalForgotten.[aname],",")-1
)
)=defense_final.last_name
Related
I have a requirement to display select statement on SSRS report (UI). I want to display select, from and where in Bold and blue color. Field name = "Query" and below is a record
select field1, field2 from table1 where field1='test'
I tried both below font->expression of "Query" field, but it's not working.
=Replace("SELECT",Fields!Query.Value," `<span style='color:red'>` " & Fields!Query.Value & "`</span>`")
=Replace(Fields!Query.Value,"SELECT"," `<span style='color:red'>` " & Fields!Query.Value & "`</span>`")
The way I would do this is as follows
note: my column is called sql, not Query as it is in yours
Create a textbox (or edit your existing one)
Type SELECT then a [space]
Then right-click inside the text box to the right of what you just typed and choose Create placeholder
Set the value expression to be
=
MID(
Fields!sql.Value,
INSTR(Fields!sql.Value, "SELECT ", Compare:= Comparemethod.Text) + 7,
INSTR(Fields!sql.Value, "FROM", Compare:= Comparemethod.Text)
- (INSTR(Fields!sql.Value, "SELECT ", Compare:= Comparemethod.Text) + 8)
)
Now type a [space] and then 'FROM', add your next placeholder with the following expression.
=
MID(
Fields!sql.Value,
INSTR(Fields!sql.Value, "FROM ", Compare:= Comparemethod.Text) + 5,
INSTR(Fields!sql.Value, "WHERE", Compare:= Comparemethod.Text)
- (INSTR(Fields!sql.Value, "FROM ", Compare:= Comparemethod.Text) + 6)
)
finally type "WHERE " and the last placeholder, set the expression to...
=
MID(
Fields!sql.Value,
INSTR(Fields!sql.Value, "WHERE ", Compare:= Comparemethod.Text) + 6
)
You can now double-click the static words and format as you wish. You could do the same with each expression to as all placeholders have their own font properties.
The final design looks like this...
And the final output looks like this.
I am attempting to use a IIF() statement in a query to update a short text field to always be 4 characters. This is my syntax, but
SELECT DISTINCT IIf(Len([User ID] = 1), "000" & [User ID], IIf(Len([User ID] = 2), "00" & [User ID], IIf(Len([User ID] = 3), "0" & [User ID], [User ID]))) AS ST
FROM _TestData;
However it seems to be appending 0's to the data regardless of length?
It could be this simple:
SELECT DISTINCT Right("000" & [User ID], 4) AS ST
FROM TestData;
If [User ID] is number, try this:
SELECT DISTINCT Format([User ID], "0000") AS ST
FROM _TestData;
Update
Missed that the field is short text. In this case
SELECT DISTINCT Format(CLng(Nz([User ID],0)), "0000") AS ST
FROM _TestData;
When I set up Allen Browne's ConcatRelated in a query to use a date range, I get ever comment in that range in each comment field. I want to group by an assembly line name but I get each line's comments. My SQL query is below. Any help would be greatly appreciated.
I have tried a sub query but I still get the same result or a syntax error depending on how I format.
SELECT
Asm_Equipment_Rate.Line_Name,
Avg(Asm_Equipment_Rate.Std_Pnls_Lbr_Hr) AS RAsm_Line_Std_Hrs,
Sum(Asm_Prod_Data.Lbr_Hrs) AS RAsm_Line_Total_Hrs,
([RAsm_Line_Std_Hrs]*[RAsm_Line_Total_Hrs]) AS RT100_Pct_Target,
([RT100_Pct_Target]*0.9) AS RT90_Pct_Target,
Sum(Asm_Prod_Data.Produced) AS RTotal_Produced,
Sum(Asm_Prod_Data.Backflushed) AS RTotal_Backflushed,
[RTotal_Produced]/[RT100_Pct_Target] AS RAsm_Line_EFF,
Sum(Asm_Prod_Data.Scrap_Qty) AS RAsm_Scrapped_Panels,
Sum(Asm_Prod_Data.Reworked) AS RAsm_Reworked_Panels,
IIf(([RAsm_Scrapped_Panels]+[RAsm_Reworked_Panels])=0,1,1-
([RAsm_Scrapped_Panels]+[RAsm_Reworked_Panels])/([RAsm_Scrapped_Panels]+
[RAsm_Reworked_Panels]+[RTotal_Produced])) AS RFYP,
ConcatRelated
('Comments',
'Asm_Prod_Data',
'PA_date Between ' & Format([Forms]![Date Prompt]!
[txtBDate],'\#yyyy-m-d\#') & ' And ' & Format([Forms]![Date Prompt]!
[txtEDate],'\#yyyy-m-d\#'),
'Comments',
', ') AS RConCat_Comments
FROM Asm_Equipment_Rate INNER JOIN Asm_Prod_Data ON
Asm_Equipment_Rate.Equipment = Asm_Prod_Data.P_Line
WHERE (((Asm_Prod_Data.PA_Date) Between [Forms]![Date Prompt]![txtBDate]
And [Forms]![Date Prompt]![txtEDate]))
GROUP BY Asm_Equipment_Rate.Line_Name;
Regards,
Bill
change the WHERE clause to
'PA_date Between ' &
Format([Forms]![Date Prompt]![txtBDate],'\#yyyy-m-d\#') &
' And ' & Format([Forms]![Date Prompt]![txtEDate],'\#yyyy-m-d\#') &
' And P_Line=' & Asm_Prod_Data.P_Line,
You'll need to quote the value if P_Line is text.
Also, I think when you call a function you need to use double quotes inside the function call, although if what you have there is working, that's not true. Maybe it's changed for later Access versions.
EDIT: I mean the WHERE clause in the function call
Try this
SELECT
Asm_Equipment_Rate.Line_Name,
ConcatRelated
('Comments',
'Asm_Prod_Data',
'PA_date Between '
& Format([Forms]![Date Prompt]![txtBDate],'\#yyyy-m-d\#')
& ' And ' & Format([Forms]![Date Prompt]![txtEDate],'\#yyyy-m-d\#')
& ' And P_Line=' & Asm_Prod_Data.P_Line,
'Comments',
', ') AS RConCat_Comments
FROM Asm_Equipment_Rate INNER JOIN Asm_Prod_Data
ON Asm_Equipment_Rate.Equipment = Asm_Prod_Data.P_Line
WHERE (((Asm_Prod_Data.PA_Date) Between [Forms]![Date Prompt]![txtBDate]
And [Forms]![Date Prompt]![txtEDate]))
GROUP BY Asm_Equipment_Rate.Line_Name,
ConcatRelated
('Comments',
'Asm_Prod_Data',
'PA_date Between '
& Format([Forms]![Date Prompt]![txtBDate],'\#yyyy-m-d\#')
& ' And ' & Format([Forms]![Date Prompt]![txtEDate],'\#yyyy-m-d\#')
& ' And P_Line=' & Asm_Prod_Data.P_Line,
'Comments',
', ');
It's unfortunate but the GROUP BY expression has to be identical to the one in the SELECT clause, no matter how complex.
I need to display data in a lastname, firstname, salutation format. However there are a few rare instances where salutation is null, so I wouldn't want to display firstname, if salutation is null. Or there (doubtful) could be a possibility that firstname is null and salutation so I wouldn't want to show lastname, , ,.
How can I in my query use a condition to only include the comma if the next field is not null?
NameWithSal: [LastName] + ", " + [FirstName] + ", " + [Salutation]
In Access, what you have will result in null if any of the fields are null. The following should provide what you specified. [LastName] & ", " + [FirstName] & ", " + [Salutation]
I need help.... I am not good at SQL I get this error when I try to apply a JOIN:
[ Token line number = 1,Token line offset = 66,Token in error = JOIN ]
This is My SQL:
var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
"FROM Team" +
"LEFT JOIN Fixtures" +
"ON Team.TeamId=Fixtures.HomeTeam" +
"ORDER BY Team.TeamName";
Team Table Has PK: TeamId
Fixture Table Has FK: HomeTeam
I am using WebMatrix 2. Razor WebPages
No spaces between line concatenations. Change every line to include space at the end.
var query = "SELECT Team.TeamName, Fixtures.HomeTeam " +
"FROM Team " +
"LEFT JOIN Fixtures " +
"ON Team.TeamId=Fixtures.HomeTeam " +
"ORDER BY Team.TeamName";
As pointed by Charles Brentana, you have missed the spaces in your SQL command.
Maybe a better solution is to you use a verbatim string literal, i.e. a string created with an # character before the double-quote character, that can span multiple lines:
var query = #"SELECT Team.TeamName, Fixtures.HomeTeam
FROM Team
LEFT JOIN Fixtures
ON Team.TeamId=Fixtures.HomeTeam
ORDER BY Team.TeamName";
You need spaces between your strings.
I avoid this by putting the space as the first character, so it's really obvious when you forget to code it:
var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
" FROM Team" +
" LEFT JOIN Fixtures" +
" ON Team.TeamId=Fixtures.HomeTeam" +
" ORDER BY Team.TeamName";
If you consistently code this way you'll be able to spot any missing spaces instantly.