how to pass date array as xml to store procedure - asp.net

Bcz when u parse that string into date that will not parse properly.
for that u have to send date string as per sql server date format
ALTER PROCEDURE [dbo].[SprSelectScheduleForReAutoAllocate] --'<AADates><AADate AADateValue="2011-04-27 00:00:00.000" /><AADate AADateValue="2011-04-28 00:00:00.000" /></AADates>'
(
#Datexml xml
)
AS
BEGIN
EXEC sp_xml_preparedocument #xmlDoc output ,#DateXml
SELECT AADateValue
FROM OPENXML(#xmlDoc,'AADates/AADate',1)
WITH
(
AADateValue Datetime
)
xmlDocuments
EXEC sp_xml_removedocument #xmlDoc
End

SQL Server reads date times values according to the ISO 8601 standard in the following two formats:
YYYY-MM-DDThh:mm:ss[.mmm]
YYYYMMDDThh:mm:ss[.mmm]
Format you input like this and SQL Server will never throw an error due to incompatible culture or language. This is the culture/language agnostic way to pass the value if you cannot pass it as an actualy datetime value.

Related

How to format an activity output as YYYY-MM-DD hh:mm:ss in Azure data factory

In my ADF pipeline I am trying to convert an output from my lookup activity to be in YYYY-MM-DD hh:mm:ss date format within the source query of a copy activity. The current output from my lookup activity is in YYYY-MM-DDThh:mm:ss format and I need to remove the 'T'.
I have tried using the dynamic content and formatDateTime functions but am having problems with the syntax. I am also using an SQL query to retrieve only the relevant data. The below is what I am
using as an input in the dynamic content query. I am able to get this to work, but I need to change '03/15/2018 12:00:00' to refer to the output of my lookup activity named LookupNewWaterMarkActivity.
SELECT *
FROM tableName
WHERE updatedDate >
'#{formatDateTime('03/15/2018 12:00:00', 'yyyy-MM-dd HH:mm:ss')}'
I have tried the below, but get the following error message:
'cannot fit package::output:any & { count, value } into the function parameter string. (6)'
SELECT *
FROM tableName
WHERE updatedDate >
'#{formatDateTime(activity('LookupNewWaterMarkActivity').output, 'yyyy-MM-dd HH:mm:ss')}'
Does anyone know how I can format the output of my activity within an SQL query any other way?
I am getting the below error when running the below code.
#concat('SELECT * FROM tableName WHERE sys_updated_on_value > ''',formatDateTime(activity('LookupNewWaterMarkActivity').output.value[0].sys_updated_on_value, 'yyyy-MM-dd HH:mm:ss'),'''')
Error code: FailToResolveParametersInExploratoryController
Details
The parameters and expression cannot be resolved for schema operations.
Error Message: { "message": "ErrorCode=InvalidTemplate,
ErrorMessage=The expression 'concat('SELECT * FROM tableName WHERE
sys_updated_on_value >
''',formatDateTime(activity('LookupNewWaterMarkActivity').output.value[0].sys_updated_on_value,
'yyyy-MM-dd HH:mm:ss'),'''')\n\n' cannot be evaluated because property
'value' doesn't exist, available properties are 'value[0]'.." }
Use the lookup activity output (activity('Lookup1').output.value[0].columnName) value in your expression with column name as shown below to refer the lookup activity output in later activities.
Lookup activity output:
Copy activity:
Expression:
#concat('SELECT * FROM tb2 WHERE date1 > ''',formatDateTime(activity('Lookup1').output.value[0].date1, 'yyyy-MM-dd HH:mm:ss'),'''')
Update:
If you have enables firstRow property in your lookup, use the below expression:
#concat('SELECT * FROM tb2 WHERE date1 > ''',formatDateTime(activity('Lookup1').output.firstRow.date1, 'yyyy-MM-dd HH:mm:ss'),'''')

Teradata SQL Assistant Date Inserts

Context is Teradata SQL Assistant
Successfully created the following table:
CREATE VOLATILE TABLE RSN_WEEKLY_TMP, NO LOG
(
EXPLICIT_DATE DATE FORMAT 'MM/DD/YYYY'
)
PRIMARY INDEX (EXPLICIT_DATE)
ON COMMIT PRESERVE ROWS;
1) The following INSERT works successfully:
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (CURRENT_DATE);
2) The following INSERT does not work and returns with Error:INSERT Failed [26665] Invalid date.
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (02/02/2016);
3) However, if I use a string 'date value' and CAST it as a Date it works.
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (CAST('02/03/2016' AS DATE FORMAT 'MM/DD/YYYY') );
I need to know how to make example #2 work? Please Advise?
02/02/2016 is an INTEGER calculation, dividing 2 by 2 by 2016, results in zero, of course this is not a valid date.
CAST('02/03/2016' AS DATE FORMAT 'MM/DD/YYYY') works because it tells the parser how to convert the string to a DATE.
The only recommended (and the shortest) way is a Standard SQL DATE literal:
DATE '2016-02-03'
You never need to think about formats because there's only one: YYYY-MM-DD
Actually, this format works too
insert into table_name (datecol) select '2015/12/31';
In your example:
CREATE VOLATILE TABLE RSN_WEEKLY_TMP, NO LOG
(
--EXPLICIT_DATE DATE FORMAT 'MM/DD/YYYY'
EXPLICIT_DATE DATE
)
PRIMARY INDEX (EXPLICIT_DATE)
ON COMMIT PRESERVE ROWS;
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) select '2016/02/02';

Capture only month and year (or null) - then convert that to a date

I'm currently working with a client that has a VB.NET web application that was developed internally. They've got everything storing to an Access database which they cannot alter or change for their own reasons. I'm not familiar with any of these technologies, so I'm hoping you may have a solution.
The client has a date field that they are only capturing mm/yyyy or blank. They need this information to save to a datetime field in the database. I'm trying to work up a statement that will automatically take the date entered and convert from mm/yyyy to mm/01/yyyy if the date is provided, or 01/01/1970 if the field was left blank. Can anyone assist?
If we are talking about MS Access functions DateSerial is what you are looking for. The basic syntax is below. If the stored value is text you will need to use the Mid function to parse the text into the year and month and you can use use a hard coded 1 for the day.
DateSerial ( year, month, day )
This function can be used in a select or update. Additional logic will be required to provide a default value for the blank result. Typically in Access this type of logic is done with an IIF.
You can use a combination of the IIf,IsNull and CDate functions, like so:
IIf(IsNull([YourDateFField]),#1/1/1970#,CDate([YourDateFField]))
This tests if your field is null and if yes it returns 1/1/1970, if no it will convert your date string to an actual date (e.g. CDate("04/2014") will return 4/1/2014)
This (MSAccess/VBA) function will do what you are asking. If you pass-in a string like mm/yyyy, it will return a datetime like mm/01/yyyy. However, if the string does not fit that pattern (or equiv), the function will return a date time of 1/1/1970, like you asked.
'in MSAccess:
Public Function mmyyyyToDate(mmyyyy As String) As Datetime
If IsDate(Replace(mmyyyy, "/", "/01/")) Then
Return CDate(Replace(mmyyyy, "/", "/01/"))
Else
Return #1/1/1970#
End If
End Function
It would be more efficient to run it in MSAccess, but if you want to run it in VB.net instead, the syntax is different:
'in VB.NET
Public Function mmyyyyToDate(mmyyyy As Object, Optional defaultDate As DateTime = "1/1/1970") As DateTime
Dim re As DateTime
If Convert.IsDbNull(mmyyyy)
return defaultDate
ElseIf DateTime.TryParse(Replace(mmyyyy, "/", "/01/"), re) Then
Return re
Else
Return defaultDate
End If
End Function
Example of running it:
'MSAccess query syntax
INSERT INTO NewDateTable (NewDateColumn)
SELECT mmyyyyToDate(oldColumn) FROM OldTable
If you can't add a new function to the MSAccess DB, you could turn this function into an inline statement (by using an IIF), but it looks pretty ugly:
'MSAccess query syntax
INSERT INTO NewDateTable (NewDateColumn)
SELECT IIF(IsDate(Replace(oldColumn, "/", "/01/")), Replace(mmyyyy, "/", "/01/"), #1/1/1970#)
FROM OldTable

SQL server convert 13 char long unix date strings into datetime data type

I have two dates as strings as below. They are in unix time format.
1364810610810+0530 and 1364830410810
I need to convert this to SQL server datetime type with SQL Server 2008 R2 and print the values on to console to see the milliseconds as well. Can someone help me with this.
I figured that 1364830410810' string can be converted as below. But it does not print milliseconds.
declare #myDate datetime;
set #myDate = DATEADD(SECOND, 1364830410810/1000 ,'1970/1/1');
print 'Second Statement: ' + Convert(varchar(100), #myDate );
You get the milliseconds by adding the third parameter, e.g. like this:
Convert(varchar(100), #myDate, 121)
See http://technet.microsoft.com/en-us/library/ms187928.aspx for details.
For the timezone have a look at the DATETIMEOFFSET datatype: http://technet.microsoft.com/en-us/library/bb630289.aspx

How to keep the military format when converting string to timestap in Oracle?

This is my code:
l_ts := to_timestamp((d || ' 13:00:00'), 'DD.MM.YYYY:HH24:MI:SS');
u_ts := to_timestamp((d || ' 17:00:00'), 'DD.MM.YYYY:HH24:MI:SS');
I keep getting:
26-MAY-13 01.00.00.000000 PM 26-MAY-13 05.00.00.000000 PM
I want:
26-MAY-13 13.00.00.000000 PM 26-MAY-13 17.00.00.000000 PM
i.e. 13 and 17 instead of 1 and 5.
Thanks for hrlping
You are converting string to date, and then display the date directly.
When You do it that way, the output is formatted to string internally and it depends on You session locale settings.
If You want to get the date in exactly the format You want, You should use TO_CHAR function that will format Your date back to string when You need it to be displayed to User. With TO_CHAR You can use format models to get Your date in any format You wish (try to do it in SQL server... ;).
Consider this script:
set serveroutput on;
DECLARE
l_ts TIMESTAMP;
BEGIN
-- Here You get DATE from STRING
l_ts := to_timestamp(('01.01.2013' || ' 13:00:00'), 'DD.MM.YYYY:HH24:MI:SS');
-- Here You get STRING representation of Your DATE.
-- Representation depends on You session settings,
-- and should not be relayed upon.
dbms_output.put_line(l_ts);
-- Examples how You can display DATE as STRING in any way You need
dbms_output.put_line(TO_CHAR(l_ts,'YYYY-MM-DD HH24:MI:SS'));
dbms_output.put_line(TO_CHAR(l_ts,'YYYY-MM-DD HH12:MI:SS AM'));
END;
Hope that will clear the issue. :)

Resources