Update SQL column with the varchar data to nvarchar column data - sql-server-2008

I need to save the double byte word in the nvarchar datatype;
My update query as follows :
UPDATE T
SET
T.[Description]= T1.[Description]
FROM temple T
INNER JOIN temple1 T1 ON T.templeId=T1.templeId
temple1 table columns:
templeid int
description varchar(60)
temple
templeid int
description nvarchar(200)
I have a text some thing like this 'GADELIUS K.K.' in the temple1 table description column I need to update exactly the same in the temple description column
if I use the above update query it's updating like this 'GADELIUS K.K.'
normally for single update I can use N'GADELIUS K.K' it works but I need to dynamically update all the columns.

you can do it something like below it works :
UPDATE T
SET
T.[Description]= N'' + T1.[Description]
FROM temple T
INNER JOIN temple1 T1 ON T.templeId=T1.templeId

Related

how to insert concat command ,and the return result is like select

enter image description here
what command do i have to write to have fullname columns = student_first_name + student_last_name.
i only know select concat command to filter, i want here that it will insert and display as select concat command.
add a new column to table and then use update command to ensure that you fill full name. Using + for string is ill-advised since it is mostly for varchar values not nvarchar values.
query in SQL Server
ALTER TABLE yourtable
ADD FullName VARCHAR(100);
UPDATE yourtable
SET FullName=CONCAT(student_first_name ,student_last_name)
--SET FullName=student_first_name +student_last_name
query in MYSQL
ALTER TABLE yourtable
ADD COLUMN FullName VARCHAR(100) AFTER student_last_name;
UPDATE yourtable
SET FullName = CONCAT(student_first_name ,student_last_name)
--SET FullName=student_first_name +student_last_name

UPDATE statement with a cross join in mysql

I have a table which has a column which contains a text blob. I want to update some of the values in the text fields by using a mapping table, but I'm not sure if there is a way to do it without cursors. Here is an example:
USE Temp;
CREATE TABLE `Temp`.`test_text` (
`some_text` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT=' ';
CREATE TABLE `Temp`.`mapping` (
`src` VARCHAR(1024) NULL,
`dest` VARCHAR(1024) NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT=' ';
-- test_text has a single column with some_text
INSERT INTO `Temp`.`test_text`
(`some_text`)
VALUES
('There once was a man named BobFrank. He changed his name to BobDude.');
-- path_mapping has two columns, which contain all the mappings I'd like to do
INSERT INTO `Temp`.`mapping`
(`src`,
`dest`)
VALUES
('BobFrank', 'BobsNewFrank'),
('BobDude', 'BobsNewDude');
UPDATE `Temp`.`test_text` tt, `Temp`.`mapping` mp
SET tt.some_text = REPLACE(tt.some_text, mp.src, mp.dest);
SELECT *
FROM `Temp`.`test_text`
Result:
There once was a man named BobsNewFrank. He changed his name to BobDude."
The code above only seems to replace BobFrank in the output.
What I would like to see is the value in test_text to be after the update:
There once was a man named BobsNewFrank. He changed his name to BobsNewDude.
Your current table structure is less than ideal, because the test_text table has unnormalized CSV data in it. This will preclude using most of the database operations which we would think to use here. I was able to come up with a solution, but it required storing each CSV term in test_text in a separate row. That is, I used the following table:
CREATE TABLE test_text (some_text varchar(55));
INSERT INTO test_text (some_text)
VALUES
('"/tmp/BobFrank"'),
('"/tmp/BobDude/"');
I left the path_mapping identical to what you had. Then, I only needed a fairly simple query:
SELECT
GROUP_CONCAT(REPLACE(t1.some_text, t2.src, t2.dest)) AS output
FROM test_text t1
INNER JOIN path_mapping t2
ON t1.some_text LIKE CONCAT('%', t2.src, '%');
This generated the following output:
Demo

SQL Server 2008 - Remove characters from multiple rows

I have the below sample data and I would like to remove the word "Division" from the data set.
Table A:
Central Division
North Division
East Division
You may looking for simple update statement
Update T
Set ColumnName=RTRIM(REPLACE(ColumnName,'Division',''))
FROM TableName t
WHERE ColumnName like '%Division%'
You can use this:
Update A
Set ColumnName=Ltrim(REPLACE(ColumnName,'Division',''))
FROM TableName A
WHERE ColumnName like '%Division%'
Or for null :
Update A
Set Column = Null
Where Column Like '%Division%'
Division is the last word on every value
As the word is always at the end and of a fixed length the best way is to simply chop off the last 9 characters:
update t set fld = left(fld, len(fld) - 9)
That do all of the job for you- Removing all ' Division' from sample table.
DECLARE #Table Table
(
nameOfColumn VARCHAR(100)
)
INSERT INTO #Table VALUES
('Central Division'),
('North Division'),
('East Division')
UPDATE #Table
SET nameOfColumn = REPLACE(nameOfColumn,' Division','')
FROM #Table
SELECT * FROM #Table

update data in one table with the values from another table

I have 2 tables, shows and show_photo
shows structure:
show_photo structure:
What i want to do is update show_photo.modified column with show.year
This is what i tried but nothing gets updated.
UPDATE show_photo t2
JOIN shows t1 ON t1.id = t2.show_id
SET t2.modified = t1.year;
Converting my comment to an answer:
Change the column type from TIMESTAMP to VARCHAR first, run the
UPDATE query; and again change the data type from VARCHAR to INT
Your current table has modified column set to TIMESTAMP. But, you're trying to update its values to integers, which will fail.

How to select some default value if there is no such column in a table?

For example, I have a table that doesn't have a column "type". But I need to have my sql query having that column. When I run the query I get an error:
SELECT t.foo, t.boo, t.type FROM tabl AS t;
Unknown column 't.type' in 'field list'
I need something like ternary operator. I tried these solutions but they both do not work:
SELECT f.foo, f.boo, IF(f.type IS NULL, 'x', f.type) AS type FROM tabl AS f
SELECT f.foo, f.boo, (CASE WHEN f.type IS NULL THEN "x" ELSE f.type) AS type FROM tabl AS f
Is there a possibility to implement such a query?
Use something like this. Assume you want to join 2 tables rows and one is missing the column:
SELECT t.foo, t.boo, t.type FROM tabl1 as t1
UNION
SELECT t.foo, t.boo, NULL as type FROM tabl2 AS t2;
You can replace NULL with a string "" or whatever you application desires.
Unfortunately, that is not the way columns work. If you need to introspect your table to determine if it have this column, then you might try using data in the information_schema to get at this. Overall sounds like a weird approach to me. Why not just create all the tables with this column?
I think you mean that you have a table with entries ... Some of your entries has got no "type" column filled in. To have default value, you need to change your table. You can change it using either phpmyadmin (set default value) or through SQL code.
This would be something on these lines:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'London'
)
This sets each entry's city to be London by default