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

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

Related

SSIS - Set SQL query result to variable for Excel connection string

I want to set connection string using SQL Query result from my table. I need to import excel file from certain folder every month but i need to import only latest file name (FILE_YYYYMMDD)
Here is my table
I already follow some instruction using Execute SQL Task to get the result of the query and i want to assign the result as connection string to import excel file since the folder will get new file every month.
Here is my variable and my Result Set in Execute SQL Task
I want to make FilePath with max FileDate as Excel connection string in SSIS.
Is there anyway to do that?
Thanks
Your query can be like below :
CREATE TABLE MyTable(ID INT, FileName VARCHAR(255),FileDate DATE)
INSERT INTO MyTable(ID,FileName,FileDate) VALUES
(5,'C:\Users\Public\Documents\File_20200130.xls','2020-01-30'),
(5,'C:\Users\Public\Documents\File_20200131.xls','2020-01-31'),
(5,'C:\Users\Public\Documents\File_20220301.xls','2022-03-01')
SELECT FileName FROM MyTable
WHERE FileDate = (SELECT MAX(FileDate) FROM MyTable )
| FileName |
| :------------------------------------------ |
| C:\Users\Public\Documents\File_20220301.xls |
db<>fiddle here
Create a variable for the value we need to pass, the in the Execute SQL Task component, put the query mentioned above that returns the path with the max value of the date. Please make sure to set the result set to single row.
Then go to the variables tab and add the variable there.
To store the FileName as variable and use it as connection string for Excel source :
Select the Connection Manager, and in the Properties window, you will find the Expressions entry where you can use/concatenate the variable(s) in the ConnectionString entity.

Power Query data type conversion issue running SQL Server stored procedure

Latest version of Excel M365 and SQL Server 2017.
I have a stored procedure which takes a date as its input in format YYYY-MM-DD - currently the variable type is set to nvarchar(20), but I've also tried to resolve this issue by making it type Date.
I have a stored procedure that when run in T-SQL, looks like this:
EXECUTE [Database1].[dbo].[SP1]
#Date = '2020-10-01'
I'm using Power Query to pull the results of this into Excel. Here's what the generated M query looks like in Power Query:
= Sql.Database("contoso.database.windows.net", "Database1", [Query="EXECUTE [Database1].[dbo].[SP1]#(lf)#Date = '2020-10-01'"])
This runs and imports the data as expected.
However now I'm trying to derive the #Date value from a cell in Excel. I have followed a good tutorial for making the date from the Excel cell available to Power Query and I believe I have that setup correctly. I have named the parameter SDate, and set it as type text since this is what the stored procedure is expecting.
So now the M query looks like this:
= Sql.Database("contoso.database.windows.net", "Database1", [Query="EXECUTE [Database1].[dbo].[SP1]#(lf)#Date = SDate"])
When I run this, I get this error in Power Query:
Microsoft SQL: Conversion failed when converting date and/or time from
character string.
Two other things I tried:
create a literal string for SDate that's the date surrounded by single quotes i.e. '2020-10-01' - this gave the same error.
changed the stored procedure so the #Date variable is of type Date. Tested that by running the exact same T-SQL statement as above in SSMS, and the results return just the same as when it was nvarchar. So then in Power Query I ensured that the SDate parameter is of type date. Then I run the M query and this time I get the error "error converting type nvarchar to date."
One thing I found interesting is that after creating the aforementioned M query, if I go into settings of Source for the step that runs the query, the SQL Statement box shows this:
EXECUTE [Database1].[dbo].[SP1]
#Date = SDate
To me this looks like it's passing a literal SDate as the date to SQL Server, instead of the value of that parameter. I could be wrong there.
Any thoughts on getting this to run correctly?
The literal string works if you drop it in right. You need to exit the literal text inside the quotes and append or insert the string you want.
Instead of this:
[Query="EXECUTE [Database1].[dbo].[SP1] #Date = SDate"]
Try this:
[Query="EXECUTE [Database1].[dbo].[SP1] #Date = " & Sdate]
Where Sdate has been defined as the literal string '2020-10-01'.
If Sdate is a date, then use this:
[Query="EXECUTE [Database1].[dbo].[SP1]
#Date = '" & Date.ToText(Sdate, "yyyy-MM-dd") & "'"]

How can I select a date from a table and pass it in as a parameter in SSIS?

I have one single date in one table. I am trying to select this date and pass it into a function that pulls data from another system, based on this date parameter. I think it should be something like this.
Declare #dateCurrent as DateTime
Set #dateCurrent = MyDate
SELECT #dateCurrent = DATEADD(Day,-1,CONVERT(VARCHAR(6),#dateCurrent,112)+'01')
Select top 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', #dateCurrent)
I set 'MyDate' as a Variable. I'm not sure if this is the right way to do it. Or, should I simply select the date from the table, like this.
Select AsOfDate
FROM DATE_RAW_DATA
No matter what I try, I'm getting an error that SSIS can't execute the query because the date parameter isn't coming in. How can I get this setup correctly? Thanks.
First, create an Execute SQL Task that gets the date value from your table and saves it into your variable.
Next, create another Execute SQL Task that is connected after the first task, and then use your variable in the query. Assuming you have the date saved in a variable named MyDate, your query for the second SQL Task would look like this:
SELECT TOP 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', DATEADD(DAY,-1,CONVERT(VARCHAR(6),?,112)+'01'))
The question mark in the query means you're going to be using an input parameter for that value. So once you have the SQLStatement field value set, click on the "Parameter Mapping" option in the left pane of the Execute SQL Task Editor. For "Variable Name", select your variable from the drop down list, and for "Parameter Name" put 0 (which corresponds to the first question mark in the query). Make sure the variable data type and size are set correctly.

Get DateTime value from DB into a variable in SSIS

I'm taking the last updated date from SQL table as below:
SELECT CONVERT(DATETIME, [Value]) AS LastModifiedDate
FROM [WarehouseDB].[dbo].[tblWarehouseSettings]
WHERE Name = 'LastModifiedDate'
[Value] is varchar.
A variable as below:
I'm using an Execute SQL Task to get the date value & assign it to the variable. My Execute SQL Task Editor values set as below:
The task executed successfully but it doesn't get the value from the DB. Assigned value to the variable after the task execution is {12/30/1899 12:00:00 AM}
Can anyone figure out what I'm doing wrong here?
There are 2 things that caused this issue :
There is no need to specify #[User::LastUpdateOn] as an Output parameter in Parameters mapping Tab
Your SQL Statement is showing that you are converting [Value] column to DATETIMEOFFSET instead of DateTime and DT_DBTIMESTAMP is related to DateTime SQL Data Type
References
https://learn.microsoft.com/en-us/sql/integration-services/data-flow/integration-services-data-types
http://bidn.com/blogs/DevinKnight/ssis/1387/ssis-to-sql-server-data-type-translations

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''

Resources