SQL Concatenate String in Result - sql

Let's say I have the following query:
SELECT anInteger FROM table;
How do I make that query concatenate a url on the front - so each row returned becomes:
'http://aurl.com/something?q=anInteger'
Note it must be the query itself that performs the concatenation - obviously in a situation where you are getting the results into a language you should concatenate in the language.

You would use something like:
SELECT 'http://aurl.com/something?q=' + cast(anInteger as varchar) FROM table;

It will depend on the RDBMS you are using:
MySQL:
SELECT concat(anInteger, " your string goes here") FROM table;
PostgreSQL:
SELECT anInteger || " your string goes here";
Oracle:
Same as PostgreSQL

Related

Regex to find a pattern using sql

I have a column called 'user_details' in a SQL table 'customers' with the below value in it:
{4:"2021-06-07T16:17:26.327+02:00",5:"1623075805735.phna3uyo",6:"www.abc.com/connexion",10:"loggedOut",12:"567879",2:"1026530505.1619610156",3:"event"}
{4:"2021-06-01T13:11:34.742+02:00",5:"1622545894742.ml2cyuw",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"",2:"435305774.1622545085",3:"event"}
{4:"2021-06-01T10:13:30.85+02:00",5:"1622535210085.vlowlxmj",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"278356",2:"1381684281.1622534907",3:"event"}
{4:"2021-06-01T10:24:51.808+02:00",5:"1622536405142.h45exkgg",6:"seigneuriegauthier.com/connexion",10:"loggedOut",12:"251666",2:"1019448131.1621925108",3:"event"}
{4:"2021-06-01T14:13:54.476+02:00",5:"1622551449049.k14838ij",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"601322",2:"1975087820.1622548509",3:"event"}
I'm trying to extract the id after number 12:" without the "" i.e. 567879,278356 etc into another column.
Since there are many reputative "" I'm unable to build the regex expression.
I tried the below but didn't get the exact match
(?<=:)"[0-9][0-9][0-9][0-9][0-9][0-9]
How can I write a SQL query to retrieve this. Pls help.
On SQL Server, with no regex, support, we can try using the base string functions SUBSTRING along with CHARINDEX:
SELECT
user_details,
SUBSTRING(user_details,
CHARINDEX('12:"', user_details) + 4,
CHARINDEX('"', user_details, CHARINDEX('12:"', user_details) + 4) -
CHARINDEX('12:"', user_details) - 4) AS val
FROM customers;
Demo

How to convert this SAS code to SQL Server code?

SAS CODE:
data table1;
set table2;
_sep1 = findc(policynum,'/&,');
_count1 = countc(policynum,'/&,');
_sep2 = findc(policynum,'-');
_count2 = countc(policynum,'-');
_sep3 = findc(policynum,'_*');
_count3 = countc(policynum,'_*');
How can I convert this into a select statement like below:
select
*,
/*Code converted to SQL from above*/
from table2
For example I tried the below code:
select
*,
charindex('/&,',policynum) as _sep1,
LEN(policynum) - LEN(REPLACE(policynum,'/&,','')) as _count1
from table2
But I got a ERROR 42S02: Function 'CHARINDEX(UNKNOWN, VARCHAR)' does not exist. Unable to identify a function that satisfies that given argument types. You may need to add explicit typecasts.
Please note that the variable pol_no is: 'character varying(50) not null'.
I am running this on using Aginity Workbench for Netezza. I believe this is IBM.
Assuming Oracle based on CHARINDEX() this may work:
You need to apply it twice, once for each character and take the minimum to find the first occurrence.
There may be a better suited function within Oracle, but I don't know enough to suggest one.
select
*,
min(charindex('/',policynum), charindex('&', policynum)) as _sep1
from table2
EDIT: based on OP notes.
Netezza seems like IBM which means use the INSTR function, not CHARINDEX.
select
*,
min(instr(policynum, '/'), instr(policynum, '&')) as _sep1
from table2
https://www.ibm.com/support/knowledgecenter/en/SSGU8G_12.1.0/com.ibm.sqls.doc/ids_sqs_2336.htm
FINDC & COUNTC functions are basically used for searching a character & counting them.
You can use LIKE operator from SQL to find characters with '%' and '_' wildcards
e.g. -
SELECT * FROM <table_name> WHERE <column_name> LIKE '%-%';
and
SELECT COUNT(*) FROM <table_name> WHERE <column_name> LIKE '%-%';
You can use regular expressions in the LIKE operator as well

Compare Strings using SQL and use it as an IF Condition to execute an SQL Query

I have two Strings in Java.
String from = "string1";
String upto = "string2";
Now I compare these Strings the way Java does and do the following
Something like this,
Cursor cursor;
if(from.compareTo(upto) > 0){
cursor = database.execSql("SELECT * FROM table WHERE name BETWEEN '" + from + "' AND '"+ upto+"' OR b.name GLOB '"+upto+"*');
} else {
cursor = database.execSql("SELECT * FROM table WHERE name BETWEEN '" + from + "' AND '"+ upto+"');
}
But I desire to compare the Strings from and upto in the way Sql does unlike how Java does and use the appropriate query.
Does anyone have idea on this?
Or, is there a way to construct a single sql query to do that?
SELECT *
FROM
table
WHERE
name BETWEEN from AND upto
OR (from = upto
AND b.name GLOB upto)
Your question is a little cryptic so I am not positive I fully understand it but it seems to merge your statements you just need to add the or and as part of that add from = upto and your other condition.
I will assume you can add the appropriate tick marks etc to build this in your code. hope it gives you an idea of how to do this.
In this case 1 will be returned:
select 1 from dual where 'asd' = 'asd'
If one of those string values was different, 1 would not be returned.
So, as far as I understood, if you create a SQL statement like:
select * from table where from = upto and ...
The query will return nothing if this condition is not valid.

Because the IN clause () SQL Server does not work this way

see if someone can get me this question:
This is my SQL query which loads into a temporary table for which to consult posterirormente there all goes well:
DECLARE #listStr VARCHAR(MAX)
SELECT #listStr = COALESCE(#listStr+' ,' , '') + sCodProducto
FROM dbo.Productos WHERE sCodProducto IN (80063, 80061, 80067, 80062, 80065)
INSERT INTO #IDPROD2(CODIGO)
SELECT #listStr
if I make this a select shows me the following data:
SELECT * FROM #IDPROD2
Well, now if I consult so this brings me nothing:
SELECT * FROM dbo.Productos P WHERE P.sCodProducto IN (SELECT CODIGO FROM #IDPROD2)
now if it works this way:
SELECT * FROM dbo.Productos P WHERE P.sCodProducto IN (80061 ,80062 ,80063 ,80065 ,80067)
A field in a query result is considered a single VALUE. The actual contents of that field are irrelevant. Doesn't matter if you have a numbers in CSV format, or one single number - that entire chunk of data is one single VALUE, as far as the DB is concerned.
Since it's a single value, your codigo field's contents are parsed/executed as:
... WHERE foo IN (#codigo)
... WHERE foo IN ('1,2,3,4,...');
... WHERE foo = '1,2,3,4,....';
The DB will NOT parse those values, and therefore will NOT treat string as multiple distinct values.
If you want the contents of a single field or variable to be treated as multiple distinct values, you have to use dynamic sql:
sql = "SELECT .... WHERE foo IN (" + #codigo + ")";
exec #sql;
Note that this is basically a form of SQL injection. You remove the "context" of being a single value from that variable field, and force the DB to treat it as multiple different values.
Some DBs get around this by providing extract functions, e.g. mysql's find_in_set, which is designed specifically for this:
SELECT ... WHERE FIND_IN_SET('80063', '80063, 80061, 80067, 80062, 80065');
There is no such function in TSQL, but can be simulated, even with a simple like query:
... WHERE foo='80063' OR foo LIKE '80063,%' OR foo LIKE '%,80063,%' OR foo LIKE '%,80063'

MS Access using VBA in a SQL IN clause

I'm trying to pass some values from a vba function to an SQL.
This is my SQL
SELECT *
FROM Hierarchy3
WHERE ID IN (getList("1 and 2"));
this is the definition of the vba function:
Function getList(Measure As String) As String
when I call the function I get: 1,2,3 as a String.
if I run the SQL as
SELECT *
FROM Hierarchy3
WHERE ID IN (1,2,3);
it works fine, but combining the two doesn't work. So I guess the String type is wrong, can you please help?
Your problem is that your query does not work like you expect it. Your function returns a string, so the query performed is:
SELECT *
FROM Hierarchy3
WHERE ID IN ("1,2,3");
Notice the quotation marks. Basically you are comparing an integer to a string and so doesn't return any results.
What you can do is use the INSTR function to see if the ID can be found in the string:
SELECT *
FROM Hierarchy3
WHERE INSTR(getList("1 and 2"),ID);
Now it will work because ID 1 can be found in the string "1,2,3" so the INSTR function will return the position where it can be found.