+ sign not inserted to Oracle db - sql

I am trying to insert data with the query
UPDATE CONTACTS SET internationalmsisdn = +904562038544 WHERE id = 31328
After executing query, the internationalmsisdn column is shown as 904562038544.
Why do I lost + sign ?
Any idea?

To insert a special character as a string you need to have the column type as varchar and pass the values as ,
UPDATE CONTACTS SET internationalmsisdn = '+904562038544' WHERE id = 31328
Hope this helps !!

Related

SQL Unmatched Data Types

I'm getting an error when trying to increment a row in my db.
Here's the sql query.
var sql = "UPDATE USERS SET Submissions = CAST (Submissions AS VARCHAR(10)) + CAST (1 AS VARCHAR(10)) WHERE Username="+userEmail;
Any help would be great!
If you're incrementing a row, you don't cast it to varchar(10). You keep it as an integer:
var sql =
"UPDATE USERS
SET Submissions += 1
WHERE Username="+userEmail;

concat with left in sql

i have two columens in the same table:
Column A :abcdef
Column B :12345
I want Column A value to be replaced by :
abcdef123
therefore i want all the data from column A plus the 3 first digit from column B.
I am stuck big time. I use Microsft SQL server Mgt Studio.
any help is welcome.
thanks
try this
set ColumnA=(select CONCAT(ColumnA,LEFT(ColumnB,3)))
as simple as this:
update table set ColumnA = ColumnA + LEFT(ColumnB, 3)
Use LEFT string function :
DECLARE #colA VARCHAR(100) = 'abcdef'
DECLARE #colB VARCHAR(100) = '12345'
SELECT #colA + LEFT(#colB,3)
Update Table_name SET ColumnA = ColumnA+LEFT(ColumnB,3)

SQL query with column name obtained from another table SQL Server 2012

trying to run the query
select * from customers, TablesList where TablesList.TableName+'ID' =
10 and tableslist.tableid= 123
where the column name obtained from another table. I get the following error
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting
the nvarchar value 'CustomersID' to data type int.
I know I can do something like Select * from customers where customersID = 10
But trying to create CustomersID column name dynamically from another table. The intent it to have TablesList.TableName+'ID' give me CustomersID string that I can use to equate to 10.
My guess is that the value for Tablelist.TableName is Customer so when you do + 'ID' it results in 'CustomerID'. 'CustomerID' is the VALUE that is returned and not the FIELD NAME that gets compared to 10.
Hence when sqlserver try to convert 'CustomerID' to 10 you get an error message telling you that it's not an integer Value.
As far as I know you cannot get a "field name" from a field value directly trough SQL, for that you'd need to create a stored proc or some kind of programming language to build the query dynamically
TablesList.TableName+'ID' generates the string 'CustomersID'. You get the error because your comparison is actually made like this:
'CustomersID' = 10 -- The comparison NVARCHAR = INT produces the error
What i think you're trying to achieve requires dynamic SQL.
The problem with what you have is that your where clause is checking if the value 'CustomerID' is equal to 10. It isn't (and can't) use that string as a column name in that context. You need to use dynamic sql.
Dynamic SQL is where you build up a string which contains the SQL you ulitmately want to run. So as an example, you could do something like this:
declare #sql varchar(max)
set #sql = 'select * from customers where ' + (select top 1 TableName from TableList where tableId = 123) + 'ID = 10'
EXEC(#sql)
This sets the #sql variable to select * from customers where customerID = 10 then runs that statement.
Use Concat:
select * from customers, TablesList where Concat('TablesList.TableName','ID') =
10 and tableslist.tableid= 123

ORA-01722:Invalid Number for Numeric Datatype

I am trying to execute the query:
UPDATE USER SET ATTEMPTS = ATTEMPTS + 1 WHERE USER_ID = "abc"
here ATTEMPTS is Numeric datatype
But I am receiving the error ORA-01722:Invalid Number
There are probably two things going on. user_id is numeric and in your post, you did not copy paste the offending sql statement, but replaced 'abc' with "abc".
The following snippet reproduces your error, and it is caused not by attempts not being numeric, but rather by user_id being numeric and compared to a string:
create table tq84_n (
attempts number,
user_id number
);
insert into tq84_n values (1, 1);
update tq84_n set attempts = attempts + 1 where user_id = 'abc';
drop table tq84_n purge;

How do I set a column value to NULL in SQL Server Management Studio?

How do I clear the value from a cell and make it NULL?
I think Zack properly answered the question but just to cover all the bases:
Update myTable set MyColumn = NULL
This would set the entire column to null as the Question Title asks.
To set a specific row on a specific column to null use:
Update myTable set MyColumn = NULL where Field = Condition.
This would set a specific cell to null as the inner question asks.
If you've opened a table and you want to clear an existing value to NULL, click on the value, and press Ctrl+0.
If you are using the table interface you can type in NULL (all caps)
otherwise you can run an update statement where you could:
Update table set ColumnName = NULL where [Filter for record here]
Use This:
Update Table Set Column = CAST(NULL As Column Type) where Condition
Like This:
Update News Set Title = CAST(NULL As nvarchar(100)) Where ID = 50
Ctrl+0 or empty the value and hit enter.
Just as a little extension to Jeff Martin's and Zack Peterson's solution.
If you still want to set several values from the columns to null you can simply set the query to
UPDATE myTable SET MyColumn1 = NULL, MyColumn2 = NULL, MyColumn3 = NULL, ...
CTRL+0 doesn't seem to work when connected to an Azure DB.
However, to create an empty string, you can always just hit 'anykey then delete' inside a cell.