Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update query not working when compared with

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2014-01-13 : 19:31:53

Based on column rmtitle and bldg_title doing updates.

update is not working. when "rmtitle" and "bldg_Title" columns they both have text data like below with slashes that is being compared with in update query,update query getting skipped. only for those rows which has slashes it seems like
'OPERACIONES Y PAGOS 08/02/2010 30/12/2010'


my update query is below:
update tab_ccsnetrm set [building]=t2.Edificio, [bin] = t2.caja
from table_rm t1 inner join tab_projects proj
on(t1.projid = proj.projid)
inner join Bodega_small t2 on(ltrim(rtrim(proj.projno)) = ltrim(rtrim(t2.clientid))) and ltrim(rtrim(t1.rmtitle))=ltrim(rtrim(t2.bldg_Title))

Thank you very much for the helpful info.

nagino
Yak Posting Veteran

75 Posts

Posted - 2014-01-13 : 21:46:22
I try following #queries, I get following #results.
It seems to be worked.
Perhaps, there is not enough information.
You can show small reproducibility set, tables schema, datas, or difference between your environment and following #queries.


--#results
SELECT 1 is
projeid building bin
1 NOT CHANGE-1 NOT CHANGE-1
2 NOT CHANGE-2 NOT CHANGE-2

SELECT 2 is
projeid building bin
1 CHANGED-1 CHANGED-1
2 CHANGED-2 CHANGED-2

---------------------------------------------------------
--#queries

CREATE TABLE tab_ccsnetrm(
projeid int NOT NULL,
building varchar(100) NOT NULL,
bin varchar(100) NOT NULL
)

CREATE TABLE table_rm(
projid int NOT NULL,
rmtitle varchar(100) NOT NULL
)

CREATE TABLE tab_projects(
projid int NOT NULL,
projno int NOT NULL
)

CREATE TABLE Bodega_small(
clientid int NOT NULL,
bldg_Title varchar(100) NOT NULL,
Edificio varchar(100) NOT NULL,
caja varchar(100) NOT NULL
)

INSERT INTO tab_ccsnetrm VALUES
(1, 'NOT CHANGE-1', 'NOT CHANGE-1'),
(2, 'NOT CHANGE-2', 'NOT CHANGE-2')

INSERT INTO table_rm VALUES
(1, 'OPERACIONES Y PAGOS 08/02/2010 30/12/2010'),
(2, 'NO SLASHES CONTAINS')

INSERT INTO tab_projects VALUES
(1, 1),
(2, 2)

INSERT INTO Bodega_small VALUES
(1 ,'OPERACIONES Y PAGOS 08/02/2010 30/12/2010', 'CHANGED-1', 'CHANGED-1'),
(2, 'NO SLASHES CONTAINS', 'CHANGED-2', 'CHANGED-2')

--SELECT 1
SELECT * FROM tab_ccsnetrm

update tab_ccsnetrm set
[building] = t2.Edificio,
[bin] = t2.caja
from table_rm t1
inner join tab_projects proj
on(t1.projid = proj.projid)
inner join Bodega_small t2
on ltrim(rtrim(proj.projno)) = ltrim(rtrim(t2.clientid))
and ltrim(rtrim(t1.rmtitle)) = ltrim(rtrim(t2.bldg_Title))

--SELECT 2
SELECT * FROM tab_ccsnetrm

DROP TABLE tab_ccsnetrm
DROP TABLE table_rm
DROP TABLE tab_projects
DROP TABLE Bodega_small


-------------------------------------
From Japan
Sorry, my English ability is limited.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-14 : 04:30:01
quote:
Originally posted by cplusplus


Based on column rmtitle and bldg_title doing updates.

update is not working. when "rmtitle" and "bldg_Title" columns they both have text data like below with slashes that is being compared with in update query,update query getting skipped. only for those rows which has slashes it seems like
'OPERACIONES Y PAGOS 08/02/2010 30/12/2010'


my update query is below:
update tab_ccsnetrm set [building]=t2.Edificio, [bin] = t2.caja
from table_rm t1 inner join tab_projects proj
on(t1.projid = proj.projid)
inner join Bodega_small t2 on(ltrim(rtrim(proj.projno)) = ltrim(rtrim(t2.clientid))) and ltrim(rtrim(t1.rmtitle))=ltrim(rtrim(t2.bldg_Title))

Thank you very much for the helpful info.



Where are you trying to do this? Are you trying to dynamically generate query? If yes, show us actual code forming dynamic string.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2014-01-14 : 06:19:18
Hi Visakh,

I am not running no dynamic queries, straight forward update query running, msg also appears as if 5400 rows updated.

But for some rows almost 900 rows updates didn't happen.

even though there is matching projid and rmtitle.

projid is INT
rmtitle is text column for that reason using trim function.

I loaded this data from excel file to sql server and then trying to update rows into tab_rm table.

Lot of titles has dates in it this way('OPERACIONES Y PAGOS 08/02/2010 30/12/2010') along with text, are these dates causing any issue.

thank you very much.





Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-14 : 07:23:03
your update condition is based on ltrim rtrim functions. Are you sure its spaces only and not any unprintable characters?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2014-01-14 : 11:17:06
Hi Visakh,
Please need your advice, even with this query if not exists is showing 876 rows. Many rm.rmtitle's do exist with the projid.
same thing i am using my doubt is in rmtitle it is not macthing. to be safe i am using ltrim rtrim to remove spaces on both ends.

Also i tried with Like operator '% on both ends. that time it was able to pick few rows.



select *
from table_bodega bldg
inner join tab_ccsnetprojects proj on ltrim(bldg.clientid = proj.projno
where not exists
(
select *
from tab_ccsnetrm rm
where rm.projid = proj.projid
and rm.rmtitle = bldg.titulocarpeta
)
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-01-14 : 12:55:31
Post sample data in a consumable format that demonstrates the issue. Without that we are just guessing.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-15 : 07:35:11
quote:
Originally posted by cplusplus

Hi Visakh,
Please need your advice, even with this query if not exists is showing 876 rows. Many rm.rmtitle's do exist with the projid.
same thing i am using my doubt is in rmtitle it is not macthing. to be safe i am using ltrim rtrim to remove spaces on both ends.

Also i tried with Like operator '% on both ends. that time it was able to pick few rows.



select *
from table_bodega bldg
inner join tab_ccsnetprojects proj on ltrim(bldg.clientid = proj.projno
where not exists
(
select *
from tab_ccsnetrm rm
where rm.projid = proj.projid
and rm.rmtitle = bldg.titulocarpeta
)


then its obvious
you've some unprintable characters or space characters coming in field which you need remove before doing match

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -