I have two columns in my table one is having a date in yyyymm format and other column has some integer values between 1 to 50. How can I add these two fields and get a date value?
For example: 201402 + 12 should give me 201502 as an answer!
I assume you don't really have a DATE column but a varchar column that stores a month specification in the format yyyymm.
If you want to make use of Oracle's date arithmetic you first need to convert this "month" into a real date.
Something like this:
select to_char(add_months(to_date('201402', 'yyyymm'), 12), 'yyyymm')
from dual;
You will need to replace the character literal '201402' with a reference to your column.
Related
I have a data frame which has two columns, one is called year and is in a numeric integer form e.g. 1990 etc... I want to convert this into a date e.g. 01-01-1990 etc... I'm thinking I should be able to use parse_date or similar but can't work it out.
I am working locally with an sqllite DB. I have imported some records from teradata where there was a date field in the format of 'YYYY-MM-DD'. When i imported the records the date switched from a date to a number. I know this is a feature of sqllite and that one can access it via date(sqllite_date) when selecting it in a where clause.
My problem is that the dates now appear to be a bit odd. For example the year appears to be negative.
Is there anyway to recover this to the correct format?
Below is an example of converting a number in the database into a date
SELECT date(18386)
# -4662-03-28
SELECT datetime('now')
# 2021-02-11 10:41:52
SELECT date(sqllite_date) FROM mydb
# Returns -4662-03-28
# Should return 2020-05-04
I am very new to this area so apologies if this is a basic question. Thank you very much for your time
In SQLite you can store dates as TEXT, REAL or INTEGER.
It seems that you stored the dates in a column with INTEGER or REAL affinity.
In this case, if you use the function date(), it considers a value like 18386 as a Julian day, meaning the number of days since noon in Greenwich on November 24, 4714 B.C.
This is why date(18386) returns 4662-03-28B.C.
But I suspect that the date values that you have are the number of days since '1970-01-01'.
In this case, 18386 days after '1970-01-01' is '2020-05-04'.
So you can get the dates in the format YYYY-MM-DD if you add the value of your column as days to '1970-01-01':
SELECT date('1970-01-01', datecolumn || ' day') FROM tablename
Or by transforming your date values to seconds and treat them as UNIX time (the number of seconds since '1970-01-01 00:00:00 UTC'):
SELECT date(datecolumn * 24 * 3600, 'unixepoch') FROM tablename
Replace datecolumn with the name of your column.
I have a table including date column and hour column which is an integer type column varying from 0 to 24. I need to combine these two fields and create an hourly composite datetime field.
However, I was able to create that kind of variable by using || and cast. But I am unable to transform this code to Hive editor syntax. Can you help me with this problem
SQL Code:
CAST(CAST(CAST(DATE_OF_TRANSACTION AS FORMAT 'yyyy-mm-dd') AS VARCHAR(11))||' '||CAST(CAST( BasketHour AS FORMAT '99:') AS VARCHAR(10))||'00:00' AS TIMESTAMP(0)) Date_Time
Thank you very much
For example like this:
cast(concat(DATE_OF_TRANSACTION, ' ', lpad(BasketHour ,2,0),':00:00.0' ) as timestamp)
I want to convert a string of length 8 to a date type for comparing it. The string is formatted as DDMMYYYY (*EUR).
It has no periods for seperating the days from months and months from years. If I input a value like '01012018' to the %date BIF the compiler tells me that the expression is too short for the format *EUR:
D dateEUR s d datfmt(*EUR)
C eval dateEUR = %date('01012018':*EUR)
My previous way to compare to two dates was to take both of them, store them in datastructues, reorder the date with subfields and take the resulting, reordered date from a overlaying subfield.
Has anyone a idea of how to convert the 8A string to a DATE type in RPGLE?
The format you specify will depend on whether or not you store the date separators. If your date is in a 10-byte character field as DD-MM-YYYY (with the dashes), this is again EUR format:
%date(alpha_date : *eur)
If you don'tt need standard separators with your date (DDMMYYYY in an 8-byte character field) simply append the number zero (0) to the end of the format name:
%date(alpha_date : *eur0)
I have 3 dropdown lists in a form for the user to input the date by day, month and year. How can I make that once t is the date that been input by the user, it will change the status from PUBLISH to UNPUBLISHED in the database?
Below is my codes:
Need your help. Thanks.
Why don't you use the DateSerial function to convert the separate values into date values, and use that to compare two dates.
From the page at W3Schools:
The DateSerial function returns a Variant of subtype Date for a specified year, month, and day.
Syntax
DateSerial(year,month,day)
Parameter Description
year Required. A number between 100 and 9999, or a numeric expression. Values between 0 and 99 are interpreted as the years 1900–1999. For all other year arguments, use a complete four-digit year
month Required. Any numeric expression
day Required. Any numeric expression
Examples
Example 1
document.write(DateSerial(2010,2,3))
The output of the code above will be:
2/3/2010
Example 2
Subtract 10 days:
document.write(DateSerial(2010,2,3-10))
The output of the code above will be:
1/24/2010