Is there a way I can change a string value "01/01/20" to a date format of dd-mm-yyyy which is 01-01-2020 in SQLite? Or a way I can use strftime(%d%m%y,date) on a string value?
You can't use strftime() because this function operates only on dates of the format YYYY-MM-DD.
In your case you must use string functions and concatenation:
substr(date, 1, 2) || '-' || substr(date, 4, 2) || '-20' || substr(date, -2)
Note that it would be better to save the dates in the table with the format YYYY-MM-DD.
If you do then it is easy to format them as you want:
strftime('%d-%m-%Y', date)
Related
I'm receiving my dates in a MMddyyyy and a Mddyyyy format in one source, if the month has 1-9 digit the month is 1 digit, if the month is between 10-12 its 2 digits, I need an SSIS expression that will be able to convert them into a date format.
ex 2051994 or 12051994
both from the same source but they are hard to convert with the same SSIS expression
The cleanest way to do it would be to normalise the input format and then convert it to data as usual to the format of your choice. In order to normalise the input, i.e. make it the same length for every month, you can add a Derived Column transformation with this expression:
RIGHT("0" + yourinputdate,8)
if the input date is integer instead of string you can do this:
RIGHT("0" + (DT_STR,8,1252)(yourinputdate),8)
using the above expressions 12051994 will remain unchanged but 2051994 will be converted to 02051994.
from this transformation downstream you can convert the string to the format you prefer, or you can do it all in the same transformation although it would make the expression a bit unreadable.
You can use the following expression to convert Mddyyy and MMddyyyy string into DateTime value:
(DT_DATE)(RIGHT("0000" + RIGHT(RIGHT("0" + [DateColumn],8),4),4) +
"-" + RIGHT("00" + LEFT(RIGHT("0" + [DateColumn],8),2),2) +
"-" + RIGHT("00" + SUBSTRING(RIGHT("0" + [DateColumn],8),3,2),2))
I want to calculate last friday's date in ssis. Below code is doing it.
DATEADD("dd", -1 - (DATEPART("dw", getdate()) % 7), getdate())
But it gives results for datetime datatype and I want results for int data type in ssis.
How to convert?
DATEADD("dd", -1 - (DATEPART("dw", getdate()) % 7), getdate())
According to this Microsoft article:
When a string is cast to a DT_DATE, or vice versa, the locale of the transformation is used. However, the date is in the ISO format of YYYY-MM-DD, regardless of whether the locale preference uses the ISO format.
In order to convert value to integer yyyyMMdd,
First you have to convert the value to a string
Get the first 10 digit
Remove the - separator
Convert to integer.
You can use the following expression:
(DT_I4)REPLACE(SUBSTRING((DT_WSTR,50)DATEADD("dd", -1 - (DATEPART("dw", getdate()) % 7), getdate()),1,10),"-","")
I received timestamp datasets which are in the format of yyyy-mm-dd HH:MM:SS.ms. I want to convert into yyyy-mm-dd HH:MM:SS format. Is there any way to select only in this format using matlab?
For example:
2012-08-01 00:10:00.0
should be:
2012-08-01 00:10:00
Please note that the millisecond values are all zero.
The general way would be to use datestr to convert it to your desired format.
dates = {'2012-08-01 00:10:00.1';
'2012-08-01 00:10:00.1'};
new = datestr(dates, 'yyyy-mm-dd HH:MM:SS');
% 2012-08-01 00:10:00
% 2012-08-01 00:10:00
Another approach would be that since all of your milliseconds are going to be zero (therefore you don't have to worry about rounding) you can just use a regular expression to remove the milliseconds component (anything after the decimal point)
new = regexprep(dates, '\..*', '')
This is likely going to be more performant as you don't need to perform the intermediate step of converting to either a datetime object or a date number.
Since the input and output format are the same except for the milliseconds, don't use date functions, but simple string operations:
% example dates
C = {'2012-08-01 00:10:00.0'
'2013-08-02 00:11:11.0'
'2014-08-03 00:12:22.0'
'2015-08-04 00:13:33.0'
'2016-08-05 00:14:44.0'};
% method 1
D = cellfun(#(x)x(1:end-2), C, 'UniformOutput', false);
% method 2 (same, but no cellfun)
D = char(C);
D = cellstr(D(:,1:end-2));
% method 3
D = regexp(C, '[^\.]*', 'match', 'once');
% method 4
D = regexprep(C, '\..*$', '');
Lets say you need this data in datetime objects anyway then i would do something like this:
inp = {'2012-08-01 00:10:00.0'; '2012-08-02 04:10:00.0'}; % Some datestrins
t = datetime(inp,'InputFormat','yyyy-MM-dd HH:mm:ss.0'); % Convert to datetimes
datestr(t, 'yyyy-mm-dd HH:MM:SS') % convert back to strings
For the input & output formatter see the documentation. I assume that the last part is always zero.
I need to parse a string like this:
Apr 3, 2014 10:03:51 AM
to something like this:
YYYY-MM-DD HH:MM:SS
And also, this long long:
1396682344000
To the same kind of string:
YYYY-MM-DD HH:MM:SS
Is there any library or function to do that? I am not very confortable writing C and I am not used to parse this kind of strings.
I tried with strptime with this code:
observationDate_message is the like first string (Apr 3, 2014 10:45:01 AM)
strptime(observationDate_message, "%G-%m-%d %r", &result);
debugLog(DEB_INFO, "observationDateConverted: %d-%d-%d %d:%d:%d\n", result.tm_year, result.tm_mon, result.tm_mday, result.tm_hour, result.tm_min, result.tm_sec);
And what I get is:
0-52-0 36905376:32630:1497284224
Tutorial in; http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Fstrpti.htm
Check if your system has the function strptime. It's part of POSIX and will do the parsing of the string for you. To convert in the opposite direction there's the C standard function strftime.
you can parse
Apr 3, 2014 10:03:51 AM
this string using sscanf() and get the year month date and time information.
if str contains the string,
sscanf(str,"%s %d, %d %d:%d:%d AM",month,&dd,&yy,&hh,&mm,&ss);
you can get the data from string. This is just a example you can extract formatted data from string as you want using sscanf()
I have struct as:
struct stored
{
char *dates; // 12/May/2010, 10/Jun/2010 etc..
};
// const
struct stored structs[] = {{"12/May/2010"}, {"12/May/2011"},
{"21/May/2009"}, {"13/May/2011"},
{"10/May/2011"}, {"19/May/2011"}};
What I want to do is to sort struct 'stored' by stored.dates.
qsort(structs, 9, sizeof(struct stored*), sortdates); // sortdates function
I'm not quite sure what would be a good way to sort those days? Compare them as c-strings?
I would convert the dates to numbers using something like:
year * 10000 + month * 100 + day;
and then do a simple numeric comparison (and for month, you'll need to map from Jan to 1, Feb to 2, etc.).
If you're doing a lot of comparisons, you may want to cache the numeric equivalent in the structure.
If you convert the dates to the format YYYYMMDD (as in 20100314), you can compare them as a string or as an integer (after conversion).
ISO 8601 formatted dates ("YYYYMMDD" or "YYYY-MM-DD" etc.) are trivially comparable as C strings. Your format is not - would changing the format of the date strings be an option?
PS: If you get rid of the "-", you could even store the date as plain 32bit integer. Depending on what your application does with those dates, that might be an additional bonus.
You can't compare these as strings, but you can compare substrings. Compare the years, and if they aren't equal you have your answer. Next compare the months, you'll need some kind of table to order the months by name. Finally if the months are the same, compare the days.