I need to update a column in SQL to add a leading zero. Problem is this column has data in it in several formats.
0047
0070-0336
0025-0145
0032-0008
0033-0196 Johnson EEOC Matter
I need to add a zero before the last 4 digits, or rather just after the dash, but to only the rows that have the 4 digit - 4 digit format (and 4 -4 plus alpha). So it would look like:
0047
0070-00336
0025-00145
0032-00008
0033-00196 Johnson EEOC Matter
I have tried several UPDATE and RIGHT statements but cannot get the result I need.
what about REPLACE
SELECT REPLACE(YourField, '-', '-0');
FROM YourTable
This will replace the first occurrence of -
SELECT STUFF(YourField, CHARINDEX('-', YourField), 1, '-0')
But will fail if you have this case
0476 jhonso-jhonson
Not tested. Please test it before execution on your real data.
UPDATE yourTable
SET col = SUBSTRING(col, 0, CHARINDEX('-', col))
+ '-0' +
SUBSTRING(col, CHARINDEX('-', col) + 1, LEN(col) - CHARINDEX('-', col))
WHERE col LIKE N'____-____%'
This filters only rows with 4 characters, followed by -, followed by 4 characters, followed by anything. It sets the column in the following way: put the character from 0 till the first - as they are, append -0 (instead of -), then append the rest of column value as they are.
You can use the following expression:
SELECT IIF(CHARINDEX('-', #COL) <> 0,
STUFF(#COL, CHARINDEX('-', #COL), 1, '-0'),
#COL)
In case of a 4 digit - 4 digit, or 4 -4 plus alpha format, this will replace the first '-' with a '-0'.
Related
I am trying to search this string(TenderString1) for SPT. This just outputs the number of characters - so if there were 1 SPT in this string it would return 3 (number of characters). So I am attempting to divide the result by the length of SPT (3) to get the number of occurrences of SPT in the string but it is not returning the correct number
(LEN(TenderString1) - LEN(REPLACE(TenderString1, 'SPT', '')) / len('SPT'))Test
TenderString1 Column:
UAD,MTY,CL,CLTG,OFD,OFD,SPT,SL
Answer should be: 1
If you want the number of times a substring appears in a string when you have something delimited like you do, something like this would do it:
SELECT COUNT(*) FROM STRING_SPLIT(',UAD,MTY,CL,CLTG,OFD,OFD,SPT,SL,SPT,SPT', ',') WHERE value='SPT'
This one returns a 3.
And here's it is with some data:
SELECT
VAL, (SELECT COUNT(*) FROM STRING_SPLIT(VAL, ',') WHERE value='SPT') CNT
FROM
(
SELECT ',UAD,MTY,CL,CLTG,OFD,OFD,SPT,SL,SPT,SPT' AS VAL
UNION
SELECT ',UAD,MTY,CL,CLTG,OFD,OFD,SPT,SL,SPT,SPT,SPT' AS VAL
) A
The division is of higher precedence than subtraction, your code executes LEN(REPLACE(TenderString1, 'SPT', '')) / len('SPT') first and then the result is subtracted from LEN(TenderString1). You could use parenthesis to perform the subtraction first and then divide the result by len('STR')..pay attention that LEN() ignores trailing spaces, so better to prevent wrong calculations (when the searched term is at the end of the string/value) by appending another character at the end of the column value/string.
select TenderString1,
(LEN(TenderString1) - LEN(REPLACE(TenderString1, 'SPT', '')) / len('SPT')) as Test,
(LEN(TenderString1) - LEN(REPLACE(TenderString1, 'SPT', ''))) / len('SPT') as Test1,
(LEN(TenderString1+'.') - LEN(REPLACE(TenderString1+'.', 'SPT', ''))) / len('SPT') as SPToccurence
from
(values
(',UAD,MTY,CL,CLTG,OFD,OFD,SPT,SL'),
(',UAD,MTY,CL,CLTG,OFD,OFD,SL, SPT')
) as t(TenderString1);
I am trying to get last numeric part in the given string.
For Example, below are the given strings and the result should be last numeric part only
SB124197 --> 124197
287276ACBX92 --> 92
R009321743-16 --> 16
How to achieve this functionality. Please help.
Try this:
select right(#str, patindex('%[^0-9]%',reverse(#str)) - 1)
Explanation:
Using PATINDEX with '%[^0-9]%' as a search pattern you get the starting position of the first occurrence of a character that is not a number.
Using REVERSE you get the position of the first non numeric character starting from the back of the string.
Edit:
To handle the case of strings not containing non numeric characters you can use:
select case
when patindex(#str, '%[^0-9]%') = 0 then #str
else right(#str, patindex('%[^0-9]%',reverse(#str)) - 1)
end
If your data always contains at least one non-numeric character then you can use the first query, otherwise use the second one.
Actual query:
So, if your table is something like this:
mycol
--------------
SB124197
287276ACBX92
R009321743-16
123456
then you can use the following query (works in SQL Server 2012+):
select iif(x.i = 0, mycol, right(mycol, x.i - 1))
from mytable
cross apply (select patindex('%[^0-9]%', reverse(mycol) )) as x(i)
Output:
mynum
------
124197
92
16
123456
Demo here
Here is one way using Patindex
SELECT RIGHT(strg, COALESCE(NULLIF(Patindex('%[^0-9]%', Reverse(strg)), 0) - 1, Len(strg)))
FROM (VALUES ('SB124197'),
('287276ACBX92'),
('R009321743-16')) tc (strg)
After reversing the string, we are finding the position of first non numeric character and extracting the data from that position till the end..
Result :
-----
124197
92
16
hie am trying to select the integer value before the char C in my SQL database table which contains the information below.
240mm2 X 15C WIRING CABLE
150mm2 X 3C flex
10mm2 x 4C swa
so far i have used the query
select left ('C',CHARINDEX ('C',product_name)) from product
and i get 'C' on my results which is correct. Now am stuck does anyone know how i can modify the above select query to get a result which only lists the integers for eg
15
3
4
Two observations: the integer before "C" has a space before it and there is no space between the integer and "C".
If these are generally true, then you can do what you want using substring_index():
select substring_index(substring_index(product_name, 'C', 1), ' ', -1) + 0 as thenumber
The + 0 simply converts the value to a number.
If you're doing this in SQL Server you could try the following:
Select Substring(product_name,
PATINDEX('% [0-9]%',product_name) + 1,
PATINDEX('%[0-9]C%',product_name) - PATINDEX('% [0-9]%',product_name)
) as num
from Product
This assumes that there is a space before the number and always a C after the number with no space.
It works out the starting point and then the length based on the start and end and performs a substring with the results.
You could use a combination of instring and substring.
First get the position of the C
Then substring till C
It goes like this:
SELECT INSTR('foobarbar', 'bar');
= 4
And then you select substring from 1 to 4.
I have a string with a specific pattern:
23;chair,red [$3]
i.e., a number followed by a semicolon, then a name followed by a left square bracket.
Assuming the semicolon ; always exists and the left square bracket [ always exists in the string, how do I extract the text between (and not including) the ; and the [ in a SQL Server query? Thanks.
Combine the SUBSTRING(), LEFT(), and CHARINDEX() functions.
SELECT LEFT(SUBSTRING(YOUR_FIELD,
CHARINDEX(';', YOUR_FIELD) + 1, 100),
CHARINDEX('[', YOUR_FIELD) - 1)
FROM YOUR_TABLE;
This assumes your field length will never exceed 100, but you can make it smarter to account for that if necessary by employing the LEN() function. I didn't bother since there's enough going on in there already, and I don't have an instance to test against, so I'm just eyeballing my parentheses, etc.
Assuming they always exist and are not part of your data, this will work:
declare #string varchar(8000) = '23;chair,red [$3]'
select substring(#string, charindex(';', #string) + 1, charindex(' [', #string) - charindex(';', #string) - 1)
An alternative to the answer provided by #Marc
SELECT SUBSTRING(LEFT(YOUR_FIELD, CHARINDEX('[', YOUR_FIELD) - 1), CHARINDEX(';', YOUR_FIELD) + 1, 100)
FROM YOUR_TABLE
WHERE CHARINDEX('[', YOUR_FIELD) > 0 AND
CHARINDEX(';', YOUR_FIELD) > 0;
This makes sure the delimiters exist, and solves an issue with the currently accepted answer where doing the LEFT last is working with the position of the last delimiter in the original string, rather than the revised substring.
select substring(your_field, CHARINDEX(';',your_field)+1
,CHARINDEX('[',your_field)-CHARINDEX(';',your_field)-1)
from your_table
Can't get the others to work. I believe you just want what is in between ';' and '[' in all cases regardless of how long the string in between is. After specifying the field in the substring function, the second argument is the starting location of what you will extract. That is, where the ';' is + 1 (fourth position - the c), because you don't want to include ';'. The next argument takes the location of the '[' (position 14) and subtracts the location of the spot after the ';' (fourth position - this is why I now subtract 1 in the query). This basically says substring(field,location I want substring to begin, how long I want substring to be). I've used this same function in other cases. If some of the fields don't have ';' and '[', you'll want to filter those out in the "where" clause, but that's a little different than the question. If your ';' was say... ';;;', you would use 3 instead of 1 in the example. Hope this helps!
If you need to split something into 3 pieces, such as an email address and you don't know the length of the middle part, try this (I just ran this on sqlserver 2012 so I know it works):
SELECT top 2000
emailaddr_ as email,
SUBSTRING(emailaddr_, 1,CHARINDEX('#',emailaddr_) -1) as username,
SUBSTRING(emailaddr_, CHARINDEX('#',emailaddr_)+1, (LEN(emailaddr_) - charindex('#',emailaddr_) - charindex('.',reverse(emailaddr_)) )) domain
FROM
emailTable
WHERE
charindex('#',emailaddr_)>0
AND
charindex('.',emailaddr_)>0;
GO
Hope this helps.
I think i have some syntax error in my script but can't figure out where.
I want to select the Integer that falls between a pair of ( ) begining from the right of a cell? Reason being, there might be another pair of brackets containing characters
and what if some records are w/o close brackets for some reason..
e.g.
Period | ProgrammeName |
Jan | ABC (Children) (30) |
Feb | Helloworld (20T (20) |
result: 30 20
select
Period,
ProgrammeName,
substring(ProgrammeName,(len(ProgrammeName) - (patindex('%(%', Reverse(ProgrammeName)))+2),(len(ProgrammeName)-1))
from
Table
but it only displays
30)
20)
i have been manipulating it so that it doesn't extract ')', but can't get the expected results.
So, you need to grab whatever's between the final set of open and closing brackets at the end of a string, right?
First off, find the first opening bracket from the end of the string. I'd use CHARINDEX, as you're just looking for a single character; you don't need to use pattern matching.
SELECT LEN(ProgrammeName) + 1 - CHARINDEX('(', REVERSE(ProgrammeName)) FROM Table
Then, find the first closing bracket from the end of the string:
SELECT LEN(ProgrammeName) + 1 - CHARINDEX(')', REVERSE(ProgrammeName)) FROM Table
Then, put those together. To use SUBSTRING, you need the position of the first character, then the length of the string you want, so you need the first result (the position of the '('), and then the second result minus the first result, to get the length of the bracketed bit, as a starting point:
SELECT (LEN(ProgrammeName) + 1 - CHARINDEX(')', REVERSE(ProgrammeName))) - (LEN(ProgrammeName) + 1 - CHARINDEX('(', REVERSE(ProgrammeName))) FROM Table
You also need to do a bit of fiddling to extract the part between the brackets, leaving the brackets alone. That's explained in the comments in this final example, where the final expression should be doing the job you want:
SELECT
-- Position of first bracket
LEN(ProgrammeName) + 1 - CHARINDEX('(', REVERSE(ProgrammeName)),
-- Position of second bracket
LEN(ProgrammeName) + 1 - CHARINDEX(')', REVERSE(ProgrammeName)),
-- Position of second bracket minus position of first bracket gives length
(LEN(ProgrammeName) + 1 - CHARINDEX(')', REVERSE(ProgrammeName))) - (LEN(ProgrammeName) + 1 - CHARINDEX('(', REVERSE(ProgrammeName))),
-- If we want to extract the bit between the brackets, we need to start from the bracket position
-- plus one character, and knock one off the length, to avoid grabbing the closing bracket.
SUBSTRING(ProgrammeName, 1 + LEN(ProgrammeName) + 1 - CHARINDEX('(', REVERSE(ProgrammeName)), (LEN(ProgrammeName) + 1 - CHARINDEX(')', REVERSE(ProgrammeName))) - (LEN(ProgrammeName) + 1 - CHARINDEX('(', REVERSE(ProgrammeName))) - 1)
FROM
Table
I've broken my answer down so you can see how I approach problems like these -- do them one bit at a time, checking the results as you go along, and it's easier to get your head around.