SQL UPDATE with Multiple ANDs - sql

I'm receiving the error
An expression of non-boolean type specified in a context where a condition is expected, near 'location'
when running this query:
UPDATE dbo.table
SET name = 'Matt'
WHERE date = '2013-11-23'
AND time = '12:57'
AND location = 'London'
If I modify the query to remove any one of the ANDs the query works.
Two questions:
Is it not possible to have more than one AND in the WHERE condition for an UPDATE?
How do I structure the query to make it work?
Thanks

It could be the use of reserved words like time and date. Try putting [] around those names and see if it works. And more importantly, you should use better names for your columns instead of those reserved words.

Related

Placing a predefined value in the empty fields of an SQL query result

I need to manipulate the data that a certain SQL query outputs as a result, only by modifying the original query. Since it is s Select-Where-From query, as a novice in SQL I assume I can simply nest it inside another query of this type, resulting in a structure similar to: Select-Where-(Select-Where-From).
The data manipulation simply requires the replacement of all empty fields in a certain column (that was taken from the result of the original query) with a specific predefined value. Here are the two attempts I've made - based on findings from this website - which failed:
select NAME_OF_COLUMN, COALESCE(NULLIF(NAME_OF_COLUMN,''), 'Value_to_insert')
from
(THIS IS WHERE THE ORIGINAL SELECT QUERY GOES)
This one doesn't throw an error, but nonetheless produces empty fields instead of populating them with the value above, as if only the original query was run.
The 2nd:
Select *, NAME_OF_COLUMN=
CASE WHEN NAME_OF_COLUMN IS NULL THEN 'Value_to_insert'
WHEN NAME_OF_COLUMN='' THEN 'Value_to_insert'
ELSE NAME_OF_COLUMN
END
from
(THIS IS WHERE THE ORIGINAL SELECT QUERY GOES)
This one throws the following error (forgive me for the messy presentation, but it was not up to me):
java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
, org.eclipse.birt.report.engine.api.EngineException: Invalid bound column name: CREATOR_USER_NAME., org.eclipse.birt.report.engine.api.EngineException: Cannot get the result set metadata.
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:ORA-00923: FROM keyword not found where expected;
Can you please assist me and tell me what am I doing wrong? Perhaps I need to select a specific column and/or use the 'as' command?
Edit: I have attempted replacing the original select which was:
select table.column as NAME_OF_COLUMN
with this:
select nvl(table.column, 'Value_to_insert') as NAME_OF_COLUMN
Unfortunately, just like the first attempt, the output is identical to the output of the original query..
NAME_OF_COLUMN=CASE ... END is invalid in Oracle. You can't assign a column value in (standard) SQL like that.
If you are trying to come up with a column alias: that needs to go after the expression:
CASE
WHEN name_of_column IS NULL THEN 'Value_to_insert'
WHEN name_of_column = '' THEN 'Value_to_insert'
ELSE name_of_column
END as name_of_column
In Oracle an empty String '' is converted to NULL when you store the value. So the second condition of your CASE expression will never be true. The whole thing can be simplified to:
coalesce(name_of_column, 'Value_to_insert') as name_of_column
Note that you need to get rid of the select * part and explicitly list all other columns excluding name_of_column there, otherwise your query ends up with two columns with the same name.

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

Inserting new rows into table-1 based on constraints defined on table-2 and table-3

I want to append new rows to a table-1 d:\dl based on the equality constraint lower(rdl.subdir) = lower(tr.n1), where rdl and tr would be prospective aliases for f:\rdl and f:\tr tables respectively.
I get a function name is missing ). message when running the following command in VFP9:
INSERT INTO d:\dl SELECT * FROM f:\rdl WHERE (select LOWER(subdir)FROM f:\rdl in (select LOWER(n1) FROM f:\tr))
I am using the in syntax, instead of the alias based equality statement lower(rdl.subdir) = lower(tr.n1) because I do not know where to define aliases within this command.
In general, the best way to get something like this working is to first make the query work and give you the results you want, and then use it in INSERT.
In general, in SQL commands you assign aliases by putting them after the table name, with or without the keyword AS. In this case, you don't need aliases because the ones you want are the same as the table names and that's the default.
If what you're showing is your exact code and you're running it in VFP, the first problem is that you're missing the continuation character between lines.
You're definitely doing too much work, too. Try this:
INSERT INTO d:\dl ;
SELECT * ;
FROM f:\rdl ;
JOIN f:\tr ;
ON LOWER(rdl.subdir) = LOWER(tr.n1)

SQL Server query erroring with 'An object or column name is missing or empty'

I have the following query in a stored procedure in SQL server:
SELECT TLI.LESNumber
,COUNT(TLT.PL)
INTO #PWCM
FROM #tmpLESImport TLI
INNER JOIN tbl_LES L
on TLI.LESNumber=L.NUMB
WHERE ISNULL(L.DELT_FLAG,0)=0
AND L.SCHL_PK=#SCHL_PK
AND TLI.PL IS NOT NULL
AND LEN(TLI.PL)>0
GROUP BY LESNumber
HAVING COUNT(PL)>1
When the query is run I get the following error:
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
Can anyone tell me why? #PWCM does not appear anywhere until this query.
When you SELECT INTO a table, it creates the table (in this case, a temp table). In order to create a table, each column needs a name, which your count column does not. You just need to give it a name:
SELECT TLI.LESNumber,COUNT(TLT.PL) [NumRecords]
INTO #PWCM
FROM #tmpLESImport TLI
...
I had this error for this query
SELECT
CASE WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'A'
WHEN COALESCE([dbo].[my-table].[field],"") = '...' THEN 'B'
...
END AS aaa INTO ##TEMPTABLE
FROM [dbo].[my-table]
Turns out I had to change the "" inside the COALSCE into ''.
Solved it for me
I just came across this, and the core reason was actually related to an errant set of brackets in the code which made the engine think there was a missing alias. Something along the lines of:
select *
from SOME_TABLE
where x = 1
[]
A stringified version of the query included a parameter list for logging, but that was being issued as the query instead of the actual query object. Deleting [] at the end resolved it.

How can I update many rows with SQL in a single table?

I have a table and one of the columns holds web addresses like: 'http://...' or 'https://...'.
The problem is that there are some invalid entries, like 'shttp://...' or '#http//...' (the first character is invalid) and I want to correct all of them.
I use the following SQL statement:
'SELECT [...] FROM MyTable WHERE WebAddress LIKE '_http%'
and I successfuly get the problematic rows.
But how am I going to change/correct all of them using an UPDATE statement?
If you have some other solution please share it!
Simply change the SELECT to an UPDATE (of course, with some syntax changes) with a "fix" expression
UPDATE
MyTable
SET
WebAddress = SUBSTRING(WebAddress, 2, 8000)
WHERE
WebAddress LIKE '_http%'
You Can use Sub string property as you can trim odd letters .Also like '_word start' suitable for your question