sql query replace all specific values with a different value - sql-server

I am attempting to replace specific values in one column with a new value but it does not work (no errors, no replacement of values, nothing happen).
UPDATE Components
SET Unit='kg'
WHERE Unit='КГ'
How can I replace all values "КГ" with "kg" in column Unit?

I thing that your Unit column is NVarChar() data type. Try following query:
UPDATE Components
SET Unit=N'kg'
WHERE Unit=N'КГ'
Another reason: If you have instead of update trigger on Components table and not update this column on it, your update not affected and no raise error too.

I suggest to use Quotename to resolve this, it is used for this type of string.
UPDATE Components
SET Unit=QUOTENAME('kg')
WHERE Unit=QUOTENAME('КГ')
It is simple and direct query which you run, then its ok. Other wise as #Mehdi said, I also fever that statement.
http://www.c-sharpcorner.com/Blogs/7515/quotename-function-in-sql-server.aspx

Related

Merging two columns into one in sql

I have a table with two columns hora_entrada and hora_saida and when running this select statement:
select hora_entrada, hora_saida
from Controlo_de_Entrada_saidas
it shows this result:
What I want to do is to combine these two columns into one that if Hora_saida = "Não saiu", it shows the data in hora_entrada but if hora_saída has an actual hour in there, it shows the data in hora_saida instead.
So basically this should be my result:
I don't know if I'm making myself clear, I actually don't even know where to start or if its even possible but any help would be appreciated.
Try using a CASE expression:
SELECT
hora_entrada,
hora_saida,
CASE WHEN hora_saida = 'Não saiu'
THEN hora_entrada ELSE hora_saida END AS new_column
FROM yourTable;
This logic assumes that any value in the hora_saida column which is not Não saiu is a valid timestamp. If not, then we could add additional logic to check for this as well.
If you are open/able to changing your data model, you might want to consider just storing NULL values for the missing data. SQL Server (and most other databases as well) has a built-in function COALESCE, which can be used to replace NULL with a backup value. This would avoid needing to use a lengthy CASE expression as you see above.

How to pass result of Conditional Split to variable?

I have a flat file and used conditional split to filter the record into a single row. For example, RecordType == "2" retrieves single row with record having multiple columns say A,B,C,D and E. I want to pass the result of Column C value to a variable. And then to use it to update the table like:
Update tablename
Set A = that variable
Where A is null
Could you please help me in find out the solution.
I would not use the variable but use a Ole DB Command object.
You set the connection.
Then add your SQL from above:
Update tablename Set A = ? Where A is null
The map to Col C.
However, what I might guess you are trying to do is add a column to your other record set that has the detail but no key.
I would use a script component to do this:
Similar to this example:
Importing Grouped Report Data to Database

Insert multiple nodes to xml field in single query

I'm having a table (let's call her t) that contains the fields id(int) and XmlField(xml).
I try to add multiple node in one query but no matter what I tried I keep getting errors.
The query is:
update t
set XmlField.modify('insert <f1>value here</f1><f2>value there</f2> into (/xmldoc)')
and I getting the error:
XQuery [t.XmlField.modify()]: Syntax error near '', expected 'as', 'into', 'before' or 'after'.
When I trying to add only one xml node it's working (example):
update t set XmlField.modify('insert <f1>value here</f1> into (/xmldoc)')
it's also working when I try to add nested nodes like this:
update t set XmlField.modify('insert <f><f1>value here</f1><f2>value there</f2></f> into (/xmldoc)')
Is there any way to make it happen?
The SQL Server documentation does say pretty clearly that the insert statement can handle multiple nodes. So my guess is that your problem is just a syntax error. (The Microsoft syntax varies slightly from that defined in the XQuery Update Facility spec, but it's recognizably similar.)
I'd try making the elements f1 and f2 into a sequence and wrapping them in parentheses (the spec requires an ExprSingle here, which means no top-level commas are allowed):
update t
set XmlField.modify(
'insert (<f1>value here</f1>, <f2>value there</f2>) into (/xmldoc)')
(Not tested against SQL Server.)

literal array in SQL statement?

I'm sure there must be a really easy way to do this, but I can't figure out how, and a search hasn't yielded a solution. Here's what I want to do, but it's illegal:
update test_table set q_name = {'SKELETAL','LEWIS','MECHANISM', ...}[q_type];
That is, q_type has an integer value, and I want to populate q_name with a string value that depends on the value of q_type. I can do it with a separate set statement for every value of q_type, but I'd rather do it with a single call. Any ideas how to do it without getting into PL/SQL?
The way that I would handle that is using the xml datatype and just populate that xml variable and then update the table from that.
here's the reference on the xml data type

Setting the FROM clause via parameter in MyBatis

I haven't been able to see anything in the documentation which speaks to my question, and upon deploying it, my app does not quite work right (more on that in a sec). I am trying to do something like
<select id="getLookupRows" parameterType="map" resultMap="lookupMap">
select id, name, active, valid
from #{table}
</select>
in MyBatis. I have a number of lookup tables that have shared columns and so the user at the view level determines which lookup table is ultimately used. The error I get when I try to execute getLookupRows is
Cause: org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter table of statement info.pureshasta.mapper.LookupMapper.getLookupRows
org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:77)
org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:69)
org.apache.ibatis.binding.MapperMethod.executeForList(MapperMethod.java:85)
org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:65)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:38)
$Proxy15.getLookupRows(Unknown Source)
info.pureshasta.service.FieldTitleService.getLookupRows(FieldTitleService.java:33)
My mapper interface is as follows:
List<Lookup> getLookupRows(#Param("specificColumn") String specificColumn,
#Param("table") String table);
so we know that I am trying to pass a String to this query, nothing special. I have the specific column, because that will be my next task. Really one of the columns of each of the lookup tables is unique, and so I have to call the appropriate specificColumn, but I would be really happy if I could the table parameter and the FROM clause working.
<select id="getLookupRows" parameterType="map" resultMap="lookupMap">
select id, name, active, valid
from ${table}
</select>
does the trick. There is a different notation from actually injecting in a value for the column name and table then say the column value. If you are injecting a value in a where clause, then the # notation is the correct to use.
If the value used for table in this query is not escaped then SQL injection problems can occur. For my use case, the DB preceded me and while I can do whatever I want to the Java and View portions, I am not allowed to alter the fundamental structures of the tables.
If anyone wants to further explain the stack trace I got (i.e. what type myBatis thought table was) I would love to read and be further educated.

Resources