Using Spring JDBCTemplate with Postgres to query ip addresses - postgresql-9.1

I have a table that has IP Addresses (v4) stored as varchar (I Cannot change this)...
I'm trying to query for a range of IP addresses... like this:
select colA, colB from table where cast(ipaddress as inet) >= ?
and I'm passing in to my PreparedStatement:
"'1.1.1.1'::inet"
I've also tried:
"cast('1.1.1.1' as inet)"
and
"inet '1.1.1.1'"
I get an error that my type is not correct for type inet.
I've also tried to create an InetAddress for the ip address and pass that in as my arguments which gives me a whole other error.
Has anyone else had this same problem and conquered it?

Related

Jupyter magic sql for Db2 %%sql invalid syntax error while assigning a variable

I get invalid syntax error on =%%sql while trying to assign a variable to the %%sql. Can a variable to assigned ? I’m trying to bring the result in a dataframe.
For eg:
X=%%sql
Select id, name , address from employees
Where age>18
The same works when x=%sql select age from employees
With details missing in your question, I would guess you want something like this:
%%sql myvariable <<
Select id, name , address from employees Where age>18
Look here under assignment: https://pypi.org/project/ipython-sql/

How to have multiple values per row

I've set up a database with sqlite using perl and I'm trying to figure out how to add multiple values on each row.
I've been trying to change my INSERT INTO statement but have had no success.
#Here I create the database.
$dbh->do("
CREATE TABLE probes(
source CHAR(15) NOT NULL,
port CHAR(5) NOT NULL,
PRIMARY KEY(source,port))")
#This is my prepare statement that I think needs to be changed.
my $sth = $dbh->prepare("INSERT INTO probes (source, port) VALUES(?,?)");
For example I have a log file that was taken from a scan done, I have a source IP and port number. I want the database to show
Source: Port:
127.0.0.1 5678 5839 5938
Instead of it showing like this:
Source: Port:
127.0.0.1 5678
127.0.0.1 5839
127.0.0.1 5938
You store one row per scan like you are now, and use grouping and aggregation to get one row per source IP when you're ready to display the data. Something like:
SELECT source, group_concat(port, ' ') AS ports
FROM probes
GROUP BY source;
db<>fiddle example

BizTalk 2013 WCF-SQL adapter to insert into decimal(38,20)

I am having problems inserting values into SQL Server columns of type decimal(38, 20) from BizTalk 2013 using WCF-SQL adapter. I get InvalidCastException with message: "System.InvalidCastException: Specified cast is not valid"
If I test column type decimal(18,18) it works.
Seems like the WCF-SQL adapter does not handle decimal with very high precision. Question is what is the limitation? And, if there is a workaround?
When I generate XSD from database table information, decimal(38,20) turns into xs:string with length restriction of 40. Maybe this is a sign of that WCF-SQL adapter cannot handle such precision...? I have also tested to alter the XSD to be xs:decimal, but no difference.
Anyone?
ADDITION:
Did not find any "good" way to handle this limitation.
Final setup is: XML => WCF-SQL adapter => Stored Procedure with table type parameter containing varchar(40) columns => CAST table variable columns to decimal(38,20) one-by-one => INSERT into destination table.
So, solution was to modify table type to accept varchar, and manually convert in stored procedure.
Would be happy if someone could explain the better solution!
Decimal precision is limited to the .NET framework type. See here.
Also described in the BizTalk documentation here. "Decimal if precision <= 28. String if precision > 28".
So your way of handling with strings is an option. Use the Round functoid in your map to the SQL schema if you don't really need more than 29 positions.
Another option you could consider is changing the regional settings for the BizTalk host user running the send port. The current setting/language of your decimal separator is a comma instead of a dot (or the other way around) and not matching the data type for SQL Server. For this option you have to keep the type as string in your schema and keep it decimal in your SQL Server table.

sql equivalent to "grep x | head -n1"

I'm beginning to learn sql and have created an sqlite database of what was previously a text file of my daily ip address assignments from Comcast. If I wanted to find the first date that an ip address was assigned with the text file, I could:
cat, awk, sort, for/do, grep and head -n1
to get a list of the first dates any particular ip address was assigned. How can I do that with sql?
select distinct ip from history;
does not display the date column, and
select distinct ip, date from history;
returns all the db entries. What am I not doing? Thanks.
Your question is exactly a duplicate of this: SQL query to select distinct row with minimum value. You are trying to select the minimum value in the date column (the start date) for every unique IP. You will need to do an inner join and use group by.

Was it mandatory to put a "distinct" field as the first field in a query?

Just out of curiosity, looks like a distinct field must be placed ahead of any other fields, am I wrong?
See this example in SQLite,
sqlite> select ip, distinct code from parser; # syntax error?
Error: near "distinct": syntax error
sqlite> select distinct code, ip from parser; # works
Why is that? Do I really have a syntax error?
There is no such thing as a "distinct field".
distinct applies to all fields in the query and therefore must appear immediately after select.
In other words, select distinct code, ip is really
select distinct
code,
ip
rather than
select
distinct code,
ip
It selects all distinct pairs of (code, ip). Thus the result set could include repeated values of code (each with a different value of ip).
It is not possible to apply distinct to a single field in the way you're trying to (group by might be a useful alternative, but we need to understand what it is exactly that you're trying to achieve).

Resources