Usage of " " inside a concat statement in excel - sql-server

I'm working on data cleansing of a database and I'm currently in the process of changing the upper case names into proper case. Hence, I'm using excel to have an update statement like this:
A | B | C | D |
| 1 | Name | id | Proper case name| SQL Statement |
|-----|------|-----|-----------------|---------------|
| 2 | AAAA | 1 |Aaaa |=CONCAT("UPDATE table SET Name = "'",C2,"'" WHERE id = ",B2,";") |
|-----|------|-----|-----------------|---------------|
| 3 | BBBB | 2 |Bbbb |=CONCAT("UPDATE table SET Name = "'",C3,"'" WHERE id = ",B3,";")|
The SQL state should be something like this:
UPDATE table SET Name = 'Aaaa' WHERE id = 1
UPDATE table SET Name = 'Bbbb' WHERE id = 2
I'm finding it difficult to get apostrophe around the name.

I think you need:
=CONCATENATE("UPDATE table SET Name = '",C2,"' WHERE id = ",B2,";")

Related

Store a list of values as a string when creating a table in snowflake

I am trying to create a table with 5 columns. COLUMN #2 (PROGRESS) is a comma seperated list (i.e 1,2,3,4 etc.) but when trying to create this table as either a string, variant or varchar, Snowflake refuses to allow this. Any advice on how I can create a column seperated list from a CSV? I tried to import the data as a TSV, XML, as well as a JSON file but no success.
create or replace TABLE AD_HOC.TEMP.NEW_DATA (
VISITOR_ID VARCHAR(16777216),
PROGRESS VARCHAR(16777216),
DATE DATETIME,
ROLE VARCHAR(16777216),
FIRST_VISIT DATETIME
)COMMENT='Interaction data'
;
Goal:
VISITOR_ID | PROGRESS | DATE | ROLE | FIRST_VISIT
111 | [1,2,3] | 1/1/2022 | OWNER | 1/1/2021
123 | [1] | 1/2/2022 | ADMIN | 2/2/2021
23321 | [1,2,3,4] | 2/22/2022 | USER | 3/12/2021
I encoded the column in python and loaded the data in Snowflake!
from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()
df = doc_data.join(pd.DataFrame(mlb.fit_transform(doc_data.pop('PROGRESS')),
columns=mlb.classes_,
index=doc_data.index))
df

Hibernate DB Insert but the primary key is an auto increased ID

My table's primary key is an auto increased ID
TranslationID | TranslationKeyID | Language | Text
... | ... | ... | ...
45 | R45_NAME_DE | DE | Text1
46 | R45_INST_EN | EN | Text2
47 | ... | ... | ...
I want to do Inserts into this table using hibernate.
My approach is to get the object from the DB using the TranslationKeyID
And then check wether the object is null or not to see if the translation already exists
HBTranslation hbTlForNameDE = getTranslation(session, "DE", route.getNameKey());
if (hbTlForNameDE == null) {
hbTlForNameDE = new HBTranslation();
//how do I set an automatically ongoing ID?
hbTlForNameDE.setTranslationId(/* ?? */);
}
The id will be set when calling entityManager.persist(...) for that entity. You can read more about this topic here: https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing

How can I write a query that returns two different columns for two different conditions?

I have a Microsoft SQL Server table that has three columns in it - one for Locations, one for the associated Images and one indicating whether a given image is meant be used as the main Image for a given location (this is done because historically multiple images were uploaded for each location and this column indiciated which image is the one that actually gets used).
Now we want to be able pick and assign a second image as a logo for our locations, resulting in a fourth column added to indiciate which image becomes that logo.
So I have a table that looks something like this:
+----------+----------+-----------------+-----------+
| filename | location | IsMainImage | IsLogo |
+----------+----------+-----------------+-----------+
| img1 | 10 | True | Null |
| img2 | 10 | Null | True |
| img3 | 10 | Null | Null |
| img4 | 20 | True | Null |
| img5 | 20 | NULL | True |
+----------+----------+-----------------+-----------+
My goal is to write a query that would return both img1 and img2 as different columns within the same row in my query, followed by img3 and img4 another row. From the table above I need the output to look like this:
+-----------+-------------+
| filename1 | filename2 |
+-----------+-------------+
| img1 | img2 |
| img4 | img5 |
+-----------+-------------+
Please note that my description is an oversimplication. I am modifying and SSIS package that is consumed by another proces that I cannot modify. This is the reason why I need the output in this format.
What became Filename1 and Filename2 used to be the same file (logo was a resized version of the main image) and now I need to differentiate between the two.
It is crucial that only the columns flagged as IsMainImage show under filename1 and only the columns flagged under IsLogo show under filename2.
I would appreciate any help with this. Thank you!
Using Case expression:
SELECT max(CASE WHEN [IsMainImage] = 'True' then filename end) as filename1,
max(CASE WHEN [IsLogo] = 'True' then filename end) as filename2
from Table_testcase group by location;
You can join the table with itself, something like this:
SELECT im1.filename as filename1,im2.filename as filename2
FROM images im1
JOIN images im2
ON im1.location = im2.location
AND im2.IsLogo = 1
WHERE im1.IsMainImage = 1

How can I update many rows with different values for the same column?

I have a table with a column containing a path to a file. The path is an absolute path, and values for this column look like this: C:\CI\Media\animal.jpg.
The table looks like so, except there are many rows so editing by hand is not practical:
`+----+-----------------------------------+
| ID | Path |
+----+-----------------------------------+
| 1 | C:\CI\Media\sushi.jpg |
| 2 | C:\CI\Media\animal.jpg |
| 3 | C:\CI\Media\Tuscany Trip\pisa.png |
+----+-----------------------------------+`
Path is an nvarchar(260)
And what'd I'd like to do is run a query that will update each record so the path for each record replaces C:\CI\ with C:\CI\Net, and end up with a table that looks like so:
`+----+---------------------------------------+
| ID | Path |
+----+---------------------------------------+
| 1 | C:\CI\Net\Media\sushi.jpg |
| 2 | C:\CI\Net\Media\animal.jpg |
| 3 | C:\CI\Net\Media\Tuscany Trip\pisa.png |
+----+---------------------------------------+`
Is there a way to format a query that will update every record, but update it based on the existing value (replace the C:\CI portion with C:\CI\Net for each record while maintaining the rest of the the value) instead of setting each column to the same value like a normal Update table set column = value ?
Gosh you almost wrote the code yourself.
Update YourTable
set path = replace(path, 'C:\CI', 'C:\CI\Net')

How to write additional data to a table when using CakePHP 2.x Tree Behaviour

I'm working with a CakePHP 2.x application and using the Tree Behaviour which comes with it - https://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html
My table is called navigations and has the following schema which is provided in the docs:
mysql> DESCRIBE navigations;
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| parent_id | int(10) | YES | | NULL | |
| lft | int(10) | YES | | NULL | |
| rght | int(10) | YES | | NULL | |
| name | varchar(255) | YES | | | |
+-----------+------------------+------+-----+---------+----------------+
5 rows in set (0.02 sec)
My Model, Navigation.php is configured to use the Tree behaviour:
class Navigation extends AppModel {
public $actsAs = array('Tree');
}
I can write data into my table as per the documentation, e.g.
$data['Navigation']['parent_id'] = 3;
$data['Navigation']['name'] = 'United Kingdom';
$this->Navigation->save($data);
However... I have a requirement to store some additional data in the table.
If I add a field to my table, jstree_data of type VARCHAR(255), I cannot write it to the table with the following:
$data['Navigation']['parent_id'] = 3;
$data['Navigation']['name'] = 'United Kingdom';
$data['Navigation']['jstree_data'] = 'foo'; // Attempt to write to 'jstree_data' field in database
$this->Navigation->save($data);
Does anyone know if it's possible to do this? It seems that by using the Tree Behaviour (public $actAs = array('Tree') in the model) you lose the normal functionality of models and being able to save other data.
Background info:
Why am I trying to do this? I'm using jstree on a project. I need to delete all of the existing records before resaving the tree. However, because the id column is an auto_increment I need to store a reference for the "old ID" in jstree_data because that's what's present in the JSON data that comes from jstree. Based on this I can then look up the "new" ID (the auto increment value) and assign that with $data['Navigation']['parent_id'] when resaving the new child elements.

Resources