How to change IP Address to other format? - sql

I need to include client's IP Address in signature code, by using this code to get client's IP Address:
SELECT client_net_address as IPAdd
FROM sys.dm_exec_connections
WHERE session_id = ##SPID
I can now get the IP Address as:
Ex: 192.168.1.24
How can i reformat it to something like this:
192168001024
I know we can do something like this in C# by using Split, PadLeft and Concat. But is it possible to do it in SQL Server too?
Thank You.

You have to write a function and can call from the query, there is no direct method to convert such notation transformation.

Related

Duplicate quotes in a postgres string

I have a function that generates a new user in the database.
I would like to check if the email address contains any quotes and if it does, I would like to duplicate them.
For example, I have the following email address: test.o'test#test.com and I would like to transform it into test.o''test#test.com.
Could anybody help me with this?
Thank you
Assuming you expect only one single quote (and not double or more), you could try using a simple replace:
UPDATE yourTable
SET email = REPLACE(email, '''', '''''');

How can you run a query in BigQuery to match logs where the callerIP is not within a certain list of IP ranges?

I am trying to run the following query in BigQuery - I am confident that the rest of the query is correct however the only issue is how I have listed the IP addresses in the line below. I am being presented with the error message "Encountered " <FLOATING_LITERAL> "x.y "" at line 17, column 62" ( where x and y are integers)
  AND NOT (protopayload_auditlog.requestMetadata.callerIp : "IP-RANGE IP-RANGE IP-RANGE IP-RANGE")
Where IP-RANGE is of course of the form x.y.z.f-x.y.z.e
I am not sure how to format this string of IP addresses to make the query work. Would be really grateful for some assistance!
The below, is the full query;
#legacySQL
SELECT
  protopayload_auditlog.authenticationInfo.principalEmail AS principalEmail,
  resource.labels.project_id AS project_id,
  resource.labels.bucket_name AS bucket_name,
  resource.labels.method AS method,
  protopayload_auditlog.requestMetadata.callerIp AS callerIp,
  timestamp AS timestamp
FROM (TABLE_DATE_RANGE([projectid.organisation_audit_logging.cloudaudit_googleapis_com_data_access_],
      DATE_ADD(CURRENT_TIMESTAMP(), -40, 'DAY'),
      DATE_ADD(CURRENT_TIMESTAMP(), -38, 'DAY')))
WHERE
  protopayload_auditlog.serviceName = "storage.googleapis.com"
  AND resource.labels.method = 'google.storage.objects.get'
  AND REGEXP_MATCH(protopayload_auditlog.authenticationInfo.principalEmail, r"^.*#mycompany\.com")
  AND NOT (protopayload_auditlog.requestMetadata.callerIp : "IP-RANGE IP-RANGE IP-RANGE IP-RANGE")
LIMIT 500
you can employ below approach as a starting point - you can wrap up all lengthy stuff into into UDF to make it compact and readable
AND NOT net.ipv4_to_int64(net.safe_ip_from_string(protopayload_auditlog.requestMetadata.callerIp)) between net.ipv4_to_int64(net.safe_ip_from_string(ip_range1_start)) and net.ipv4_to_int64(net.safe_ip_from_string(ip_range1_end))
AND NOT net.ipv4_to_int64(net.safe_ip_from_string(protopayload_auditlog.requestMetadata.callerIp)) between net.ipv4_to_int64(net.safe_ip_from_string(ip_range2_start)) and net.ipv4_to_int64(net.safe_ip_from_string(ip_range2_end))
...

how to get rid of queries in a URL using Hive?

I have a few million urls that can look like:
www.wikipedia.com/helloworld?somekey=published_links&otherkey=1
www.wikipedia.com/helloworld?wowkey=20005
www.wikipedia.com/helloworld
I would like to get rid of the url queries so that they all look like:
www.wikipedia.com/helloworld
How can I do that? Is it safe to do it with regex? Should I use parse_url instead (Hive)?
Thanks!
You can use parse_url function with a concatenation of http:// or https:// to the existing column and get the HOST and PATH values concatenate them to get the desired result.
select CONCAT(parse_url(concat('http://',col),'HOST'),
parse_url(concat('http://',col),'PATH')
)
from tbl

How to call SQL queries with multiple variable in the localhost server?

I have a java function which performs a SQL query:
SELECT usernum, username, uniquenum
FROM table
WHERE username = ? AND uniqenum = ?
In my localhost I can call the similar function with one variable as http://localhost:8080/listUsers?uniqenum=5 but how can I call a function with two variables?
I have tried http://localhost:8080/listUsers?username="John"&uniqenum=5 but it doesn't work.
Assuming that your Java code is set up to take GET requests properly, all you really need to do is remove the double quotes.
http://localhost:8080/listUsers?username=John&uniqenum=5

set up way of getting mysite.$domain

I have several domains, only one website and one databse table for each domain.
example: wbesite.us - data from USA goes to database table main_usa
wbesite.co.uk - data form UK goes to database table main_uk
Only have one database with name of the website. Having only one website structured and having variables like this:
$sql="select * from main_".$countrycode." where bla..bla...
and many other variables to catch the domain extension, and so on...
Now, instead of having one full website for each domain, how can set a script and wher do I put it in order to detect the domain that the user uses.
In my server root do I create something like website.$domain?
Something like website OLX but for different purposes.
I hope I made myself clear.
Thank you.
You could use the $_SERVER['SERVER_NAME'] global and somehow parse it, to get the country code.
I didn't try it, but something like this should work:
$servername_array = explode('.', $_SERVER['SERVER_NAME']);
$country_code = array_pop($servername_array);
That way, $country_code will be com, uk, or whatever is after the last dot in the domain name.