March 15, 2017 at 11:39 am
I have a data table called 'ItemNum'
The column is a SKU, which is all numbers...the current format is: 028200136107
I need to remove all leading zeros (0) from the first digit, but I need SQL to leave the number alone IF it does not lead with a zero.
Something like this?
UPDATE Inventory
SET replace(ltrim(replace(ItemNum.Inventory, '0', '')), '', '0')
where ItemNum = LEN(ItemNum) = 12
Unfortunly this does not work 🙁
Thanks
March 15, 2017 at 11:47 am
chef423 - Wednesday, March 15, 2017 11:39 AMI have a data table called 'ItemNum'The column is a SKU, which is all numbers...the current format is: 028200136107
I need to remove all leading zeros (0) from the first digit, but I need SQL to leave the number alone IF it does not lead with a zero.
Something like this?
REPLACE(LTRIM(REPLACE(str_col, '0', ' ')), ' ', '0')
UPDATE Inventory
SET ItemNum
WHERE ItemNum LEN(ItemNum) = 12
Here's one way – not fantastically fast, because of the data type conversions.
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50));
WHERE ItemNum like '0%';
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
March 15, 2017 at 11:53 am
Phil Parkin - Wednesday, March 15, 2017 11:47 AMchef423 - Wednesday, March 15, 2017 11:39 AMI have a data table called 'ItemNum'The column is a SKU, which is all numbers...the current format is: 028200136107
I need to remove all leading zeros (0) from the first digit, but I need SQL to leave the number alone IF it does not lead with a zero.
Something like this?
REPLACE(LTRIM(REPLACE(str_col, '0', ' ')), ' ', '0')
UPDATE Inventory
SET ItemNum
WHERE ItemNum LEN(ItemNum) = 12Here's one way – not fantastically fast, because of the data type conversions.
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50));
WHERE ItemNum like '0%';
Hmmm
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE ItemNum like '0%'
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to bigint.
March 15, 2017 at 11:55 am
Sorry, I left out that some of the ItemNum SKU's do have letters in the ItemNum field....but I want to ignore any of those ItemNum fields...ONLY the ones that have leading zeros...
so check all ItemNum and only drop leading 0 if it begins with a leading 0
Tried this:
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE LEN(ItemNum) = 12
March 15, 2017 at 11:56 am
chef423 - Wednesday, March 15, 2017 11:53 AMHmmm
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE ItemNum like '0%'Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to bigint.
Sounds like you have some ItemNum's that aren't Numbers. Could this be true
If not, have a look at how many rows this returns.SELECT ItemNum
FROM Inventory
WHERE TRY_CAST(ItemNum AS BIGINT) IS NULL;
Edit, nevermind,OP posted again further.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
March 15, 2017 at 12:02 pm
Thom A - Wednesday, March 15, 2017 11:56 AMchef423 - Wednesday, March 15, 2017 11:53 AMHmmm
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE ItemNum like '0%'Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to bigint.Sounds like you have some ItemNum's that aren't Numbers. Could this be true
If not, have a look at how many rows this returns.
SELECT ItemNum
FROM Inventory
WHERE TRY_CAST(ItemNum AS BIGINT) IS NULL;Edit, nevermind,OP posted again further.
Yes, 450 rows return. Sorry, I do have some ItemNum fields that are not numbers, but I want my query to ignore those.
I thought this may work, but it did not:
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE LEN(ItemNum) = 12
Any ideas?
March 15, 2017 at 12:02 pm
Borrowing Thom's code a little, try adding
AND TRY_CAST(ItemNum AS BIGINT) IS NOT NULL
to the WHERE clause of the code I posted earlier.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
March 15, 2017 at 12:04 pm
No luck with this:
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE LEN(ItemNum) = 12 and ItemNum like '0%'
March 15, 2017 at 12:08 pm
chef423 - Wednesday, March 15, 2017 12:04 PMNo luck with this:
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE LEN(ItemNum) = 12 and ItemNum like '0%'
Why are you persisting with that LEN test? It's not helping you weed out the non-numeric items.
UPDATE Inventory
SET ItemNum = CAST(CAST(ItemNum AS BIGINT) AS VARCHAR(50))
WHERE ItemNum like '0%' AND TRY_CAST(ItemNum AS BIGINT) IS NOT NULL;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
March 15, 2017 at 12:10 pm
Here's an alternative version because it seems that your numbers are not digit-only.UPDATE Inventory
SET ItemNum = SUBSTRING(ItemNum, PATINDEX('%[^0]%',ItemNum), 4000)
WHERE ItemNum like '0%'
March 15, 2017 at 12:12 pm
Luis Cazares - Wednesday, March 15, 2017 12:10 PMHere's an alternative version because it seems that your numbers are not digit-only.UPDATE Inventory
SET ItemNum = SUBSTRING(ItemNum, PATINDEX('%[^0]%',ItemNum), 4000)
WHERE ItemNum like '0%'
Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'pkInventory'. Cannot insert duplicate key in object 'dbo.Inventory'. The duplicate key value is (1001, 12300118835).
How could this be a violation??
March 15, 2017 at 12:29 pm
chef423 - Wednesday, March 15, 2017 12:12 PMLuis Cazares - Wednesday, March 15, 2017 12:10 PMHere's an alternative version because it seems that your numbers are not digit-only.UPDATE Inventory
SET ItemNum = SUBSTRING(ItemNum, PATINDEX('%[^0]%',ItemNum), 4000)
WHERE ItemNum like '0%'Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'pkInventory'. Cannot insert duplicate key in object 'dbo.Inventory'. The duplicate key value is (1001, 12300118835).How could this be a violation??
I understand why, when its dropping the zero, there is a duplicate item number...
How can I ignore any of the items that may be a duplicate?
March 15, 2017 at 12:38 pm
chef423 - Wednesday, March 15, 2017 12:29 PMchef423 - Wednesday, March 15, 2017 12:12 PMLuis Cazares - Wednesday, March 15, 2017 12:10 PMHere's an alternative version because it seems that your numbers are not digit-only.UPDATE Inventory
SET ItemNum = SUBSTRING(ItemNum, PATINDEX('%[^0]%',ItemNum), 4000)
WHERE ItemNum like '0%'Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'pkInventory'. Cannot insert duplicate key in object 'dbo.Inventory'. The duplicate key value is (1001, 12300118835).How could this be a violation??
I understand why, when its dropping the zero, there is a duplicate item number...
How can I ignore any of the items that may be a duplicate?
Note that this will make the update slower.UPDATE i
SET ItemNum = SUBSTRING( i.ItemNum, PATINDEX('%[^0]%', i.ItemNum), 4000)
FROM Inventory i
WHERE i.ItemNum like '0%'
AND SUBSTRING(i.ItemNum, PATINDEX('%[^0]%',i.ItemNum), 4000) NOT IN (SELECT x.ItemNum FROM Inventory x)
March 15, 2017 at 1:01 pm
or another alternative to Luis code - wonder which one will run faster
UPDATE Inventory
SET ItemNum = stuff(ItemNum, 1, patindex('%[^0]%', ItemNum) - 1, '')
WHERE ItemNum like '0%'
and not exists (select 1 from Inventory i2 where ItemNum = stuff(ItemNum, 1, patindex('%[^0]%', ItemNum) - 1, ''))
March 15, 2017 at 1:17 pm
frederico_fonseca - Wednesday, March 15, 2017 1:01 PMor another alternative to Luis code - wonder which one will run faster
UPDATE Inventory
SET ItemNum = stuff(ItemNum, 1, patindex('%[^0]%', ItemNum) - 1, '')
WHERE ItemNum like '0%'
and not exists (select 1 from Inventory i2 where ItemNum = stuff(ItemNum, 1, patindex('%[^0]%', ItemNum) - 1, ''))
How does this avoid touching non-numeric ItemNum values?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply