SQL update, string to numeric - sql

In an SQL Server database, I have to update some fields, the query is (, is the decimal separator) :
UPDATE MyTbable
SET
Field1 = CAST('111,11' AS DEC(18,4)),
Field2 = CAST('222,22' AS DEC(18,4)),
Field3 = CAST('333,33' AS DEC(18,4))
WHERE Id = '1'
I receive the error :
Error converting data type varchar to numeric. I tried with the cast, same problem
Field1,Field2 and Field3 are numeric 38,2
Any idea ?
Thanks,

Take the commas out.
UPDATE MyTbable
SET
Field1 = CAST(REPLACE('111,11',',','.') AS DEC(18,4)),
Field2 = CAST(REPLACE('222,22',',','.') AS DEC(18,4)),
Field3 = CAST(REPLACE('333,33',',','.') AS DEC(18,4))
WHERE Id = '1'

Use . instead of ,
UPDATE MyTbable
SET
Field1 = CAST('111.11' AS DEC(18,4)),
Field2 = CAST('222.22' AS DEC(18,4)),
Field3 = CAST('333.33' AS DEC(18,4))
WHERE Id = '1'
If you want to select these values afterwards as string and with comma then do that:
select replace(cast(Field1 as varchar(30)), '.',','),
replace(cast(Field2 as varchar(30)), '.',','),
replace(cast(Field2 as varchar(30)), '.',','),
where Id = '1'

remove the commas in you query like :
UPDATE MyTbable
SET
Field1 = CAST('11111' AS DEC(18,4)),
Field2 = CAST('22222' AS DEC(18,4)),
Field3 = CAST('33333' AS DEC(18,4))
WHERE Id = '1'
or you can rmove the comma using replace:
replace('111,11',',','.')

Related

Selecting a specific value within a case statement

I need help with selecting a specific value within a CASE Statement.
For an example
SELECT CASE WHEN Field1 >= (Field2 = 1) THEN Answer
WHEN Field1 < (Field2 = 1) THEN No Answer
ELSE Question END AS Field3
I want to be able to select a specific value in Field2 within the CASE Statement
could be you are looking for
SELECT CASE WHEN Field1 >= Field2 AND Field2 = 1 THEN Answer
WHEN Field1 < Field2 AND Field2 = 1 THEN No Answer
ELSE Question END AS Field3
or more simply
SELECT CASE WHEN Field1 >= 1 THEN Answer
WHEN Field1 < 1 THEN No Answer
ELSE Question END AS Field3
-- 1. You don't specify the fields types.
-- 2. You do a comparison of Field1 with a logical expression (Field2=1). i guess it fails.
-- 3. I will assume Answer, No answer and Question will be the desired string result, and have to be quoted.
-- 4. A Table name is missing. I will assume Field1 and Field2 are fields from MyTable
-- 5. I' am assuming Field1 and Field2 of the same type, and be varchar(10) for example.
DECLARE #Field2 varchar(10) = 1
SELECT
CASE WHEN Field1 >= Field2 THEN 'Answer'
WHEN Field1 < Field2 THEN 'No Answer'
ELSE 'Question'
END AS Field3
FROM MyTable
WHERE Field2 = #Field2

SQL Server : skip update if variable is NOT NULL

I have a problem with a SQL Server database. I want to update the values of an ID only if the value is null, if there is a value it should be skipped -> not updated.
I don't know how to realise this "if else" in SQL Server - could somebody give me a hint?
Thanks in advance.
This is my pseudo code:
UPDATE dbo.mytable
IF FIELD1 IS NOT NULL SKIP --Don't update
ELSE IF FIELD1 IS NULL
SET FIELD1 (SELECT DISTINCT FIELD1
FROM mytable
WHERE {Some ID} = '123'
AND FIELD1 IS NOT NULL)
WHERE {Some ID} = '123'
AND FIELD1 IS NULL
UPDATE t
SET t.FIELD1 = ( SELECT DISTINCT TOP 1 FIELD1
FROM mytable t2
WHERE SomeID = '123'
AND FIELD1 IS NOT NULL )
FROM dbo.mytable t
WHERE FIELD1 IS NULL
AND SomeId = '123'
I think you want something like this:
UPDATE dbo.mytable
SET FIELD1 = ?
WHERE FIELD1 IS NULL;
The ? is a placeholder for the value you want to set it to.
If you want to set the value to the "123" value:
UPDATE dbo.mytable
SET FIELD1 = (SELECT t2.FIELD1 FROM dbo.mytable t2 WHERE t2.ID = 123)
WHERE FIELD1 IS NULL;
You can simply write the below query
UPDATE dbo.mytable
SET ID = 123
WHERE ID IS NULL
Hope this will help you. It will only update the ID when the value in ID field is NULL.
UPDATE dbo.mytable
SET FIELD1 = (SELECT DISTINCT FIELD1
FROM mytable
WHERE {Some ID} = '123'
AND FIELD1 IS NOT NULL)
WHERE {Some ID} = '123'
AND FIELD1 IS NULL --here your IF condition will get filter [[IF FIELD1 IS NOT NULL SKIP --Don't update]]

DRY for field formula in Oracle SQL

We have some SQL in Oracle which looks like this:
SELECT
CASE
WHEN
field1 - field2 + field3 < 0
THEN
0
ELSE
field1 - field2 + field3
END
we'd like to specify field1 - field2 + field3 once, whilst not altering the output of the SELECT query. Is there any neat way to do this?
As an alternative, if there is a function which can return a numeric value above 0 and returns 0 for negative values that would work as well.
Use greatest():
select greatest(field1 - field2 + field3, 0)
Something like this. If you wrap it in a stored procedure you can return the result.
DECLARE
n number := field1 - field2 + field3;
BEGIN
CASE
WHEN
n < 0
THEN
0
ELSE
n
END CASE
END;
Try:
SELECT
CASE
WHEN
expr < 0
THEN
0
ELSE
expr
END
FROM (SELECT field1 - field2 + field3 as expr
FROM ... );

How can I make this pl/sql cursor more efficient?

Hi guys I have a pl/sql cursor that takes too long to execute. I want to know how can I make the same process but with better performance and probably better code. I am new to PL/SQL.
Declare
Cursor Cursor1 is
select * from table1 where
field1 IS NULL
or
field2 IS NULL or field3 IS NULL or field4 is null or field5 IS NULL or field6 IS NULL;
Begin
For i in Cursor1 loop
if i.field1 IS NULL then
update table1 set field1=0 where recordId=i.recordId;
end if;
if i.field2 IS NULL then
update table1 set field2=0 where recordId=i.recordId;
end if;
if i.field3 IS NULL then
update table1 set field3=0 where recordId=i.recordId;
end if;
if i.field4 IS NULL then
update table1 set field4=0 where recordId=i.recordId;
end if;
if i.field5 IS NULL then
update table1 set field5=0 where recordId=i.recordId;
end if;
if i.field6 IS NULL then
update table1 set field6=0 where recordId=i.recordId;
end if;
End loop;
End;
The question basically is how can I update a field of one specific record, taking into account the conditions of the field. The thing is that the update can occur in the same record many times if the condition apply for many fields in the record.
Thanks...
It's possible to do the same with one UPDATE
UPDATE table1 SET
field1 = COALESCE(field1, 0)
, field2 = COALESCE(field2, 0)
, field3 = COALESCE(field3, 0)
, field4 = COALESCE(field4, 0)
, field5 = COALESCE(field5, 0)
, field6 = COALESCE(field6, 0)
WHERE field1 IS NULL OR field2 IS NULL OR field3 IS NULL
OR field4 IS NULL OR field5 IS NULL OR field6 IS NULL
Here's another take on this:
UPDATE TABLE1
SET FIELD1 = NVL(FIELD1, 0),
FIELD2 = NVL(FIELD2, 0),
FIELD3 = NVL(FIELD3, 0),
FIELD4 = NVL(FIELD4, 0),
FIELD5 = NVL(FIELD5, 0),
FIELD6 = NVL(FIELD6, 0);
Rationale: any query which performs this update is going to do a full table scan anyways because it's looking for NULLs, which won't be indexed in the usual case, and even if they ARE indexed there's a fair chance the optimizer will choose a full table scan anyways. Why waste time checking six different fields for NULLs?
Share and enjoy.
Try executing couple of updates like this, avoiding the usage of a cursor:
update table1 set field1=0 where field1 IS NULL;
update table1 set field2=0 where field2 IS NULL;
update table1 set field3=0 where field3 IS NULL;
update table1 set field4=0 where field4 IS NULL;
update table1 set field5=0 where field5 IS NULL;
update table1 set field6=0 where field6 IS NULL;
I don't think that there is a more efficient way to do this.

Conditional statement in WHERE clause

I need to write a conditional statement in my where clause that uses different operators based on the parameter passed into the procedure. I can't seem to find the syntax that will work.
My example is as follows:
#DateValue datetime
select *
from table
where field1 = 'x'
and field2 = 'y'
and if #DateValue = '1/1/1900' then
field3 <= getdate()
else
field3 = #DateValue
end
Thanks for everyone's assistance.
and ((#DateValue = '1/1/1900' and field3 <= getdate()) or
(#DateValue <> '1/1/1900' and field3=#DateValue))
I think you would need something like (psuedo-code)
Create PROCEDURE GetPartialTable
#DateValue datetime
AS
Begin
IF (#DateValue = '1900-01-01')
select * from table where field1 = 'x' and field2 = 'y' and field3 <= getdate();
else
select * from table where field1 = 'x' and field2 = 'y' and field3 = #DateValue;
END IF
END
It sounds like the solution above, but the results might be seriously different if the context is more general, e.g. if the entire SQL statement is completely different.