This question already has answers here:
What does the colon sign ":" do in a SQL query?
(8 answers)
Closed 10 months ago.
likely a very simple SQL (oracle) question for you. See here a simplified example of my problem:
DELETE FROM schema.tablename
WHERE
col1 = :v0
AND col2 = :v1
AND col3 = :v2
;
I don't know what :vo...:vo3 means and I have no idea what to google. Can you either explain it or give me some good literature on that ?
it's called bind-variable , just think that those start with colon(:) is different variable for binding value used in sql command.
Try imagine the use case of it.
"I want to delete records from table schema.tablename by using where clause so I will remove those record that have col1 = value1 and col2 = value2 and col3 = value3 "
Well ok then,how I write those above sql statement by not fixing the value of value1,value2,value3 ?
the answer is using variable for binding the value!
:v0 - the variable that has name v0
:v1 - the variable that has name v1
:v2 - the variable that has name v2
ref.
https://www.oracletutorial.com/python-oracle/bind-variables/
Related
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 7 months ago.
Improve this question
I have a column with a varchar(255) format.
The column has number values and NULL values.
I need to convert the column to an integer format, but it fails because of the NULL values.
I have looked it up online and this issue is stated many places. But I can't seem to find the right solution. The solution has to work in a Microsoft SQL server enviroment.
I have tried this:
CAST(varcharname AS INT) AS varname
CAST(CASE WHEN varcharname IS NOT NULL THEN varcharname ELSE NULL END AS INT) AS varname
CAST(NULLIF(varcharname , '') AS INT) AS varname
CAST(NULLIF(varcharname , NULL) AS INT) AS varname
The problem is not the null values, and I can prove it:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c6734d0967eae022b0af2ee3a2b694db
More likely there is something else in the column that fails the conversion. Perhaps some whitespace? Remember that an empty string is not the same as NULL, and a string with only whitespace is not the same as an empty string.
Try this:
cast(nullif(rtrim(varcharname),'') as int)
One other thing to consider is if there is more data in the column and you are restricting results with a WHERE clause.
For example, let's say you have this data:
id
varcharname
1
'1'
2
NULL
3
'3'
4
'4'
5
'Five'
You may have a query with WHERE clause condition like this:
SELECT cast(varcharname as int) WHERE id <> 5
hoping to get results like this:
~
1
NULL
3
4
This solution will often (not always!) still fail, because the database may decide it's more efficient to apply the cast before the WHERE clause.
In this case (or if the earlier solution don't work) you can use TRY_CAST().
However, rather than use TRY_CAST() to merely smooth over your errors, first look to find out which rows/values are causing the errors (so you can fix them, and also fix whatever upstream process saved the bad data):
SELECT [id], varcharname
FROM [MyTable]
WHERE TRY_CAST(varcharname as int) IS NULL and varcharname IS NOT NULL
Finally, remember it's very poor practice to use varchar columns to store number (or date) data in the first place, to the point I consider such schemas to be broken.
I have an table on SQL Server which has two columns like below.
OldValue
NewValue
ReqNo: 123456789
ReqNo: 89898989
RandomGibberish: , ReqNo:
RandomGibberish: , ReqNo: 12121212
I want to be able to filter the table if both the columns OldValue and NewValue start with "ReqNo: " followed by an 8 digit number and are an exact match to this pattern.
This would mean that the first row would be in my output but not the second row.
I know how to do this in python but still new to SQL Server and can't seem to find the right syntax.
Please help.
You could use SQL Server's enhanced LIKE operator here:
SELECT *
FROM yourTable
WHERE OldValue LIKE 'ReqNo: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' AND
NewValue LIKE 'ReqNo: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]';
I have a Question table. It has a dummy question in it , i want to get the question in random but that dummmy question should always be on top in the returned ActiveRecord collection. I can get the records in random buy using Random() function which is working fine but that dummy question has also random position which i dont want, i want it to be on first position. Any solution would be helpful.
Question table columns: id, statement, answer
The dummy question has blank statement.
Query i am using
Question.all.order("Random()")
I am using PostgreSQL.
First option:
select_clause = "(CASE WHEN (statement = '') THEN 0 ELSE Random() END) as dummy, questions.*"
Question
.select(select_clause)
.order('dummy')
Second option:
select_clause = "(statement <> '') as dummy, questions.*"
Question
.select(select_clause)
.order('dummy, Random()')
Edit: statement shouldn't contain NULL in both options
I just spent the last hour trying to figure out why my SQLite database wasnt updating.
The query being done was
UPDATE Table SET col1 = val AND col2 = val2 AND col3 = val3 WHERE col1 = val4
Obviously this is the wrong syntax for an update query, however no exceptions were thrown, no errors shown and when run directly using SQLiteBrowser it even said that the query executed correctly and had updated a row (yet no update was done)
Can anyone explain why this query wasn't rejected with an exception or syntax error?
Update:
So after looking at things further, I can see that an update was indeed performed, however instead of setting col1 to the new value, it was instead set to 0 which seems odd.
The AND operator in most languages, including SQLite, can be used in expressions (for boolean or bitwise operations, etc.), and the = operators is testing for equality, so you're just setting col1 equal to the value of the expression val AND col2 = val2 AND col3 = val3.
For example, if all the columns and values are 1 in your expression, you're setting col1 to:
1 AND 1 = 1 AND 1 = 1
...which is a valid expression that evaluates to 1.
SQLite will report as many rows as matched by the WHERE clause to have been affected by the update, even if the new value in col1 was the same as the old value, i.e. even if the data isn't actually changing. It still found rows to update and put a new value in there, so it reports having successfully performed an update.
We'd need to know the types/values of the columns to work out exactly what's going on, but I don't see a syntax error there, though it's clear you didn't mean to do what the expression (validly!) says!
I've got a small issue with regex in oracle.
I have a table as follows with 3 cases of string formats:
In my table, col1 holds the Strings I have, col2 is target.
Case 1: W1234W4321
Case 2: W1234,W4321
Case 3: W1234/W4321
(Length and and actual numbers vary)
Now I've set up this little regex: [\d,/]W.* to separate the two values after decimal, comma and slash.
I've tested the result in the tool RegExBuddy where he result is as expected.
When updating my table with following query, cases 2 and 3 are being updated, case 1 is still null in col 2.
update nyTable set col2 = regexp_substr(col1, '[\d,/]W.*');
Is this some issue related to oracle (maybe not understanding the \d)?
http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
\d: A digit character. It is equivalent to the POSIX class [[:digit:]].