concat apostrophe to oracle sql query - sql

Hi all I am looking for some pointers on how I can add an apostrophe to my query results on my first column.
My current query:
set verify off
set colsep "',"
set pagesize 2000
ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YY-HH24:MI:SS';
spool /home/user/out.txt
select '[' || table1.collectdatetime as "['Date-Time",table1.cycletime as "'Time'" from table1 where interfacename='somename' and collectdatetime > (CURRENT_DATE - 1)
order by collectdatetime ASC;
Which results:
['Date-Time ','InCycleTime'
-------------------',-------------
[02-MAR-13-17:56:16', 29
What I am struglling with is getting the results to return and add an apostrophe after the [
['Date-Time ','InCycleTime'
-------------------',-------------
['02-MAR-13-17:56:16', 29
This is for an oracle 11.1.0.7 build. The data is being queried and parsed but I need to get that apostrophe issue worked out.

use this:
select '[''' || table1.collectdatetime as "['Date-Time",table1.cycletime as "'Time'" from table1 where interfacename='somename' and collectdatetime > (CURRENT_DATE - 1)
order by collectdatetime ASC;

Related

ADF -pass SQL query in source with single quotes in date column

I am trying to pass parameters in dynamic pipeline in source query inside data flow. Tablename and Column name is dynamic and will be passed through parameters. But i got an error while executing the below ADF query:
ADF Query: concat('SELECT ', $ValidationColumn, ' FROM ', $ValidationTable,' WHERE audit_datetime >= ', replace($StartAuditDateTime,'T',''),' AND audit_datetime <= ' ,replace($EndAuditDateTime,'T','') )
Expected SQL query: Select ID from Master where audit_datetime >= '2020-07-23 18:47:20.5666' AND audit_datetime <= '2020-07-24 01:47:20.5456'
Please try below expression to form your query in your ADF Mapping dataflow expression builder.
"SELECT '{$ValidationColumn}' FROM '{$ValidationTable}' WHERE audit_datetime >= '{$StartAuditDateTime}' AND audit_datetime <= '{$EndAuditDateTime}'"
This will result in below QUERY formation:
SELECT ID FROM Master WHERE audit_datetime >= '2020-07-23 18:47:20.5666' AND audit_datetime <= '2020-07-24 01:47:20.5456'
I am assuming you want to escape single quote to get '{yourDateValue}' format,
you can do that like this ''''
example:
#{concat('select * from [yourtable] WHERE [yourColumn] >=','''',{yourexpression},'''')}
Use this SQL syntax in Expression builder in ADF Data Flow where you need to use singe quotes
"SELECT * FROM myTable WHERE someColumn = 'Y'"

SQL to get whole words till end from the keyword

Consider I have text from the field (notes) as below:
Please check http://example.com
I want a SQL query which fetches the link part only. That is to find keyword http and print till the last.
Output:
http://example.com
Also if the text field doesnt have any link, can we print NA?
CASE WHEN sys like '%Clo%'
THEN RIGHT( i.notes,LEN(i.notes) - CHARINDEX('http',i.notes,1)+1)
ELSE "No Link Available" END AS Cl_Link
For SQL Server you can consider this below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
For MySQL-
SET #T = 'Please check http://example.com';
SELECT RIGHT(#T,LENGTH(#T) - POSITION("http:" IN #T)+1)
In case of select query using table, queries will be-
-- SQL Server
SELECT RIGHT(column_name,LEN(column_name) - CHARINDEX('http:',column_name,1)+1) FROM your_table_name
-- MySQL
SELECT RIGHT(column_name,LENGTH(column_name) - POSITION("http:" IN column_name)+1) FROM your_table_name
To apply 'NA' when no link available, please use the below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT
CASE
WHEN CHARINDEX('http:',#T,1) >= 1 THEN RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
ELSE 'NA'
END
Below is the query for your reference, executed on mysql:
SELECT substr(columnname,locate('http',columnname)) as link
FROM `tablename` where column=value
For MySQL try this:
select REGEXP_SUBSTR(text_column, 'http\:\/\/([\\w\\.\\S]+)') from mytest

Concat function in postgresql

I have the below select statement in hive .It executes perfectely fine.
In Hive
select
COALESCE(product_name,CONCAT(CONCAT(CONCAT(TRIM(product_id),' -
'),trim(plan_code)),' - UNKNOWN')) as product_name
from table name;
I am trying to use the same select statement in POSTGRESQL and it throw me error saying "
Query execution failed
Reason:
SQL Error [42883]: ERROR: function concat(text, unknown) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
In postgresql:
select
COALESCE(product_name,CONCAT(CONCAT(CONCAT(TRIM(product_id),' -
'),trim(plan_code)),' - UNKNOWN')) as product_name
from table name;
Could some one throw some light on this ?
Instead of concat try with ||:
SELECT COALESCE(product_name,
(TRIM(product_id) || ' - ' || TRIM(plan_code) || ' - UNKNOWN')
) AS product_name
FROM tablename;
or simply a single CONCAT as:
SELECT COALESCE(product_name,
CONCAT(TRIM(product_id)::text, ' - ', TRIM(plan_code)::text, ' - UNKNOWN')
) AS product_name
FROM tablename;
You can also consider using format function:
SELECT coalesce(product_name, format('%s - %s - UNKNOWN', trim(product_id), trim(plan_code)))

Using variable from cursor for loop in where clause

I was doing some SQL on Oracle and I found something weird.
I have that FOR loop that iterate over a list of date (Stored as VARCHAR2):
FOR current_date in (SELECT DISTINCT DATCRE as datcre
FROM EVE cdv
WHERE cdv.datcre BETWEEN p_datedeb AND p_datefin)
LOOP
[queries]
END LOOP;
I need to use current_date in the where clause of the following query:
SELECT COUNT(cdv.numeve)
INTO nb
FROM EVE cdv
WHERE cdv.datcre = current_date.datcre;
This query doesn't work, and I get a ORA-00933: SQL command not properly ended.
Now, if I declare a curDate VARCHAR2(10) variable, store current_date.datcre in it and use it in the query, it works fine:
curDate := (current_date.datcre);
SELECT COUNT(cdv.numeve)
INTO nb
FROM EVE cdv
WHERE cdv.datcre = curDate;
Am I missing something or is this just the way Oracle works ?

How to write this query compatible with oracle database

I am facing issue to convert the sql query to oracle .Actually i am new to oracle db.
SELECT TI1.FOLDERRSN, DBO.F_OPENTAX_PROPERTYROLL_FOLDER(TI1.FOLDERRSN) ROLL,
TI1.DUEDATE DUEDATE, TI1.YEARFORBILLING,(TI1.SUMDUETAX + TI1.SUMPAIDTAX + TI1.SUMDUEPENALTY + TI1.SUMPAIDPENALTY) OUTSTANDING
FROM TAXINSTALLMENT TI1 WHERE (TI1.SUMDUETAX + TI1.SUMPAIDTAX + TI1.SUMDUEPENALTY + TI1.SUMPAIDPENALTY) > 0
AND EXISTS (SELECT * FROM TAXINSTALLMENT TI2 WHERE YEAR(TI2.DUEDATE) BETWEEN 1980 AND YEAR(GETDATE()) - 5 AND (TI2.SUMDUETAX + TI2.SUMPAIDTAX + TI2.SUMDUEPENALTY + TI2.SUMPAIDPENALTY) > 0
AND TI2.FOLDERRSN = TI1.FOLDERRSN ) ORDER BY TI1.FOLDERRSN, DUEDATE DESC
Anyone give me idea to change to oracle .
Thanks
The statement uses a function F_OPENTAX_PROPERTYROLL_FOLDER(TI1.FOLDERRSN) in Schema DBO. Make sure you have installed that function in that database Schema and have granted an execution right to the user in question or to PUBLIC for that matter.
EDIT: Moreover there is no function YEAR in Oracle. You must replace it with EXTRACT: EXTRACT(YEAR FROM TI2.DUEDATE) and for GetDate(): EXTRACT(YEAR FROM SYSDATE).