Log parser: Using a text file as an input in WHERE clause - mysql

I am working on a batch script where I am parsing IIS logs using Log Parser.
So, I have 2 questions here:
Q1. What I want is, not to see the entries having username written in a text file. So, one can update that text file without worrying about the code syntax.
In other words, Instead of putting every username(could be around 30-50) in 'WHERE' clause using 'AND' again & again, I will have a text file having list of usernames.
Code Example:
"LogParser.exe" -i:csv "SELECT DISTINCT date, cs-username, clientun, cs-uri-stem FROM D:\temp.csv WHERE NOT cs-username IN ('NULL';'abc';'def';'hij';'and_so_on')" >D:\final_output.txt -o:NAT -rtp:-1
I was trying to do this via sub-query first, but it is not supported in Log parser it seems. I found OPENROWSET as a solution here but that doesn't seems to be working for me or may be I am not getting how to make it work.
Other solution I found were for SQL and were not working for log parser.
Q2. I want the logs to be read between the dates mentioned. So, I am taking a start and an end date from user in YYYY-MM-DD format and putting them in query
"LogParser.exe" -i:iisw3c "SELECT DISTINCT cs-username, REVERSEDNS(C-IP), date, cs-uri-stem, FROM \logs\location\* WHERE date BETWEEN %date_1% AND %Date_2%" >D:\temp.csv -o:csv
The error I get here is:
Error: WHERE clause: Semantic Error: left interval of BETWEEN operator ("'2016-02-15'") has a different type than operand ("date")
Please note, the output file generated from the Q2 is used as input in Q1.

Q1: Instead of filtering the data using logparser use findstr and the /g:file /v switches to filter input files or output lines (depending on the case)
Q2: Strings are not timestamps. Use
BETWEEN TO_TIMESTAMP('%date_1%','yyyy-MM-dd') AND TO_TIMESTAMP('%date_2%','yyyy-MM-dd')

Related

Syntax error in date in query expression for non-date fields

I'm having trouble building a query in Access 2013. The database isn't mine and the only thing I really have control over is this query. There is a table, I'm pulling 7 fields from it and eventually adding an 8th field to the query to do some string manipulation.
However, I keep getting getting "Syntax error in date in query expression 'fieldname'." error whenever I click on the arrow to sort the fields. The odd thing is these errors pop up when sorting non-date fields. When sorting the date field I get "Syntax error (missing operator) in query expression 'Release Date'."
This happens after a fresh build. I have no WHERE conditions, just SELECT and FROM. Ideas?
Here's the sql query, though I'm mainly working in the query design view:
SELECT Transmissions.[Job#], Transmissions.[Part#], Transmissions.TransmissionSN, Transmissions.Status, Transmissions.[Release Date], Transmissions.[Build Book Printed], Transmissions.[ID Tags Required]
FROM Transmissions;
Well... it seems you are the lucky inheritor of a poorly designed database.
Using special characters in a field name is just asking for trouble. And you've found what that trouble is.
Access uses the # sign to designate a Date type for query comparisons. Such as:
dtSomeDate = #2/20/2017#
You surround the date with the # signs.
In your case, the query thinks [Job#] and [Part#] are trying to wrap dates. But of course, that's not the case and thus it fails.
You can try a couple of work arounds. (I leave it to you to experiment.)
1) You can try to rename the problem fields within your query. So that:
Transmissions.[Job#] becomes Transmissions.[Job#] as JobNum
and
Transmissions.[Part#] becomes Transmissions.[Part#] as PartNum
2) You can try to copy the [Transmissions] table to a new table that you create
that does not have the naming problems.
3) Export the [Transmissions] table to a CSV file and re-import it to a new
table (or possibly new database) without the naming problems.
Here is a link to a microsoft article that tells you why to avoid special characters in Access:
Big Bad Special Chars.
Hope that puts you on the right track. :)
Typically, this means that the field names are missing or misspelled.
Try running this to see:
SELECT * FROM Transmissions;

SQL fulltext-search with CONTAINS raises geometry error

My php code generate a sql query as follow:
SELECT id, title, files, rating, DATE_FORMAT(date, '%d %M %Y') AS fancydate
FROM ArtArchive
WHERE CONTAINS (tags, 'Pepper')
ORDER BY date DESC
And it returns the error:
HY000 - 3055 - Geometry byte string must be little endian.
tags is a VARCHAR(255) indexed as FULLTEXT
I got the query to work by using : tags LIKE '%Pepper%', so I'm certain the CONTAINS is in cause.
I couldn't figure out what this error mean, nor what I have to do with geometry of all things: I'm simply trying to find words in a text.
After checking multiple examples, I'm pretty sure that I've been using the correct syntax. I also tried things such as : CONTAINS (tags, '"Pepper"'), CONTAINS (tags, 'Pepper'), CONTAINS (tags, "'Pepper'"), with no different result.
I'm trying not to use IN or LIKE because I intent to search multiple values at once in the tags field. I've toned it down to one value in the example until I get get it to work at all.
MySQL does NOT support CONTAINS() for text searches, as you would expect:
MySQL only recognizes the CONTAINS SQL function when dealing with
spatial data. It requires two graphics objects as arguments, and
returns a 1 or 0 depending on if the first object completely contains
the second. Designed as an implementation of the OpenGIS framework,
the MySQL CONTAINS function does not work on ordinary strings, and
will produce an error if you try it. MySQL only recognizes the LIKE
and STRCMP functions when dealing with string of information.
Found that info here.

Eliminate or translate HTML character in SQL import from XML file

I came across an issue when running a procedure that shreds an XML file and imports the data into SQL server.
It has been running successfully for a few months, but today I got an error:
Conversion failed when converting date and/or time from character
string.
This is the line where it fails
SELECT
ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))) AS StartDate
,ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))) AS EndDate
FROM #xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X)
When I looked at the XML file, I noticed that some of the dates were written like this:
01/12/2016&#160
This character &#160 is a Non-breaking space.
I would like to know if there is any way in SQL Server to account for these types of issues? For this specific problem, I can use REPLACE:
SELECT
REPLACE(ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))),'&#160','') AS StartDate
,ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))) AS EndDate
FROM #xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X)
but if other XML/HTML characters come up, is there a way to universally check for/deal with them?
I'd imagine you could create an auxiliary table of strings that you wish to replace/remove and join that to your query. This would be preferred over hard-coding each character, and would allow you to expand on the functionality easily. The caveat is that I'd expect it to slow down you query based on the number of characters you need to replace/remove.
SELECT
REPLACE(ltrim(rtrim(T.X.value('Cell[4]/Data[1]','varchar(max)'))),
StringsToRemove.string,'') AS StartDate,
REPLACE(ltrim(rtrim(T.X.value('Cell[5]/Data[1]','varchar(max)'))),
StringsToRemove.string, '') AS EndDate
FROM
#xml.nodes('/Workbook[1]/Worksheet1]/Table[1]/Row') as T(X),
StringsToRemove

MySQL define 1 result as text from SELECT query

Using: MySQL Workbench & MySQL for Excel
I have a number returned that is 12 digits long, however when imported in to excel using any manner this results in: 1.20523E+11.
I want (need) to specify that the value needs to be returned as text so to complete the query via MySQL for Excel without this error.
My existing query:
Select
-- Get Order data --
T5.orders_id As OID,
DATE_FORMAT(T5.date_purchased, '%Y-%m-%d') As ODate,
T3.products_name As PName,
T3.products_id As PID,
error:
DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s') As DecRepID,
-- This results in 1503070244
-- Transaction ID to match with Payments
I need the 1503070244 to return as TEXT so excel imports it without me needing to then convert the text.
I've looked at CONVERT & CAST however I can't get the query to work correctly & they resolve in to syntax errors.
CONVERT(VARCHAR(12),(DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s'))) As DecRepID,
CAST returns the correct values, however Excel doesn't recognise as text.
cast(DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s') as char) As DecRepID
I know it's something simple, however I can't find what is the cause of the error.
Is there a way to correct this through code, or am I stuck with post-formatting after import in to Excel?
Thank you.
Using the idea that Shahkalpesh had, you could do 2 different things:
1) You could try escaping the ' by using: '\''
concat('\'', DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s')) As DecRepID,
2) You could try escaping the ' by using: "'"
concat("'", DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s')) As DecRepID,
By escaping the ' the column will import in to Excel as: '1503070244 which is what you will need to have Excel recognise it as text to stop the shortened result.
You can read more here: http://dev.mysql.com/doc/refman/5.7/en/string-literals.html

mysql 0 terminated string showing up in XML as Unicode 0x0

In the process of moving some applications from PHP and Delphi to Java-RS some column entries are spoiling the show at this time.
After reading the data from mySQL with JPA into Java Pojos converting the result to XML using JaxB and trying to read back the result to JQuery / jqGrid a failure happens.
In the browser the problem simply shows at "not well formed".
Looking at the details with the Eclipse XML editor gives the error message:
An invalid XML character (Unicode 0x0) was found in the element content of the document
Now I'd like to proceeed and fix the original data.
How would an SQL query look like that looks for the rows that have
invalid entries?
How would an SQL query look like that fixes these
rows?
Let's assume the column with the problem is "name" in Table "Customer"
For question #1 I found:
SELECT name from customer where hex(name) like "%00";
to work. For question #2 I am assuming that update with a left substring might work. I am not sure about the length in this case. It looks like length(name) will return the length including the terminating zero character. Will the update with left(length(name)-1) work correctly in this case?
I am not sure whether backup and restore of the database would keep the current somewhat corrupted database in shape. So it would also be helpful to know how the situation can be reproduced with an insert statement that creates null terminated strings on purpose.
I think you should be able to transform the HEX() with something like
UPDATE customer SET name=UNHEX(REPLACE(HEX(name), '00', ''));
or simply
UPDATE customer SET name=REPLACE(name, CHAR(0), '');
update customer set name=left(name,length(name)-1) where hex(name) like "%00";