SQl Trim problem - sql

I am trying to pull data from table using this select statement..
SELECT ID_NO
FROM EMPLOYEE
WHERE trim(SYN_NO) ='21IT';
SYN_NO column hold data in this format
21IT / 00065421
I want to get just first four characters and drop rest of it.. i tried trim, rtrim but it didnt work. Is there a way to do this.. Thank you

Sadly for those of us coming from SQL Server, there is no LEFT in oracle. The article I linked below talks about one person's encounter with this problem, and recommends using
SUBSTR(SYN_NO,1,4)
(see article)
Oracle SUBSTR specification

Sorry, just realized this is Oracle. You need to use SUBSTR:
SELECT ID_NO
FROM EMPLOYEE
WHERE SUBSTR(SYN_NO, 1, 4) ='21IT';

In Oracle, you'd use the SUBSTR function
SELECT id_no
FROM employee
WHERE substr(syn_no,1,4) = '21IT'

Related

trim whitespaces in oracle where condition

I'm facing the following issues when i tried to retrieve the data from oracle database by trimming the whitespace. I'm using oracle 12G version.
Let's say the name column has some whitespaces like 'Test '.
Table name - PGM_DETAIL
ID NAME
1 Test
When I'm trying to fetch the data using following query. It's not fetching it.
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');
Could you anyone please suggest me what the problem in my sql statement.
Thanks in advance...,
Oracle comparisons are case-sensitive by default.
So try:
WHERE UPPER(TRIM(NAME)) = TRIM('TEST');
try
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');

SQL code for retrieving values of column starting using wildcard

I want to look for values in variable/column which start with 'S' and has 'gg' in between.
For instance Staggered is a word which starts with alphabet S and has gg in between the word.
so what sql query to write to get the result.
Due to the fact that you did not provide much meta information (which database?), I'll just show the following:
SELECT * FROM <table>
WHERE <columnname> LIKE 'S%gg%';
Good luck :)
As the target database is not mentioned, I will answer with Oracle syntax:
select *
from TABLE_NAME
where COL_NAME like 'S%gg%'

Replacing values with wildcards (parsing text data)

I have rows which contains HTML tags. e.g.
<b>Abc</b> <strong>Bca</strong>
So I need to cut it out. As I suggest I need to find something like '%<%>%' and make a REPLACE to ''.
How can I do it? Interested for both solutions - MS SQL & Oracle also.
Assuming table is called yourtable and field is called htmltag.
In SQL Server:
SELECT
SUBSTRING(substring(htmltag,charindex('>',htmltag)+1,250),0,CHARINDEX('<',
substring(htmltag,charindex('>',htmltag)+1 ,250),0))
FROM yourtable
SQL FIDDLE
In Oracle
SELECT regexp_replace(htmltag, '<[^>]+>', '') htmltag
FROM yourtable
SQL FIDDLE

Compare String with "like" against a list

is it possible to compare a string against a list using "like" and wildcards, so sth like
select column
from table
where column like('%foo%', '%bar%')
The example does not work in any database format I know. How can I do this without using a verbose solution like
select column
from table
where column like'%foo%' or column like '%bar%'
I am interested in a platform-independent solution but mainly Sybase ASE.
Thanks very much!
If you are using MySQL/PostgreSQL you could use regexp, that is one way.
SELECT column FROM table WHERE (column REGEXP '^ALA[0-9]')
http://dev.mysql.com/doc/refman/5.0/en/regexp.html#operator_regexp
http://www.oreillynet.com/pub/a/databases/2006/02/02/postgresq_regexes.html
Second solution would be, mentioned by you creating many likes, joined by or.

Oracle - Select where field has lowercase characters

I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields are varchar - last_name and first_name.
When rows are inserted into this table, the first name and last name fields are supposed to be in all upper case, but somehow some values in these two fields are mixed case.
I want to run a query that will show me all of the rows in the table that have first or last names with lowercase characters in it.
I searched the net and found REGEXP_LIKE, but that must be for newer versions of oracle - it doesn't seem to work for me.
Another thing I tried was to translate "abcde...z" to "$$$$$...$" and then search for a '$' in my field, but there has to be a better way?
Thanks in advance!
How about this:
select id, first, last from mytable
where first != upper(first) or last != upper(last);
I think BQ's SQL and Justin's second SQL will work, because in this scenario:
first_name last_name
---------- ---------
bob johnson
Bob Johnson
BOB JOHNSON
I want my query to return the first 2 rows.
I just want to make sure that this will be an efficient query though - my table has 500 million rows in it.
When you say upper(first_name) != first_name, is "first_name" always pertaining to the current row that oracle is looking at? I was afraid to use this method at first because I was afraid I would end up joining this table to itself, but they way you both wrote the SQL it appears that the equality check is only operating on a row-by-row basis, which would work for me.
If you are looking for Oracle 10g or higher you can use the below example. Consider that you need to find out the rows where the any of the letter in a column is lowercase.
Column1
.......
MISS
miss
MiSS
In the above example, if you need to find the values miss and MiSS, then you could use the below query
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]');
Try this:
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]','c'); => Miss, miss lower text
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[A-Z]','c'); => Miss, MISS upper text
SELECT *
FROM mytable
WHERE FIRST_NAME IN (SELECT FIRST_NAME
FROM MY_TABLE
MINUS
SELECT UPPER(FIRST_NAME)
FROM MY_TABLE )
for SQL server where the DB collation setting is Case insensitive use the following:
SELECT * FROM tbl_user WHERE LEFT(username,1) COLLATE Latin1_General_CS_AI <> UPPER(LEFT(username,1))