update the sql table with some condition [closed] - sql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to update my whole sql table except some rows. Is it possible?
I am trying with this query but not work.
UPDATE `boon_rise`.`users`
SET `zip` = '0'
WHERE `users`.`id`
!IN ('5','6','7')

It seems like you meant to use the not in operator:
UPDATE `boon_rise`.`users`
SET `zip` = '0'
WHERE `users`.`id` NOT IN ('5', '6', '7')
-- Here ------------^

Use NOT IN instead of !IN
UPDATE `boon_rise`.`users`
SET `zip` = '0'
WHERE `users`.`id`
NOT IN('5','6','7')

Yes it is possible :
Update 'boon_rize'.'users'
Set 'zip'='0'
Where 'users'.'id'
Not In ('5','6','7')
Good Luck.

Related

simplifying postgres queries

Note: Asking to improve the working code.
My data column is as follows
{
"color":"red",
"toy":{
"id":27,
"name":"Truck",
"price":12,
"available":true
},
"quantity":"12"
}
In the above data, I want to set available to false and price to zero.
To do this I Am using the below code.
UPDATE toys
SET data=JSONB_SET(data, '{toy,available}','false')
WHERE data->'toy'->>'id'='27';
UPDATE toys
SET data=JSONB_SET(data, '{toy,price}','0')
WHERE data->'toy'->>'id'='27';
My question is it possible to update both values in a single query?
Thanks.
Sure:
UPDATE toys
SET data = jsonb_set(
jsonb_set(data, '{toy,available}', 'false'),
'{toy,price}',
'0'
)
WHERE data->'toy'->>'id'='27';

I get error Conversion failed when converting date and/or time from character string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have search in others topics related and i couldn't get it.
I have this query :
SELECT *
FROM
(SELECT TOP 10
[ID] AS [DOCUMENTID],
REPLACE([ImagePath], '\', '/') AS [FILENAME],
9 AS TEMPLATEID,
NULL AS ENCODEDRESULT,
CASE
WHEN QA.DBValue IS NULL THEN QA.Value
ELSE QA.DBValue
END AS RESULT,
'Finished' AS [STATUS],
QA.X AS [RLEFT],
QA.Y AS [RTOP],
QA.W AS [RWIDTH],
QA.H AS [RHEIGHT],
1 AS [QASTATUS],
QA.[ASDTYPE] AS [ASDTYPE],
QA.[DNNVALUE] AS [DNNRESULT],
QA.[DNNSCORE] AS [DNNSCORE],
CAST(QA.[ITEMDATE] AS DATE) AS [SCANDATE]
FROM
[QA].[QAItems] QA WITH(NOLOCK)
WHERE
QA.[PROCESSORTYPE] = 'F09'
AND (QA.[DBVALUE] IS NOT NULL OR QA.[VALUE] IS NOT NULL)
AND (QA.[DBVALUE] != 'null' OR QA.[VALUE] != 'null')
AND QA.[DBVALUEPROCESSED] = 1) sub
WHERE
CAST(sub.RESULT as date) >= '2019-06-01'
I get an error
Conversion failed when converting date and/or time from character string.
I think is in the cast execution but I can't figure why or how to solve it.
The data type for Result is nvarchar. I think i have done the wrong condition.
I think the problem with the case expression :
CASE WHEN QA.DBValue IS NULL
THEN QA.Value
ELSE QA.DBValue
END AS RESULT
CASE expression would return single type data. So, QA.Value & QA.DBValue should have a same type data.
However, you can simply transit it with COALESCE() :
COALESCE(QA.DBValue, QA.Value)
Try:
where convert(date,sub.RESULT) = convert(date,'2019-06-01')

Can't join and select in Sequel -- PG::SyntaxError [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I'm trying to rename a column at join:
# ...............
result = DB[:my_items1].join(:my_items2, id: :my_item2_id).
select([Sequel[:my_items2][:name].as(:my_items_name),
Sequel[:my_items2][:amount].as(:my_item2_amount)
])
# ...............
Exception:
Sequel::DatabaseError - PG::SyntaxError: ERROR: syntax error at or near "AS"
LINE 1: SELECT ("my_items2"."name" AS "my_item2_name", "...
^
:
Dataset#select takes multiple arguments, not a single array:
result = DB[:my_items1].join(:my_items2, id: :my_item2_id).
select(Sequel[:my_items2][:name].as(:my_items_name),
Sequel[:my_items2][:amount].as(:my_item2_amount)
)

ORDERBY 'Incorrect Syntax' in Declare Statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to do such a simple task, yet it's driving me crazy how it keeps failing.
DECLARE #neighbour HierarchyId = (SELECT [stPath] as tmpPath
FROM [DEV].[tmp].[StrategyTable] t
WHERE ParentCode = 'TOP')
ORDER BY t.stKey DESC;
I keep getting an error saying
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'ORDER'.
At the same time, it works for something WITHOUT a where clause; like
DECLARE #parent HierarchyId = (SELECT
[stPath] AS tmpPath
FROM [DEV].[tmp].[StrategyTable] t WHERE [ParentCode] = 'TOP')
WTF.......
DECLARE #neighbour HierarchyId = (SELECT TOP 1 [stPath] as tmpPath
FROM [DEV].[tmp].[StrategyTable] t
WHERE ParentCode = 'TOP'
ORDER BY t.stKey DESC);
This should accomplish what you want.

I need to remove the first three characters of every field in an SQL column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table called "users" that has a column called "username." Recently, I added the prefix "El_" to every username in the database. Now I wish to delete these first three letters. How can I do that?
assuming MySql you can do something like this.
update users set username=substring(username,4);
which will update every row to not include el_, but this assumes that every row starts with the El_.
sqlfiddle - http://sqlfiddle.com/#!2/3bcf6/1/0
SELECT RIGHT(MyColumn, LEN(MyColumn) - 3) AS MyTrimmedColumn
This removes the first three characters from your result. Using RIGHT needs two arguments: the first one is the column you'd wish to display, the second one is the number of characters counting from the right of your result.
This should do!
EDIT: if you really like to remove this prefix from every username for good use an UPDATE statement as follows:
UPDATE MyTable
SET MyColumn = RIGHT(MyColumn, LEN(MyColumn) - 3)
This should work
mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT username, id FROM users";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_numrows($result);
$i = 0;
while($i < $count){
$username = (mysql_result($result, $i, "username"));
$id = (mysql_result($result, $i, "id"));
$username = substr($username, 3);
$con=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
mysqli_query($con,"UPDATE users SET username = $username WHERE id = $id);
mysqli_close($con);
i++;
}