I have an SQL query that brings back 17 numbers with this format
06037-11
I need to add a 0 before the dash, so it is:
060370-11
Is there an easy way to do this? I have seen STUFF() as an option, but I don't understand it.
Edit
I am using Teradata
One way in Oracle:
with qry as
(select '06037-11' code from dual)
select regexp_replace(code, '-', '0-') from qry;
Previous response includes example for Teradata 14.x using regular expression support. The following will work in Teradata 13.x or Teradata 12.x without regular expression support:
SELECT SUBSTRING('06037-11' FROM 1 FOR (POSITION('-' IN '06037-11') -1))
|| '0-'
|| SUBSTRING('06037-11' FROM (POSITION('-' IN '06037-11') + 1))
since you talk about STUFF I assue you use SQL Server:
select stuff(your_column, charindex('-', your_column), 0, '0')
from your_table
SQLFiddle demo
Related
i'm trying to retrieve the last substring from a string, starting from the end.
Here it is my dataset:
Input:
BRAND_Arnette
BRAND_Persol
MODEL_CODE_DISPLAY_226781
Output:
Arnette
Persol
226781
What i 've managed to do is to retrieve what i need, but i'm not using an universal approach, because i'm considerging always the latest 10 chars, starting from the right:
SELECT
SUBSTR(RIGHT(rtrim(cast(attrval.IDENTIFIER as char(50))), 10), LOCATE('_',RIGHT(rtrim(cast(attrval.IDENTIFIER as char(50))), 10))+1)
FROM ...
How can this select be edited so it can be always valid? Thanks
Try the following expression:
SUBSTR (identifier, LOCATE_IN_STRING (identifier, '_', -1) + 1)
dbfiddle example.
I would suggest regexp_substr():
select regexp_substr(identifier, '[^_]+$')
Here is a db<>fiddle.
SQL....On my table I have attribute table with “Pat0700-1700” on my report I want to drop the Pat and only display 0700-1700. How would I accomplish this on SQL. I have search and tried the substring with neg results.
On these following RDBMS:
Oracle
MySQL
DB2
StandardSQL
you can try with the function SUBSTR():
SELECT SUBSTR(<column>, 4) AS substr_string
FROM <table>
OUTPUT:
substr_string
-------------
0700-1700
The standard SQL method would be replace():
select replace(col, 'Pat', '')
Given that the rest of the string has a fixed format -- 9 characters -- you might also find that one of these is appropriate (and more general):
select right(col, 9)
select substr(col, 4, 9) -- or perhaps substring()
I have names in my dataset and they include parentheses. But, I am trying to clean up the names to exclude those parentheses.
Example: ABC Company (Somewhere, WY)
What I want to turn it into is: ABC Company
I'm using standard SQL with google big query.
I've done some research and I know big query has left(), but I do not know the equivalent of find(). My plan was to do something that finds the ( and then gives me everything to the left of -1 characters from the (.
My plan was to do something that finds the ( and then gives me everything to the left of -1 characters from the (.
Good plan! In BigQuery Standard SQL - equivalent of LEFT is SUBSTR(value, position[, length]) and equivalent of FIND is STRPOS(value1, value2)
With this in mind your query can look like (which is exactly as you planned)
#standardSQL
WITH names AS (
SELECT 'ABC Company (Somewhere, WY)' AS name
)
SELECT SUBSTR(name, 1, STRPOS(name, '(') - 1) AS clean_name
FROM names
Usually, string functions are less expensive than regular expression functions, so if you have pattern as in your example - you should go with above version
But in more generic cases, when pattern to clean is more dynamic like in Graham's answer - you should go with solution in Graham's answer
Just use REGEXP_REPLACE + TRIM. This will work with all variants (just not nested parentheses):
#standardSQL
WITH
names AS (
SELECT
'ABC Company (Somewhere, WY)' AS name
UNION ALL
SELECT
'(Somewhere, WY) ABC Company' AS name
UNION ALL
SELECT
'ABC (Somewhere, WY) Company' AS name)
SELECT
TRIM(REGEXP_REPLACE(name,r'\(.*?\)',''), ' ') AS cleaned
FROM
names
Use REGEXP_EXTRACT:
SELECT
RTRIM(REGEXP_EXTRACT(names, r'([^(]*)')) AS new_name
FROM yourTable
The regex used here will greedily consume and match everything up until hitting an opening parenthesis. I used RTRIM to remove any unwanted whitespace picked up by the regex.
Note that this approach is robust with respect to the edge case of an address record not having any term with parentheses. In this case, the above query would just return the entire original value.
I can't test this solution at the moment, but you can combine SUBSTR and INSTR. Like this:
SELECT CASE WHEN INSTR(name, '(') > 0 THEN SUBSTR( name, 1, INSTR(name, '(') ) ELSE name END as name FROM table;
my table have column contain data like this.
SQL 2008
97W
125/ 122Q
121/ 118Q
121/ 118S
123/ 120S
112H
111H
i am trying to remove data before / so output will look like as
97W
122Q
118Q
118S
120S
112H
111H
can anyone share experience how can i achieve if came across such scenario.
Thanks,
Try this:
SELECT LTRIM(RIGHT(mycol, LEN(mycol) - CHARINDEX('/', mycol)))
FROM mytable
Demo here
Look at this one
Select LTRIM(SUBSTRING(col, CHARINDEX('/', col) + 1, LEN(col)))
from table
Use a CASE expression to check whether the string contains /, if yes use CHARINDEX to find the index of / and get the right part. Else the string as it is.
Query
SELECT
CASE WHEN your_column_name LIKE '%/%'
THEN LTRIM(RIGHT(your_column_name, CHARINDEX('/', REVERSE(your_column_name), 1) - 1))
ELSE your_column_name END AS new_string
FROM your_table_name;
SQL Fiddle Demo
Can you please help to get this code for SQL?
I have column name INFO_01 which contain info like:
D10-52247-479-245 HALL SO
and I would like to extract only
D10-52247-479
I want the part of the text before the third "-" dash.
You'll need to get the position of the third dash (using instr) and then use substr to get the necessary part of the string.
with temp as (
select 'D10-52247-479-245 HALL SO' test_string from dual)
select test_string,
instr(test_string,1,3) third_dash,
substr(test_string,1,instr(test_string,1,3)-1) result
from temp
);
Here is a simple statement that should work:
SELECT SUBSTR(column, 1, INSTR(column,'-',1,3) ) FROM table;
Using a combination of SUBSTR and INSTR will return what you want:
SELECT SUBSTR('D10-52247-479-245', 0, INSTR('D10-52247-479-245', '-', -1, 1)-1) AS output
FROM DUAL
Result:
output
-------------
D10-52247-479
Use:
SELECT SUBSTR(t.column, 0, INSTR(t.column, '-', -1, 1)-1) AS output
FROM YOUR_TABLE t
Reference:
SUBSTR
INSTR
Addendum
If using Oracle10g+, you can use regex via REGEXP_SUBSTR.
I'm assuming MySQL, let me know if I'm wrong here. But using SUBSTRING_INDEX you could do the following:
SELECT SUBSTRING_INDEX(column, '-', 3)
EDIT
Appears to be oracle. Looks like we may have to resort to REGEXP_SUBSTR
SELECT REGEXP_SUBSTR(column, '^((?.*\-){2}[^\-]*)')
Can't test, so not sure what kind of result that will have...