Teradata SQL - how to get everything before hyphen? - sql

The question is how to get all data before hyphen using SQL in Teradata?
The pattern is like this : 123ABC-456. I need only 123ABC.
This regex expression:
SELECT RegExp_Replace('123ABC-456', '\w[^-]*$')
returns "123ABC-" with hyphen for some reason
and this "^[^-]*[^ -]" - returns "-456" instead of "123ABC"
Please any help?

You can try using REGEXP_SUBSTR()
select REGEXP_SUBSTR('123ABC-456', '^[^-]*[^ -]')

Related

How to get substring based on a character and starting to read the string from the right

I have the following values on a column:
DB3-0800-VRET,
DB3-0800-IC,
IB-TZ-850-IB,
O11FS-OB ...
From each value I want to remove the last part after the dash.
I need to have the following result:
DB3-0800-VRET -> DB3-0800,
DB3-0800-IC -> DB3-0800,
O11FS-OB -> O11FS
I tried to work with the SPLIT_PART function of RedShift but I didn't have any luck.
If someone knows a regex to select the part I need I'd be grateful.
In both Postgres and Redshift, you should be able to use regexp_replace():
select regexp_replace(str, '-[^-]+$', '')

string between two special characters impala sql

Hi all I am trying to write sql for selecting string between two special characters.
example: in the table, field value like 7185878969-129981041-000000 . how can I select only middle portion 129981041 without hard coding. What will be the best way to go about this?.Please provide sample code. Thanks
Impala has split_part():
select split_part(col, '-', 2)
Try this for MySQL:
SELECT REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(Column,'-',2)),'-',1))
FROM table_name;
Result:
129981041

Regular expression for gettin data after - in sql

I have a column with assignment numbers like - 11827,27266,91717,09818-2,726252-3,8716151-0,827272,18181
Now i am selecting the records like
select assignment_number from table;
But now i want that the column detail is retreived in such a way that numbers are only retrieved without -2 -3 etc like
726252-3---> 726252 8716151-0-->8716151
I know i can use regex for this but i do not know how to use it
This will select everthing before the character -:
^([^-]+)
From 726252-3 will match 726252
You would use regexp() substr:
select regexp_substr(assignmentnumber, '[0-9]+')
This will return the first string of numbers encountered in the string.

How to select all the string characters preceding a . in oracle

I am using Oracle 11 G and have the following set of data:
12.0
4.2
Version.1
7.9
abc.72
I want to return all string characters before the period. What sort of query would I run in order to achieve this? Any help would be greatly appreciated, thanks!
You can try a combination of instr and substr.
Something like this:
select substr(field, 1, instr(field, '.') - 1)
from your_table;
Assuming field always contains a . character on it.
You can also deal with strings without a . by using case, if or any other similar valid conditional function on Oracle's SQL language implementation.
Of course, you can always put this on a function to make it look nicer on your query.

Select query that displays Joined words separately, not using a function

I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.