How to set date as a parameter when using Log Parser - logparser

When using Log Parser, it actually support pass parameter to a sql file. In my case, I want set the output file name to contain today's date in format of yyyyMMdd. Here's my sql file content:
SELECT RecordNumber, TimeGenerated
,EventId,EventType,EventTypeName,EventCategory,EventCategoryName
,SourceName,REPLACE_STR(Strings,'\u000d\u000a','.') AS Strings
,ComputerName,SID,REPLACE_STR(Message,',','.') AS Message,Data
INTO C:\EventLog\Security_%date%.csv
FROM Security
WHERE TimeGenerated > SYSTEM_DATE()
And I call log parser from Windows power shell like :
.\LogParser.exe -o:CSV file:sqlfile.sql?date=.....
what can I put after "date=" so that I can get the current date in the format of yyyyMMdd?

LogParser will automatically use values from your SELECT statement to replace wildcards (*) in the INTO statement. So try adding this as the first field in your SELECT statement
TO_STRING(SYSTEM_DATE(), 'yyyyMMdd') AS [DateForFileName],
and change your INTO statement to
INTO C:\EventLog\Security_*.csv
and you no longer need ?date=..... on the logparser.exe command.
/ Colin

Related

TO_DATE() is returning incorrect format than the format specified in Snowflake

I am trying to convert the date I have got using the GETDATE() function into YY-MON-DD format such as 04 Nov 2022. I have used TO_VARCHAR() to convert into the date returned by the GETDATE() into a string. The output is correct till the TO_VARCHAR() is used i.e. SELECT TO_VARCHAR(GET_DATE, 'DD-MON-YY') returns the desired format.
When I try to wrap it around TO_DATE() function; the date format changes into 2022-11-04.
SELECT TO_DATE(TO_CHAR(GETDATE(), 'DD-MON-YY'), 'DD-MON-YY')
How can I resolve the problem and correct the format!?
A date will display in the default format for your session. If you want to display it in a different format you would need to use the TO_CHAR(date_col, fmt) construct. So your select statement just needs to be:
SELECT TO_CHAR(GETDATE(), 'DD-MON-YY')
wrapping a TO_DATE round it doesn't achieve anything if all you are trying to do is display that column as an output of your SELECT statement
You need set the desired format at session level
alter session
set date_output_format='DD-MON-YYYY';
select getdate()::date --getdate() returns a timestamp not a date
select current_date() --alternatively
As Nick suggested, use the select to format dates instead of tinkering with session parameters

Azure Data Factory copy activity failed with Exception ERROR [22007] Timestamp is not recognized?

I am using Azure Data Factory to copy data from CSV to Snowflake, the copy executes fine but it has an error when it comes to copy Date from the CSV which has this value (14/01/2000), if the Date is (12/10/2000) or less, it works very well.
Here is the error message:
ErrorCode=UserErrorOdbcOperationFailed,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=ERROR [22007] Timestamp ‘14/01/2000’ is not recognized
I tried to adjust the format of the date in the copy activity to be dd/MM/yyyy or change the Culture to en-UK as the below image but it has the same issue.
I tried to use all the possible types of date in Snowflake as below but I still have the same issue:
DATE
DATETIME
TIMESTAMP
TIMESTAMP_LTZ
Snowflake doesn't supports format as DD/MM/YYYY and even it supports MM/DD/YYYY it can lead to incorrect dates (05/02/2013 could be interpreted as May 2, 2013 instead of February 5, 2013).
So this:
select '14/01/2000'::timestamp;
produces:
Timestamp '14/01/2000' is not recognized
while this:
select '01/14/2000'::timestamp;
produces:
2000-01-14 00:00:00.000
Same for:
select '14/01/2000'::date;
select '01/14/2000'::date;
The guidelines for how to use date/timestamp formats are described here.
In your case one way to get that value as a date is to use the to_date function, like this:
select to_date('14/01/2000', 'DD/MM/YYYY');
gives me:
2000-01-14

Microsoft SQL Server - Open Query where > date

I am trying to run a select using openquery with filtering result by date, but I have problem with using the date after where clause.
Ideally I would like to be able to pass a variable
set #d = dateadd(day, -30, getdate())
but for the sake of example I will try to use specified date :
Example:
select *
from OPENQUERY([Linked_Server], 'select id, name from Users where LastModifiedDate > ''2017-01-01''')
This returns an error:
INVALID_FIELD:
select id, name from Users where LastModifiedDate > '2017-01-01'
value of filter criterion for field 'LastModifiedDate' must be of type dateTime and should not be enclosed in quotes".
It works ok if I use for example istrue = true, but comparing dates seems to be the problem.
Can someone please advise me on this ?
It looks like you're querying a linked server that is not standard SQL Server but is instead Salesforce which uses SOQL which has a specific format for date and datetime literals. The correct format for date filters in Salesforce is:
WHERE LastModifiedDate > 2017-01-01T00:00:00Z
So your full SQL should be:
SELECT *
FROM OPENQUERY(
[Linked_Server],
'SELECT id, name FROM Users WHERE LastModifiedDate > 2017-01-01T00:00:00Z')
WE use a lot of Open queries here and we've stumbled into that kind of scenario. What we have done in the past is this:
CONVERT(VARCHAR(11),#d,101)
OR
CONVERT(VARCHAR(25),#d,126)
This already converts the date into the format DavidG posted there.
Also, to double check if the query is coming out correctly, we assign the query text into a variable and then we use the print to show the variable, that print which is just simple text that will show on your Message tab alongside your resultset tab. As long as the Where clause shows with only two single quotations, the query you have should work, just in case of doubt, copy the message and run it separately by replacing all doubled single quotations into one single quotation only.
What I had in my message tab on WHERE clause was something like that:
WHERE thisdate BETWEEN ''02/27/2017'' AND ''2017-02-27T23:59:59.990''

Using cognos report studio prompts in pass through SQL

I have created a report in Cognos Report Studio using pass through SQL syntax. My query includes a couple of common table expressions. How can i pass prompts to my query? I would like to use one optional date filter, which then is used in two cte:s. Then another required and multi choice text-filter that will be used in the final select statement.
Below is a simplified version of my query:
WITH in_date AS
(SELECT * FROM in_dates WHERE in_date > optional_date_prompt),
out_date AS
(SELECT * FROM out_dates WHERE out_date > optional_date_prompt),
organisation AS
(SELECT * FROM organisation)
-- some joins and unions later i end up with this table
SELECT * FROM final_table
WHERE organisation_name = 'required_text_prompt' OR
organisation_name = 'optional_text_prompt_value'
To use the prompts as regular cognos filters applied on the report page is not an option as the report would take hours to run.
To pass in parameters directly to SQL use a macro. A prompt macro for a string prompt called org would look like this:
#prompt('org','string')#
The first parameter is the prompt name and the second the type. This is the minimum amount of parameters that are needed to be specified. There are other optional parameters, such as default value that can be specified as well. You can consult the Cognos documention for more options.
You put the prompt macro in your WHERE clause:
WHERE organisation_name = #prompt('org','string')#
Cognos will see the macro and resolve it before sending the SQL on to the data source.

How to use XML data of SQL table column and perform tasks based on tag values

I need some help. I am suppose to get XML data from column of a table. The XML data has many tags. One of the tag contains values like
<Code date= 30/03/2013>
<apr id =1> -2 </apr>
<rdv id =2> 1 </rdv>
</code>
I need to run a particular task which checks the date. Like if today's date= (Code date -2) or
today's date = (code date + 1)
run a mailer task.
How should I go about it ? Please forgive me for the XML data format. I am naive in XML.
Create 2 variables to store the XML column Date value and other one to get the current date .To write an expression ,select the variable ,right click on it .In the properties windows Set EvaluateAsExpression =true and write the expression
Name DataType Expression
ExecDate DateTime
CurrentDate DateTime GETDATE()
Use Execute SQL Task .Set Resultset to Single Row and write the following code to read the date value from the XML column in your table
SELECT
convert(datetime,XMLData.value('(/Code/#date)[1]', 'varchar(10)'),103) as PkgDate
from yourTable
Demo in SQL Fiddle
In the resultset tab map the output from the above sql query with the variable ExecDate
Result Name Variable Name
PkgDate ExecDate
Put another Task or component Ex SEND Mail TASK after Execute SQL Task and write an expression in the Precedence Constraint.
Expression : #ExecDate==#CurrentDate
Only when the Date value from the XML column matches with the Current Date which is stored in the variable #CurrentDate then only the other components after Execute SQL Task will execute .
today's date= (Code date -2) or today's date = (code date + 1) run a mailer task.
In order to perform the above expression ,you need to change the sql code in Execute SQL Task
To get CodeDate +1
SELECT
Dateadd(day,1,convert(datetime,XMLData.value('(/Code/#date)[1]', 'varchar(10)'),103)) as PkgDate
from yourTable
I don't have the SSIS environment to test it now but this is how you should be doing it

Resources