Add keyword before and after cast() in oracle - sql

I would like to add a keyword before and after each field value in Oracle.
For example if I am getting 123 as my ID, I would like to make it
Test123Test
Here is my query:
SELECT
CAST("ID" as varchar(10))
FROM
TABLENAME;
I have tried add + "Test" but it is giving me error.

Use || instead of + to concatenate strings in Oracle.
SELECT 'test' || CAST(ID as varchar(10)) || 'test'
FROM TABLENAME
Note that I removed the " around ID too, since you most likely won't need them and they can cause problems when it unintended strictly matches column names.

I have tried add + "Test" but it is giving me error.
Perhaps, + is used as concatenation in SQL Server. In Oracle, you could use the CONCAT function or the || operator.
The concat function is restricted to only two strings. You can have a look the concat function in the documentation http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions026.htm.
Let's see an example using the operator -
SELECT 'test' || to_char(id) || 'test' new_id
FROM TABLENAME

Related

SQL to replace all occurrences from table using REGEX_REPLACE

I would like to know how I can replace all occurrences (from any column in a table) of \\\\N with an empty string. I think I should use the REGEX_REPLACE function, but I've only been able to see examples of it used on one column inside Snowflake.
REGEXP_REPLACE( <subject> , <pattern> [ , <replacement> , <position> , <occurrence> , <parameters> ] )
What you're looking for is not possible natively in SQL. You could do
update your_table
set col1=replace(col1,'\\\\N',''),
col2=replace(col2,'\\\\N',''),
col3=replace(col3,'\\\\N',''),
....
I personally prefer the following because I can run the select portion to take a look at my output before making any changes
create or replace table your_table as
select top 0 * --to avoid having to write column names in subsequent select
from your_table
union all
select replace(col1,'\\\\N',''),
replace(col2,'\\\\N',''),
replace(col3,'\\\\N',''),
...
from your_table
You can generate the SQL to operate on each column using the 'show columns' then build a set of SQL statements using the lastqueryID
show columns in table mytable;
select 'update mytable set ' || "column_name" || ' = replace(' || "column_name" || ',''\\\\\\\\N'','''',);' from TABLE(RESULT_SCAN(LAST_QUERY_ID()));
My issue was that Snowflake by default replaced NULL values with \\N that's why I was seeing the exported file from my s3 bucket containing a double escaped newline characters. The issue wasn't a problem with the table itself but after export and setting the file_format option to override the default as empty did the trick.
copy into <#s3..> from <view> header = true max_file_size = 5368709120 Single=True overwrite = true file_format = (TYPE='CSV' COMPRESSION='NONE' DEFAULT_NULL=());

Applying semicolon to the values in a table in SQL

Currently I am working on an SQL script. The result of the script is a table with three columns.I want to apply a semicolon to the left side of all the values.
Select distinct
emp_id, -->numeric value
emp_dob, -- >numeric value
emp_ssid -- >alphnumeric value
.
.
How can I do it. I am quite new to SQL
If you are using Oracle, I think you want ||:
Select distinct emp_id || ';',
emp_dob || ';' -- >numeric value
emp_ssid, || ';' -- >alphnumeric value
Your syntax strongly suggests SQL Server, but the question is tagged Oracle.
Because you wanted to append a semicolon to the end of the value, your result column cannot be converted to a numeric value. You need to cast it to nvarchar and then append.
Please try
CAST(emp_id AS NVARCHAR(20)) + ';'
Use CONVERT to turn a numeric column into string. The string concatenation operator in Sybase is the non-standard +.
select
';' + convert(varchar, emp_id),
';' + convert(varchar, emp_dob),
';' + emp_ssid
from ...

Oracle LIKE-wildcard in inner SELECT query

In SQL Server T-SQL I used to use the scenario like this
SELECT .. FROM .. WHERE sometable.eng LIKE (SELECT tmpcolumn FROM tmptable WHERE tmpID = #counter) + '%';
How to pass LIKE (subquery) + '%' in Oracle correcly?
Does it actually work for Oracle 11g+ or not?
.. smth LIKE (SELECT .. FROM ..) + '%';
The underscore _ for fixed length doesn't fit my needs, so % only.
Oracle uses || for string concatenation, not +. So it should be:
smth LIKE (SELECT .. FROM ..) || '%'
This seems like such an odd formulation. Just as a note, I would write the query as:
SELECT ..
FROM ..
WHERE EXISTS (SELECT 1
FROM tmptable
WHERE tmpID = #Counter AND
sometable.eng LIKE tmpcolumn || '%'
);
Putting a subquery between the keyword LIKE and the wildcard makes the query harder to read (at least for me).

struggling with creating Insert query

I create Insert statement for organization table like this:
select'Insert into Organizations(Name,ContactPerson,ContactNumber,Mobilenumber)values('''+Nameofthecompany+''+','+Nameofthepersonresponsibleforrecruitment+','+PhoneNumber+','+MobileNumber+''')' from Organization
When I execute this statement I get insert statement. But the issue is where the value is null, it shows all columns null.
Example: (in database)
Name: xxxx
ContactPerson: zzzz
ContactNumber:444444
MobileNumber: null
so my insert statement looks like:
Null.
I want only that column provide null. other details showing properly. Is there any way in sql server? Help me anyone...
The result of concatenating anything to NULL, even itself, is always NULL. Workaround with ISNULL function:
select'Insert into Organizations(Name,ContactPerson,ContactNumber,Mobilenumber)
values('''+ISNULL(Nameofthecompany, 'NULL')+''+','
+ISNULL(Nameofthepersonresponsibleforrecruitment, 'NULL')+','
+ISNULL(PhoneNumber, 'NULL')+','
+ISNULL(MobileNumber, 'NULL')+''')'
from Organization
Demo on SQLFiddle
Sure - just use ISNULL(..) to turn a NULL into e.g. an empty string:
SELECT
'INSERT INTO Organizations(Name, ContactPerson, ContactNumber, Mobilenumber) VALUES(''' +
ISNULL(Nameofthecompany, '') + '' + ',' +
ISNULL(Nameofthepersonresponsibleforrecruitment, '') + ',' +
ISNULL(PhoneNumber, '') + ',' + ISNULL(MobileNumber,'') + ''')'
FROM Organization
When you are adding each of the parameters to the SQL statement, you need to check whether they're null, and if so use the keyword NULL, otherwise include a literal string surrounded with single quotes, but bearing in mind that if the string contains any single quotes, they need to be replaced with two single quotes.
Update the SQL for each parameter something like the following:
CASE WHEN MobileNumber IS NULL THEN 'NULL' ELSE '''' + REPLACE(MobileNumber, '''', '''''') + '''' END

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example
select id, (name + ' ' + surname + ' ' + age) as info from users
this doesn't work, how to do it?
I'm using postgreSQL.
Postgres uses || to concatenate strings; your query needs to be:
select id, (name || ' ' || surname || ' ' || age) as info from users
It should be the key just above the Enter key on a full keyboard, but on mine it's not a sold line - there's a break in the middle of it. You have to hold the Shift key to get the character (called a pipe, btw).
I believe the ANSI standard concatenation operator is: ||
SELECT id, name ||' ' || surname || ' ' || age AS info FROM users
It perfectly might be dependent of the database I would take a look for a concatenation function for the database you are running the select for.
Example. Mysql: CONCAT(name, surname, age).
You may need to cast the fields to a common type before concatenating. In T-SQL for example this would read.
Select id, Cast(name as varchar(50)) + ' ' + Cast(surname as varchar(50)) + ' ' +
Cast(age as varchar(3)) As info From Users
|| is used for this purpose.
Use it like
select name ||' ' ||surname ||' ' ||age as info from users
If you're using mysql or oracle then try CONCAT function:
SELECT id, CONCAT(name, ' ', surname, ' ', age) as info FROM users
That should work as is, but in general it is better not to do too much concatenation sql side if you can help it; rather return all the columns and concat them on the other end.
What are the data types you are using? You may need to CAST / CONVERT into (n)varchar
Make sure your data types are similar and convert any datatypes to string as necessary:
select id, (name + ' ' + surname + ' ' + convert(varchar(3),age)) as info from users
Works fine in most databases I know, although you probably have to CAST the age field to be TEXT. The exact method for doing this depends on the database you're using, which you did not specify.