I am working with a DB2 database for the first time.
I am trying to work with DB2 dates, but the data is stored as a string in the DB2 database.
I want to convert this date-string into an actual date, preferably dropping off time because I want all transactions between 1 Jan 2011 and 26 Jan 2011.
So essentially, I want this MS SQL statement in DB2 magic...
CONVERT(datetime,SETTLEMENTDATE.VALUE,103)
For background, I have got as far as
CAST(SETTLEMENTDATE.VALUE, DATE)
AND
DATE(SETTLEMENTDATE.VALUE)
But I need the expert knowledge of a DB2 whizzkid!
Thanks
Based on your own answer, I'm guessing that your column has data formatted like this:
'DD/MM/YYYY HH:MI:SS'
The actual separators between Day/Month/Year don't matter, nor does anything that comes after the year.
You don't say what version of DB2 you are using or what platform it's running on, so I'm going to assume that it's on Linux, UNIX or Windows.
Almost any recent version of DB2 for Linux/UNIX/Windows (8.2 or later, possibly even older versions), you can do this using the TRANSLATE function:
select
date(translate('GHIJ-DE-AB',column_with_date,'ABCDEFGHIJ'))
from
yourtable
With this solution it doesn't matter what comes after the date in your column.
In DB2 9.7, you can also use the TO_DATE function (similar to Oracle's TO_DATE):
date(to_date(column_with_date,'DD-MM-YYYY HH:MI:SS'))
This requires your data match the formatting string; it's easier to understand when looking at it, but not as flexible as the TRANSLATE option.
I know its old post but still I want to contribute
Above will not work if you have data format like this 'YYYMMDD'
For example:
Dt
20151104
So I tried following in order to get the desired result.
select cast(Left('20151104', 4)||'-'||substring('20151104',5,2)||'-'||substring('20151104', 7,2) as date) from SYSIBM.SYSDUMMY1;
Additionally,
If you want to run the query from MS SQL linked server to DB2(To display only 100 rows).
SELECT top 100 * from OPENQUERY([Linked_Server_Name],
'select cast(Left(''20151104'', 4)||''-''||substring(''20151104'',5,2)||''-''||substring(''20151104'', 7,2) as date) AS Dt
FROM SYSIBM.SYSDUMMY1')
Result after above query:
Dt
2015-11-04
Hope this helps for others.
In format function your can use timestamp_format function.
Example, if the format is YYYYMMDD you can do it :
select TIMESTAMP_FORMAT(yourcolumnchar, 'YYYYMMDD') as YouTimeStamp
from yourtable
you can then adapt then format with elements format foundable here
Okay, seems like a bit of a hack. I have got it to work using a substring, so that only the part of the string with the date (not the time) gets passed into the DATE function...
DATE(substr(SETTLEMENTDATE.VALUE,7,4)||'-'|| substr(SETTLEMENTDATE.VALUE,4,2)||'-'|| substr(SETTLEMENTDATE.VALUE,1,2))
I will still accept any answers that are better than this one!
You can use:
select VARCHAR_FORMAT(creationdate, 'MM/DD/YYYY') from table name
Related
I have a large dataset with 50M+ rows. Database is on sql server 2019.
In Power Query, all but the last step shows the native query. The last step is converting the value (for some reason Power query picks up the number as decimal) to whole number. When I right-click on this step it shows the native query option disabled.
Why is Decimal to Whole number is preventing Power Query from showing native query? What is the way to achieve native query in this situation?
My intention is to configure incremental load on this table.
Assuming your data source is SQL (?) you could
SELECT CAST(myObscureNumber AS int)
Changing datatypes frequently breaks query folding and is mentioned in the docs. Here is a good read: https://blog.crossjoin.co.uk/2021/06/29/data-type-conversions-for-sql-server-sources-and-query-folding-in-power-query/
Connectors are regularly updated to include more native DB features and enable better folding.
EDIT - Don't do this - see David's comment for why this is not efficient
If PQ folds the conversion to, say, select cast(dt as date) d, ...
from t and then the user filters on the projected column d, will
result in SQL query like select ... where cast(dt as date) >
'2022-01-01', which can't use indexes or partitions, and will have to
convert the dt column for each row to compare it with the filter
value. – David Browne - Microsoft 1 hour ago
According to this, you could try decimal to text and then extract the text before the decimal point to avoid breaking the query fold.
https://en.brunner.bi/post/changing-data-types-that-do-not-break-query-folding-in-power-query-power-bi-1
I am currently using DMN for a task. This task basically checks a given month and day, depending on the input will create a specific date for the output variable. My issue is the output date is dependant on the input year i.e if the input was 2022/04/01, then my output would be 2022-02-30 (Similar for 2019, output would be 2019). I cannot figure out how I can use the input variable for year as the year in the output ? Or maybe this is not possible ?
Thank you in advance!
In definedDate you could use the string join function, e.g.: date and time(string join ([string(YEAR), "09-22T00:00:00"], "-"))
Edit:
string join seems not to be available in Camunda DMN, however it is available in other DMN Tools (see here).
Have string "09/07/2019 20:40:01 EDT"
Run command in SAS EG 7.1 connecting to a remote UNIX server
input(substr(event_time, 1, length(event_time)-4),anydtdtm23.) format DateTime23.3 as event_time
One machine comes back correctly as Sep 07, 2019. (dropping the time bit for clarity)
The other machine comes back as July 09, 2019.
Based on what I've read in related questions it seems to be caused by locale setting
What governs SAS locale setting ? Is it my OS ? the Remote UNIX server ? Something in SAS EG settings ?
Thank you
Don't use a guessing procedure unless you must. The ANYDT... series of informats will assume that ambiguous MDY or DMY stings follow the custom of the current language settings of your SAS session. So if SAS thinks you are in England then '09/07/2019' looks like the 9th of July. But if you are in the US then it looks like September 7th.
Instead read the string using either the MMDDYY or DDMMYY informat, depending on what format the strings are actually stored in. If your source actually is using mixed month day ordering styles then write code that uses information from your data to pick the right informat for a particular observation instead of forcing SAS to use your session setting to decide which one to use.
I've recently started using SAS Enterprise guide and would like to convert the date "01MAR2014" to "MAR 2014". I've been googling but unfortunately no luck.
I've tried the following however it just returns an
SELECT input(put(StartDate,EURDFMYw.),9.) AS Order_DDD_YY
Does anyone know how to convert this?
If your date is a SAS date, i.e. a numeric with date format applied then you can change the display by using the monyy5. format instead. Note that this will create a character variable that will no longer sort properly in reports/datasets, it will sort alphabetically instead of a date order.
put(startDate, monyy7.) as Order_DDD_YY
Otherwise you can apply the format instead of converting it.
I've got the following question.
I'm trying to run a partial least square forecast on a data model I have. Issue is that I need to block certain line in order to have the forecast for a specific time.
What I want would be the following. For June, every line before May 2014 will be blocked (see the screenshot below).
For May , every line before April 2014 will be blocked (see the screenshot below).
I was thinking of using a delete through a proc sql to do so but this solution seems to be very brutal and I wish to keep my table intact.
Question : Is there a way to block the line for a specific date with needing a deletion?
Many thanks for any insight you can give me as I've never done that before and don't know if there is a way to do that (I did not find anything on the net).
Edit : The aim of the blocking will be to use the missing values and to run the forecast on this missing month namely here in June 2014 and in May 2014 for the second example
I'm not sure what proc you are planning to use, but you should be able to do something like the below.
It builds a control data set, based on a distinct set of dates, including a filter value and building a text data set name. This data set is then called from a data null step.
Call execute is a ridiculously powerful function for this sort of looping behaviour that requires you to build strings that it will than pass as if they were code. Note that column names in the control set are "outside" the string and concatenated with it using ||. The alternative is probably using quite a lot of macro.
proc sql;
create table control_dates as
select distinct
nuov_date,
put(nuov_date,mon3.)||'_results' as out_name
from [csv_import];
quit;
data _null_;
set control_dates;
call execute(
'data '||out_name||';
set control_dates
(where=(nuov_date<'||nouv_date||'));
run;');
call execute('proc [analysis proc] data='||out_name||';run;');
run;