I can't find any documentation on the web whether there is something like INET_ATON in AWS Redshift. So I guess it isn't there yet but I am wondering if there can be a workaround in some way. BTW I am using max_mind data.
For a workaround you can use:
SELECT ipAddr,
SPLIT_PART(ipAddr, '.', 1)* 16777216::bigint +
SPLIT_PART(ipAddr, '.', 2)* 65536::bigint +
SPLIT_PART(ipAddr, '.', 3)* 256::bigint +
SPLIT_PART(ipAddr, '.', 4)::bigint AS addressRange
FROM <your_table) LIMIT 5
addressRange should match to maxmind startIpNum < addressRange < endIpNum.
I ended up creating a wrapper in Python (which is the main language I am using) which translates IP string into a number and then use it.
I loaded the country blocks and locations CSVs in homonymous tables and joined with the following query
INSERT INTO dim.geoip_country
SELECT
SPLIT_PART(first_ip, '.', 1) * 16777216::BIGINT
+ SPLIT_PART(first_ip, '.', 2) * 65536::BIGINT
+ SPLIT_PART(first_ip, '.', 3) * 256::BIGINT
+ SPLIT_PART(first_ip, '.', 4)::BIGINT AS ip_inf,
SPLIT_PART(first_ip, '.', 1) * 16777216::BIGINT
+ SPLIT_PART(first_ip, '.', 2) * 65536::BIGINT
+ SPLIT_PART(first_ip, '.', 3) * 256::BIGINT
+ SPLIT_PART(first_ip, '.', 4)::BIGINT
+ POW(2, 32 - mask_bits)::BIGINT AS ip_sup,
network,
isocode2,
name,
continent_code,
continent_name,
is_anonymous_proxy,
is_satellite_provider
FROM (
SELECT
b.network,
SPLIT_PART(b.network, '/', 1) AS first_ip,
SPLIT_PART(b.network, '/', 2)::INTEGER AS mask_bits,
l.country_iso_code AS isocode2,
l.country_name AS name,
l.continent_code AS continent_code,
l.continent_name AS continent_name,
b.is_anonymous_proxy::BOOLEAN AS is_anonymous_proxy,
b.is_satellite_provider::BOOLEAN AS is_satellite_provider
FROM ext.geoip2_country_blocks_ipv4 b
JOIN ext.geoip2_country_locations_en l
ON b.geoname_id = l.geoname_id
)
Related
I have to develop system which is accept data (in database) from another system. The problem I face is all DATE and TIME data is in NVARCHAR. I have to convert to DATE and TIME in order to calculate the duration. I am using SQL Server 2008 R2.
Data example:
STR_YMD STR_HMS
--------------------
20150101 151000
20090807 123009
20130113 145602
20090515 061700
How could I convert this data into following DATETIME data?
STR_DT
--------------------
2015-01-01 15:10:00
2009-08-07 12:30:09
2013-01-13 14:56:02
2009-05-15 06:17:00
Hopefully someone could help me. Thanks in advance
Bet
Something like this ?
SELECT CAST(SUBSTRING(STR_YMD, 1, 4) + '/' + SUBSTRING(STR_YMD, 5, 2) + '/' +
SUBSTRING(STR_YMD, 7, 2) + ' ' + SUBSTRING(STR_HMS, 1, 2) + ':' +
SUBSTRING(STR_HMS, 3, 2) + ':' + SUBSTRING(STR_HMS, 5, 2) AS DateTime)
EDIT : You can use LEFT function to be compatible with short time formats :
SELECT CAST(SUBSTRING(STR_YMD, 1, 4) + '/' + SUBSTRING(STR_YMD, 5, 2) + '/' +
SUBSTRING(STR_YMD, 7, 2) + ' ' + SUBSTRING(LEFT(STR_HMS+'000000', 6), 1, 2) + ':' +
SUBSTRING(LEFT(STR_HMS+'000000', 6), 3, 2) + ':' +
SUBSTRING(LEFT(STR_HMS+'000000', 6), 5, 2) AS DateTime)
I have a query/view that is created to be used in a report. When I run in the SMS and in SSRS it run fine. But when I connect the view to the tool that generates our reports it throws the following error. Incorrect syntax near '.8'. When I contact the support for this product they say it has to do with how we calculate the 8thgradyear . I have placed the code below. Any suggestions.
SELECT
dbo.studemo.suniq,
dbo.studemo.ident,
dbo.studemo.lastname,
dbo.studemo.firstname,
dbo.studemo.emailaddr AS stuemail,
dbo.studemo.birthdate,
dbo.track.schoolc,
dbo.school.schname,
dbo.stustat.graden,
dbo.stustat.edate,
dbo.zstustat.descript AS status,
RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname AS advisor,
dbo.track.schyear,
SUM(8) - dbo.stustat.graden + dbo.track.schyear AS [8thgradyear],
sf.Email, LOWER(sf.Username) AS [user],
LOWER(RIGHT(SUM(8) - dbo.stustat.graden + dbo.track.schyear, 2) + LEFT(dbo.studemo.firstname, 1) + REPLACE(REPLACE(REPLACE(dbo.studemo.lastname, '-', ''), ' ', ''), '''', '') + RIGHT(dbo.studemo.ident, 3)) AS newuser,
CONVERT(varchar(8), dbo.studemo.birthdate,1) AS password,
'STUDENTS' + '/' + (CASE WHEN track.schoolc IN ('19', '43', '17', '23') THEN 'Middle' ELSE 'Elementary' END) + '/' + dbo.school.schname AS neworg,
sf.OU, sf.LastLoginTime
FROM dbo.studemo INNER JOIN
dbo.stustat ON dbo.studemo.suniq = dbo.stustat.suniq INNER JOIN
dbo.track ON dbo.stustat.trkuniq = dbo.track.trkuniq INNER JOIN
dbo.zstustat ON dbo.stustat.stustatc = dbo.zstustat.stustatc INNER JOIN
dbo.facdemo ON dbo.stustat.funiq = dbo.facdemo.funiq LEFT OUTER JOIN
dbo.vw_google_OU AS sf ON sf.Firstname = dbo.studemo.firstname AND sf.Lastname = dbo.studemo.lastname INNER JOIN
dbo.school ON dbo.school.schoolc = dbo.track.schoolc
WHERE (dbo.stustat.stustatc IN
(SELECT stustatc
FROM dbo.zstustat AS zstustat_1
WHERE (snstatus IN ('A', 'M', 'P')))) AND (dbo.stustat.xdate IS NULL OR
dbo.stustat.xdate < dbo.stustat.edate) AND (dbo.track.schoolc NOT IN ('P34', 'P24', '802', '801'))
GROUP BY dbo.studemo.suniq, dbo.studemo.ident, dbo.studemo.lastname, dbo.studemo.firstname, dbo.studemo.birthdate, RIGHT(dbo.studemo.ident, 3), dbo.track.schoolc,
dbo.stustat.graden, dbo.zstustat.descript, RTRIM(dbo.facdemo.lastname) + ' ' + dbo.facdemo.firstname, dbo.stustat.edate, dbo.studemo.gradyear, dbo.track.schyear,
sf.Email, CONVERT(varchar(8), dbo.studemo.birthdate, 1), sf.Username, dbo.school.schname, sf.OU, dbo.studemo.emailaddr, sf.LastLoginTime
SUM function was introduced in SQL Server 2008 and your tool must be using and older version of SQL Server.
I have been using the following export query (CTE) for a few years but a client has requested that we do not give them all of the data from the 'C' line as they do not need some of the data. I have all of the PlaintiffNames that they do not need but I have no idea how to adjust the query to exclude the PlaintiffNames they do not need. And using NOT IN as is shown below has not worked and I am guessing I am missing something!
SELECT 4 AS grpOrd
, null AS posInGrp
, 'C'
, A.CaseNumberKey
, 'C|' + IsNull(J.CType, '') + '|' + IsNull(J.plaintiffName,'') + '|' + IsNull(J.plaintiffAdd1, '') + '|' + IsNull(J.plaintiffCity, '') + '|' + IsNull(J.plaintiffState, '') + '|' + IsNull(J.plaintiffZip, '') + '|' + '|' + IsNull(J.defendantName, '') + '|' + IsNull(J.defendantAdd1, '') + '|' + IsNull(J.defCity, '') + '|' + IsNull(J.defState, '') + '|' + IsNull(J.defZip, '') + '|' + '|' + IsNull(J.Court, '') + '|' + IsNull(J.CaseID, '') + '|' + IsNull(J.JAmt, '') + '|' + IsNull(replace(convert(VarChar(10), JDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(VARCHAR(10), revivedDate, 101), '/', ''), '') AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN Acme.new_judgment_system.dbo.selected_compiled_clean J
ON J.CaseNumber = A.CaseNumberKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd
AND ClientKey = 2
AND (J.plaintiffName NOT IN (SELECT Plaintiff FROM dbo.excluded_Plaintiffs))
I am getting invalid object name dbo.excluded_Plaintiffs when I try to run this. Note that is the very bottom of the CTE which has numerous lines but the issue is solely in this last line.
An example of a common plaintiff that they do not want is 'PHILA TRAFFIC COURT'
Is it possible to adjust the above to check the table (excluded_Plaintiffs) and exclude that data? I have tried to no avail and am hoping someone has the answer.
Well I answered my own question. I missed the database in this line dbo.excluded_Plaintiffs. Should be written newCityCollection.dbo.excluded_Plaintiffs.
I already have query to concatenate
DECLARE #ids VARCHAR(8000)
SELECT #ids = COALESCE(#ids + ', ', '') + concatenatedid
FROM #HH
but if I have to do it inline how can I do that? Any help please.
SELECT sum(quantity), COALESCE(#ids + ', ', '') + concatenatedid from #HH
Thanks.
Use the XML PATH trick. You may need a CAST
SELECT
SUBSTRING(
(
SELECT
',' + concatenatedid
FROM
#HH
FOR XML PATH ('')
)
, 2, 7999)
Also:
Join characters using SET BASED APPROACH (Sql Server 2005)
Subquery returned more than 1 value
I am developing a dynamic SQL using SQL Server 2008 T-sql code. So I want to return values that look like "Jan'11".
My code looks something like:
left(datename(month, SGD_SIGNOFF_DATE), 3) + ' + '''' +
' RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear
But this is not working. Either I get output to look like "Jan11" or I get error messages. What is proper syntax for my solution?
SELECT LEFT(datename(month, SGD_SIGNOFF_DATE), 3) + '''' + RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear
This should work:
left(datename(month, SGD_SIGNOFF_DATE), 3) + '''' + RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear
I got it! Here is the code now that works!
left(datename(month, SGD_SIGNOFF_DATE), 3) + '''''''' +
RIGHT(year(SGD_SIGNOFF_DATE), 2) AS MonthYear