Currently I'm trying to fix the following 2 warnings with the the following format.
+ start: `2015-05-05T12:30:00` - (optional, datetime) The start datetime (ISO8601 format)
+ Default: `0001-01-01-T12:00:00+00:00`
+ end: `2015-05-06T15:20:12` - (optional, datetime) The end datetime (ISO8601 format)
+ Default: `Utc Now`
My warning are:
Specifying parameter 'start' as required supersedes its default value, declare the parameter as 'optional' to specify its default value.
Specifying parameter 'end' as required supersedes its default value, declare the parameter as 'optional' to specify its default value.
Found the 2 mistakes the type was specified after the - and datetime is not a valid type.
correct format
+ start: `2015-05-05T12:30:00` (optional, string) - The start datetime (ISO8601 format)
+ Default: `0001-01-01-T12:00:00+00:00`
+ end: `2015-05-06T15:20:12` (optional, datetime) - The end datetime (ISO8601 format)
+ Default: `Utc Now`
Related
I have two connections to different databases. Two dates come from them. Through the tMap component, I want to compare these dates according to the condition that if hive_data > postgres_data or postgres_data is null, run the bash script through the tSystem component.
https://prnt.sc/Th3Cr1PoxNaa - job
https://prnt.sc/95HsXYnjzdyB - tMap expression comparison
If you want to compare date, use TalendDate.compareDate() .
The TalendDate.compareDate function in Talend is used to compare two dates and returns an integer value indicating their relative order. The function takes two arguments, both of which should be in the format of java.util.Date objects. The function will return 0 if the two dates are equal, a positive integer if the first date is after the second date, and a negative integer if the first date is before the second date.
Here is an example of how to use the TalendDate.compareDate function in a Talend Job:
// Define two date variables
java.util.Date date1 = TalendDate.parseDate("yyyy-MM-dd", "2022-01-01");
java.util.Date date2 = TalendDate.parseDate("yyyy-MM-dd", "2022-12-31");
// Compare the two dates
int comparison = TalendDate.compareDate(date1, date2);
// Check the result of the comparison
if (comparison < 0) {
System.out.println(date1 + " is before " + date2);
} else if (comparison == 0) {
System.out.println(date1 + " is equal to " + date2);
} else {
System.out.println(date1 + " is after " + date2);
}
I have a column type time(7).
What i want is to add in time column to date.
i manage to get the only date using GETDATE() function but i fail to add the time part next to date.
Query:
SELECT [Compay]
,[Time]
,CAST(GETDATE() AS DATE) AS Today
,CAST(CAST(GETDATE() AS DATE) AS NVARCHAR) AS Today_AS_Nvarchar
,CAST([Time] AS NVARCHAR) AS Time_AS_Nvarchar
,CAST(CAST(GETDATE() AS DATE) AS NVARCHAR) + ' ' + CAST([Time] AS NVARCHAR) AS Today_Time_AS_Nvarchar
,CONVERT(datetime,CAST(CAST(GETDATE() AS DATE) AS NVARCHAR) + ' ' + CAST([Time] AS NVARCHAR),103)
FROM [Testing_Env].[dbo].[Com_CD_Test]
Error:
Conversion failed when converting date and/or time from character string.
The error arise on CONVERT(datetime,CAST(CAST(GETDATE() AS DATE) AS NVARCHAR) + ' ' + CAST([Time] AS NVARCHAR),103)
is there any easier/orthodox way to achieve it?
You can't add the new date and time data types together like you can the old data types; personally I think this is also better as it stop people treating dates and times like a numerical value.
Assuming you have a date column and a time column you have a few of options. The first is to CAST/CONVERT both to a datetime and then "add" them together. Because the "old" data types work more like numerical values this works, however, you will lose accuracy of your time value if it has a precision of 3 or higher (as datetime is only accurate to 1/300 seconds):
DECLARE #TimeValue time(7) = '17:52:12.1234567',
#DateValue date = '20211016';
SELECT CONVERT(datetime, #DateValue) + CONVERT(datetime, #TimeValue);
If loosing accuracy isn't an option, then you could to use conversion on the date value and use DATEDIFF and DATEADD. For a time(7) you'll want to be using nanoseconds (as microseconds isn't accurate enough). Unfortunately this poses another problem; DATEADD can't handle bigint values (still) and there is no DATEADD_BIG (like there is DATEDIFF_BIG), so this becomes overly complex. You need to first get the difference in milliseconds, and then add the remainder in nanoseconds to still be accurate to 1/1000000 of a second:
DECLARE #TimeValue time(7) = '17:52:12.1234567',
#DateValue date = '20211016';
SELECT DATEADD(NANOSECOND,DATEDIFF_BIG(NANOSECOND,'00:00:00', #TimeValue) % 1000000,DATEADD(MILLISECOND, DATEDIFF_BIG(MILLISECOND,'00:00:00', #TimeValue), CONVERT(datetime2(7),#DateValue)));
Finally, yes, you can convert to values to strings, and then to a datetime2 value; this is probably the easiest methiod. You just need to ensure you use style codes:
DECLARE #TimeValue time(7) = '17:52:12.1234567',
#DateValue date = '20211016';
SELECT CONVERT(datetime2(7),CONVERT(varchar(10), #DateValue, 23) + 'T' + CONVERT(varchar(17), #TimeValue, 114),126);
I´m trying to pase the next String using LocalDateTime, but I always get de unparsed text found error:
Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10
Here is my String: convertDate: '2016-08-18 14:27:15.103+02'
And my code:
public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException {
LocalDate dateTime =LocalDate.parse(convertDate);
return dateTime;
}
I guess is not too complicated, buy I´m not able to see the error. Could the +02 in the String be the cause?
tl;dr
OffsetDateTime odt = OffsetDateTime.parse ( "2016-08-18 14:27:15.103+02" , DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" ) ) ;
Details
The Answer by greg-449 is correct about the problem (using a date-only object for a date-time value) but not the solution.
That Answer uses LocalDateTime which unnecessarily throws away valuable information about the offset-from-UTC. A LocalDateTime does not represent a specific moment on the timeline, only a vague idea about possible moments depending on adjusting into a particular time zone.
The +02 is an offset-from-UTC meaning “two hours ahead of UTC”. So in UTC the time-of-day for this simultaneous moment is 12 hours, 2 hours less than your 14 hours. This does represent a specific moment on the timeline. This offset is the valuable information you are throwing away with a LocalDateTime rather than an OffsetDateTime.
The format of your string is in SQL format, which is close to standard ISO 8601 format. Merely replace the SPACE in the middle with a T. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.
String input = "2016-08-18 14:27:15.103+02";
String inputModified = input.replace ( " " , "T" );
Unfortunately, Java 8 has a bug in parsing offset values abbreviated to just an hour or offset values omitting the colon between hours and minutes. Fixed in Java 9. But in Java 8, we need to adjust the input.
// Workaround for Java 8 where 2-digit offset fails parsing. Fixed in Java 9.
int lengthOfAbbreviatedOffset = 3;
if ( inputModified.indexOf ( "+" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
if ( inputModified.indexOf ( "-" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
Now parse.
OffsetDateTime odt = OffsetDateTime.parse ( inputModified );
Dump to console. Note how we transformed +02 into +02:00.
System.out.println ( "input: " + input + " | inputModified: " + inputModified + " | odt: " + odt );
input: 2016-08-18 14:27:15.103+02 | inputModified: 2016-08-18T14:27:15.103+02:00 | odt: 2016-08-18T14:27:15.103+02:00
Alternatively, specify a formatting pattern. The offset-parsing bug does not bite when using this formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
Database
Coming from Postgres, you should be retrieving the value as a date-time object rather than a String.
If your JDBC driver complies with JDBC 4.2 you can call ResultSet::getObject to get an Instant or OffsetDateTime. If not, call ResultSet::getTimestamp to get a java.sql.Timestamp, then immediately convert to java.time by calling toInstant on the Timestamp object.
Stick with java.time for your business logic; use the java.sql types briefly and only for exchange with the database.
Your code is using LocalDate which only parses a date - not a date and time so you are getting an error when the parse finds the space after the date.
So you should be using LocalDateTime but LocalDateTime.parse(String) expects an ISO format date which is not the format you are using.
So you need to use a DateTimeFormatter to specify the format of your input string. Something like:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");
LocalDateTime result = LocalDateTime.parse(convertDate, format);
What is the equivalent expression in SSRS of the following conversion of a date (#Date) in T-SQL?
CONVERT(INT,CONVERT(CHAR,#Date,112))
I need the date parameter value to be converted to an integer in YYYYMMDD format.
Assuming you have a date parameter called YourDate.
You could use the following expression:
=Cint(Format(Parameters!YourDate.Value, "yyyyMMdd"))
Explanation:
Step 1: Format the date to the yyyyMMdd format:
Format(Parameters!YourDate.Value, "yyyyMMdd")
Step 2: Cast the result as an int:
Cint(<FormattedDate>)
Dry coding here, but try...
=Fields!MyDateColumn.Value.Year * 10000 + Fields!MyDateColumn.Value.Month * 100 + Fields!MyDateColumn.Value.Date
Try the following expression :-
=format(cdate(Parameters!Date.Value),"yyyyMMdd")
XQuery has a set of useful functions for date conversion. But how to convert relative dates like "Today" and "Yesterday" into the actual date?
For example "Today, 17:33" should be converted to "2012-05-30", and "Yesterday, 22:13" to "2012-05-29".
1. Parse the Date string
Parse the date string. I provided a small function which splits the word indicating the date off and parses it. I added some more convenient names for dates, but you can easily add more if neccessary, notice I convert to lower-case! It uses XQuery date and time functions for calculating the matching date.
declare function local:from-relative-date($string as xs:string) as xs:date {
switch (lower-case(substring-before($string, ",")))
case "today" return current-date()
case "yesterday" return current-date() - xs:dayTimeDuration('P1D')
case "day before yesterday" return current-date() - 2 * xs:dayTimeDuration('P1D')
case "tomorrow" return current-date() + xs:dayTimeDuration('P1D')
case "day after tomorrow" return current-date() + 2 * xs:dayTimeDuration('P1D')
default return error(xs:QName("XPTY0004"), "Unknown Date")
};
2. Format the date
Now use the XQuery 3.0 function format-date(...) (I hope your XQuery engine supports it, BaseX which I used for this example does) to format the date string like you need it:
format-date(local:from-relative-date("Yesterday, 22:13"), "[Y]-[M00]-[D00]")
If it doesn't, you will have to use year-from-date(...) and according functions for month and day, use some number formatting and concatenate yourself.