How to get 0001 instead of 2001 using YYYY - sap

I have a column with different timestamps, like:
5771.10.04 16:07:23.800913000
0967.06.17 06:20:28.800906000
3857.06.18 03:49:03.800906000
01.04.29 16:45:04.400909000
I need to convert these into decimals (which I use for a join of some million rows), like so:
57711004160723800913
9670617062028800906
38570618034903800906
10429164504400909
And I do this using this function:
cast( substr(to_char($timestamp,'YYYYMMDDHH24MISSFF'),1,20), 'decimal(20,0)');
The problem is the last timestamp, which only has two digits for the year, where the YYYY conversion occurs. I would need the 01 to be transformed into 0001, instead it is transformed into 2001.
Any ideas how I could solve this in a quick/non-intensive way?
Tl;dr I need to transform the year 01 into 0001 instead of 2001 in BODS.

Related

NZSQL/Code - How to set NZSQL to NOT round to the nearest whole number

Everyone.
I am using some functions in NZSQL and am finding that NZSQL is rounding to the nearest whole number and was not sure if this is the design of the functions, or if the rounding could be disabled.
One of the functions that I am using is
TO_NUMBER(AGE(column_a),'000')
and it rounds to the nearest number, but I would like to have it leave it at a decimal number. Something like 12.42. Is this possible? Should I be using a different function? I have tried using '00.000) but it still rounds...
Thanks in advance!
The AGE function returns an interval which may not behave as you'd expect/hope when paired up with TO_NUMBER's format templates. The shape of the templates has a particular meaning that is different than what you might intuit.
For example, here I have a format template that corresponds to NUMERIC(20,6)
SYSTEM.ADMIN(ADMIN)=> select age('01/01/1960'::date) , to_number(age('01/01/1960'::date),'99999999999999999999.999999');
AGE | TO_NUMBER
-----------------------------------+---------------------
54 years 11 mons 15 days 23:17:21 | 541115231721.000000
(1 row)
Here you can see the interval expressed as digits in the result of the TO_NUMBER. The first two digits represent the 54 years, the next two represent the 11 months, and the last two before the decimal point represent the 21 seconds. Note that there is no value past the decimal point, and this is expected (well, by the design if not by us).
If we take one 9 off the template to the right or the left of the decimal point we get a malformed response. Notice that the 48 seconds is truncated to just a 4.
SYSTEM.ADMIN(ADMIN)=> select age('01/01/1960'::date) , to_number(age('01/01/1960'::date),'9999999999999999999.999999'), to_number(age('01/01/1960'::date),'99999999999999999999.99999');
AGE | TO_NUMBER | TO_NUMBER
-----------------------------------+--------------------+--------------------
54 years 11 mons 15 days 23:27:48 | 54111523274.000000 | 54111523274.000000
(1 row)
The point there was just to highlight that the format of the TO_NUMBER template does something other than what you likely expect/want.
What you probably want instead (if I get the right gist from your comment) is something like this, which uses DATE_PART as a loose substitute for DATEDIFF:
SYSTEM.ADMIN(ADMIN)=> select date_part('day',now() - '01/01/1960'::date) / 365.242;
?COLUMN?
------------
54.9580826
(1 row)

SQLite Convert String to Date when date column has single digit months and days

I am trying to mimic the solution in this answer post: Sqlite convert string to date
The problem I am running into though is that my raw text date field contains dates in the following format:
1/5/2014 0:00:00
instead of:
01/05/2014 0:00:00
How can I account for this single digit month/day variability and adapt the code for my purpose? I cannot think of a decent way. I have thought of using excel to create a conversion table that I could join upon. Is there a more elegant approach?
Edit on 10/2/2014:
I would have thought there would be more like 81 possible combinations to identify. Or Maybe there is a simpler way I am not thinking of?
1/1/
1/2/
1/3/
1/4/
1/5/
1/6/
1/7/
1/8/
1/9/
2/1/
2/2/
2/3/
2/4/
2/5/
2/6/
2/7/
2/8/
2/9/
...
There are eight different combinations of one- or two-digit fields; you just have to check for all of them:
SELECT CASE
WHEN Trans_Date LIKE '_/_/____ _:__:__' THEN substr(Trans_Date, 10, 1)
WHEN Trans_Date LIKE '_/_/____ __:__:__' THEN substr(Trans_Date, 10, 2)
...
END AS Hour,
...
FROM LS2014

How to convert a date to string then split it into 4 parts using Pentaho Kettle

I have a table that contains a column named time_created representing a date: 2014-02-19 23:49:59.998557. Now I need to generate a new table that consists of year, month, day and quarter columns.(For analysis purpose).
For example, 2014-02-19 23:49:59.998557 should be converted to:
year| month | day | quarter
2014 | 2 | 19 | Q1
How can I do that using Kettle? Thanks!
Or use the select values step.. this can make copies of fields then you simply specify the format mask for the date to string conversion in the meta tab. Potentially more flexible too..
Use a calculator step to generate the four fields you need (year, month,..). For each field use your timestamp as Field A and choose the appropriate calculation (e.g. Year of date A).
You will need to work around the quarter field to prepend a Q (calculation = set field to constant value A ). You can do this in the same calculator step, using the remove feature to get rid of your Q field.

SQL search date range in part of text field

I have a text field in a SQL database with dates that are generally like 13 Jan 1897 but could be just 1962 or Jan 2013 or ?1956 etc.
I want to be able to do a WHERE on year ranges. eg. from 1945 to 1987.
I realise that it may be difficult to cater for all formats of the data. I would be happy if it just catered for:
13 Feb 1972, and Feb 1972, and 1972
If the only date formats you are concerned about are like those above and you just want the Year it looks like you could just use the last 4 characters of the value.
In SQL Server that would be:
RIGHT(datefield,4)
Or in most languages (including SQL Server):
SUBSTRING(datefield,LEN(datefield)-3,4)
In ANSI SQL:
MID(datefield,LEN(datefield)-3,4)
Given you are using MySQL. If you want to be able to query your table with proper date comparisons, you may want to create a new field with DATETIME type and fill it with DATETIMES generated from your current field. You'd need queries like :
UPDATE _TABLE_ set NEW_DATETIME_FIELD = STR_TO_DATE(_CURRENT_FIELD_, '%Y') where CHAR_LENGTH(_CURRENT_FIELD_) = 4;
The above query would work for records with values in _CURRENT_FILED_ like '1945'. You'd need to run this query for each type of date string you got in the current field.
The new DATETIME field will allow you to properly use MySQL date functions against it.

how to display number value in words

Q. Display the number value in Words and output should look like this
SAL In_Words
--------- -----------------------------------------------------
800 eight hundred
1600 one thousand six hundred
1250 one thousand two hundred fifty
And, I'm still didn't figure out, how this query is the solution for the above output.
select sal, to_char(to_date(sal,'j'),'Jsp') in_words from emp
What to_date is doing here ? Anyone have any idea about this query ?
So how the query works? Well here’s why:
select to_char(to_date(:number,'j'),'jsp') from dual;
If you look into the inner most part of the query to_date(:number,'j') the ‘j’ or J is the Julian Date (January 1, 4713 BC), basically this date is been used for astronomical studies.
So to_date(:number,'j') it take the number represented by number and pretend it is a julian date, convert into a date.
If you pass 3 to number, so it will convert date to 3rd Jan 4713 BC, it means 3 is added to the Julian date.
select to_char(to_date(3,'j'),'jsp') from dual;
Now to_char(to_date(3,'j'),'jsp'), jsp = Now; take that date(to_date(3,'j')) and spell the julian number it represents, the output is:
TO_CH
-----
three
There is a limitation while using Julian dates ,It ranges from 1 to 5373484. That’s why if you put the values after 5373484, it will throw you an error as shown below:
ORA-01854: julian date must be between 1 and 5373484
Hi everyone, it is interesting this topic. I remember when I was learning Oracle in 2005 one of the instructor required me to write a PL/SQL code to convert numbers in words, it was a whole two pages code to reach this.
Here is some reference that could help us to understand the Julian day, that is why we use the letter 'j' or 'J' during this operation.
First there is a website that has the example and explanation about "How To Convert Number Into Words Using Oracle SQL Query":
http://viralpatel.net/blogs/convert-number-into-words-oracle-sql-query/
Second if you want to know more about "Julian day" go to:
http://en.wikipedia.org/wiki/Julian_day
Third if you want to know more about who proposed the Julian day number in 1583, it was by "Joseph Scaliger":
http://en.wikipedia.org/wiki/Joseph_Justus_Scaliger
It is not make sence for me continue repiting what another author in these websites have made, that is why I just posted the link you can access them and read what you need to understand how query like this works:
SELECT TO_CHAR (TO_DATE (2447834, 'j'), 'jsp') FROM DUAL;
//Output: two million four hundred forty-seven thousand eight hundred thirty-four
I've never heard of a DBMS with a built-in function to do as you ask. You'll need a table of number-names, to join to that once per digit using modulo arithmetic, and string concatenation to produce one In_Words column. Plus some logic to eliminate leading zeros. It will take time to write.
J stands for Julian day - the number of days since January 1, 4712 BC. The numbers in your table converted to Julian date. JSP spells out the date:
SELECT to_char(SYSDATE,'JSP') AS number_of_days_sinse_4712_BC
FROM dual
/
to convert decimal number into words, you can follow below code
SELECT TO_CHAR(to_date(TRUNC(num),'J'),'Jsp')
||' and '
|| TO_CHAR(to_date(to_number(SUBSTR(num-TRUNC(num),instr(num-TRUNC(num),'.')+1)),'J'),'Jsp') Indicator
FROM
(SELECT &enter_numbr num FROM dual
);
Hope this will help!!!