How to get first 9 digits of a column in openquery? - sql

I am using SSMS to pull some data from oracle via openquery and only need the first 9 digits of a number out of a column.
I have tried using "left(column1, 9)" and it returns "LEFT:invalid identifier"
SELECT *
FROM OPENQUERY(servername,'
SELECT left(sv.column2, 9) AS new_number
FROM server.servername sv
')
column2 = 0987654321
new_number = 098765432

You miss the "Select"
SELECT *
FROM OPENQUERY([SERVER\INSTANCE],' select left(sv.column, 9) AS new_number FROM BD.dbo.Table sv')
Good Code.

In Oracle, you would use substring(), so something like this:
SELECT SUBSTR(sv.column2, 1, 9) AS new_number
FROM server.servername sv

Related

Select Where Like regular expression

I need to create a SQL Query.
This query need to select from a table where a column contains regular expression.
For example, I have those values:
TABLE test (name)
XHRTCNW
DHRTRRR
XHRTCOP
CPHCTPC
CDDHRTF
PEOFOFD
I want to select all the data who have "HRT" after 1 char (value 1, 2 and 3 - Values who looks like "-HRT---") but not those who might have "HRT" after 1 char (value 5).
So I'm not sure how to do it because a simple
SELECT *
FROM test
WHERE name LIKE "%HRT%"
will return value 1, 2, 3 and 5.
Sorry if I'm not really clear with what I want/need.
You can also change the pattern. Instead of using % which means zero-or-more anything, you can use _ which means exactly one.
SELECT * FROM test WHERE name like '_HRT%';
You can use substring.
SELECT * FROM test WHERE substring(name from 2 for 3) = 'HRT'
Are the names always 7 letters? Do:
SELECT substring (2, 4, field) from sometable
That will just select the 2-4th characters and then you can use like "%HRT"

like search on number column in SQL

How do I do a like search on a number column in SQL?
I want numbers which are like '0.0000%'.
I tried with
select * from emp where emp_id & '' like '123%'
select * from emp where CONVERT(varchar(20), emp_id) like '123%'
but in vain.
Please help me
Regardless of which DBMS you are using AND assuming you have a valid reason to do this, you have several ways to solve problems like these. I can think of three right now:
Convert the number to a string and use a LIKE operator on this:
select *
from emp
where to_char(emp_id) like '123%';
Use mathematical operators directly (like Andrey suggests), for example:
select *
from table
where num between 0 and 0.0001;
Construct a mathematical expression (actually, this is just another case of method 2), for example:
select *
from table
where abs(num - round(num, 5)) < 0.00001;
Use comparison operators (> and <):
select * from table where num > 0 and num < 0.00001
select 0.0001*1000 from dual if it is <1 means the 0.0001 has 3 or more zeros.
so i did like
select * from emp where emp_id*10000<1

How I can get the first 3 digits in 123456 Numbers in sql?

I have field called CallingParty in My CDR table it contains data like this:
CallingParty
------------
267672668788
I want to select the first 3 number of each of those numbers like
CallingParty
------------
267
if CallingParty is of type int:
SELECT CAST(LEFT(CallingParty, 3) AS INT)
From CDR
SQL Server has a Left() function, but it works best on strings. (varchar/char in SQL)
Select left(cast(267672668788 as varchar), 3)
Use this query:
SELECT SUBSTRING(CAST(CallingParty AS VARCHAR(50)), 1, 3) FROM [CDR]
If the data length does not change then you can always divide by 10 * the digits you have
SELECT FLOOR(267672668788 / 1000000000)
=267
Try this:
SELECT Substring(callingparty, 1, Length(callingparty) - 9)
FROM cdr;

How can I select the first 100 characters in SQL Server?

I want to truncate a column to a max of 100 characters. How do you do this in SQL Server?
Try this:
SELECT LEFT (your_column, 100) FROM your_table
Edit:
you can also try something like this:
SELECT LEFT (your_column, LEN(your_column)-5) FROM your_table
for say if you want to trim the last 5 characters from a record.
You can also use the LEFT() function.
LEFT(col, 100)
SUBSTRING(myColumn, 1, 100)
See the docs: http://msdn.microsoft.com/en-us/library/ms187748.aspx
substring is the method:
SUBSTRING ( value_expression ,start_expression , length_expression )
from the help.
SELECT SUBSTR(COLUMN_NAME, 1, LENGTH) FROM TABLENAME where LENGTH(COLUMN_NAME) > LENGTH
Ex:
SELECT SUBSTR(DESCRIPTION,1,100) FROM STOREDETAILS where LENGTH(DESCRIPTION)>100
For those records, with length less than 100, the actual value would be shown.
Otherwise, some databases induce blank characters in the resultant records.

Searching Technique in SQL (Like,Contain)

I want to compare and select a field from DB using Like keyword or any other technique.
My query is the following:
SELECT * FROM Test WHERE name LIKE '%xxxxxx_Ramakrishnan_zzzzz%';
but my fields only contain 'Ramakrishnan'
My Input string contain some extra character xxxxxx_Ramakrishnan_zzzzz
I want the SQL query for this. Can any one please help me?
You mean you want it the other way round? Like this?
Select * from Test where 'xxxxxx_Ramakrishnan_zzzzz' LIKE '%' + name + '%';
You can use the MySQL functions, LOCATE() precisely like,
SELECT * FROM WHERE LOCATE("Ramakrishnan",input) > 0
Are the xxxxxx and zzzzz bits always 6 and 5 characters? If so, then this is doable with a bit of string cutting.
with Test (id,name) as (
select 1, 'Ramakrishnan'
union
select 2, 'Coxy'
union
select 3, 'xxxxxx_Ramakrishnan_zzzzz'
)
Select * from Test where name like '%'+SUBSTRING('xxxxxx_Ramakrishnan_zzzzz', 8, CHARINDEX('_',SUBSTRING('xxxxxx_Ramakrishnan_zzzzz',8,100))-1)+'%'
Results in:
id name
1 Ramakrishnan
3 xxxxxx_Ramakrishnan_zzzzz
If they are variable lengths, then it will be a horrible construction of SUBSTRING,CHARINDEX, REVERSE and LEN functions.