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 2005 Forums
 Transact-SQL (2005)
 UPDATE in bulk

Author  Topic 

jszulc
Yak Posting Veteran

66 Posts

Posted - 2010-05-04 : 15:44:33
How do I write UPDATE statement to update all PropertyID records that are displaying less than 6 characters to 6 characters. Ex I have 1000 records that are showing as 92317. I need this record to be 092317. I have 200 records that are showing as 672. I would need to flip them to 000672. The problem is I have thousands of them so I cannot do them individually. Is tehre a way to do this in bulk with 1 statemnet on the entire column?
CREATE TABLE [dbo].[KPITbl](
[TaskID] [int] IDENTITY(1,1) NOT NULL,
[Lead] [nvarchar](255) NULL,
[WRM] [nvarchar](255) NULL,
[PropertyIDStartLoc] [nvarchar](250) NULL

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-04 : 15:49:42
UPDATE KPITbl
SET PropertyIDStartLoc = RIGHT('000000' + PropertyIDStartLoc, 6)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jszulc
Yak Posting Veteran

66 Posts

Posted - 2010-05-04 : 16:23:02
(42579 row(s) affected)

however did not work..... Any ideas?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-04 : 16:28:25
Need to add a where clause: WHERE DATALENGTH(PropertyIDStartLoc) < 12 -- 6*2 since using nvarchar

Here's my test:

CREATE TABLE #KPITbl (PropertyIDStartLoc nvarchar(50))

INSERT INTO #KPITbl VALUES ('1234')
INSERT INTO #KPITbl VALUES ('12345')
INSERT INTO #KPITbl VALUES ('123')
INSERT INTO #KPITbl VALUES ('12')
INSERT INTO #KPITbl VALUES ('1234567')
INSERT INTO #KPITbl VALUES ('123456')

SELECT * FROM #KPITbl

UPDATE #KPITbl
SET PropertyIDStartLoc = RIGHT('000000' + PropertyIDStartLoc, 6)
WHERE DATALENGTH(PropertyIDStartLoc) < 12 -- 6*2 since using nvarchar

SELECT * FROM #KPITbl

DROP TABLE #KPITbl

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jszulc
Yak Posting Veteran

66 Posts

Posted - 2010-05-04 : 16:52:37
still no luck
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-04 : 17:03:58
As illustrated with sample data, Tara's solution should give your desired results.
Can you please show some sample data from your table and the exact query you tried.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-04 : 17:06:21
quote:
Originally posted by jszulc

still no luck



Prove it. I proved mine with the code I posted. Run it to see. It uses a temp table, so it won't impact your tables.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2010-05-05 : 01:59:58
Trigger ,do you have a trigger on this table?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-07 : 07:22:47
Did you run it on a character column?
Otherwise you need to convert it to varchar

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-05-07 : 07:37:19
quote:
Originally posted by tkizer

Need to add a where clause: WHERE DATALENGTH(PropertyIDStartLoc) < 12 -- 6*2 since using nvarchar

This is VERY important, since without it, the value 1234567 will be updated to 234567

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-07 : 12:56:21
Yep, that's why I posted again with it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -