I am very new to laravel and I have an input for date of birth whose format is DD-MM-YYYY, but I have a SQL Error because the default format is YYYY-MM-DD HH:MM:SS, how can I change this default format?. This is my migration code for the specific date.
$table->date('date_of_birth');
Use :
$table->dateTime('date_of_birth');
Or,
$table->timestamp('date_of_birth');
timestamp and dateTime store a date (YYYY-MM-DD HH:MM:SS) together in a single field i.e. YYYY-MM-DD HH:MM:SS.
The difference between the two is that timestamp can use CURRENT_TIMESTAMP as it's value, whenever the database record is updated.
Their major difference is the range. For DateTime its up to year 9999 while for timestamp its only up to year 2038. Other differences include the amount of bytes needed to store each.
Related
I am getting timestamp format as '01-APR-21 12.02.00.496677000 AM' from oracle, I want to change the format to load data into sql server column with datatype as datetime.
Current Input: '01-APR-21 12.02.00.496677000 AM'
Expected Output : 2021-04-01 12:02:00.496
I need to write the code in oracle to change timestamp format
Thanks
Neha
A TIMESTAMP is a binary data format consisting of 7-20 bytes (century, year-of-century, month, day, hour, minute, second, up to 6 bytes for fractional seconds and up to 7 bytes for time zone information); it does NOT have a format.
Why am I seeing the TIMESTAMP with a format?
You are seeing it with a format because whatever user interface you are using to access the database has decided that it is more useful to display the binary information as a formatted string rather than returning the raw byte values to you.
Typically, for SQL/Plus and SQL Developer, this is managed by the NLS_TIMESTAMP_FORMAT session parameter. Other user interfaces will have different mechanisms by which they manage the default format of dates and timestamps.
If you want to change the default for SQL/Plus (and SQl Developer) then you can use:
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF9';
(Or whatever format you would like.)
How can I format the TIMESTAMP value?
If you then want to display the timestamp with a format (remember, a TIMESTAMP is not stored with any format) then you want to use TO_CHAR to convert it to a string where it can have a format.
If you want to format the TIMESTAMP as a YYYY-MM-DD HH24:MI:SS.FF3 string then use:
SELECT TO_CHAR( your_timestamp_column, 'YYYY-MM-DD HH24:MI:SS.FF3' )
FROM your_table
How can I convert a formatted string back to a TIMESTAMP?
From your comment:
I loaded the oracle data into a different SQL table where date format was varchar and data got loaded as '01-APR-21 12.02.00.496677000 AM'
Use TO_TIMESTAMP:
SELECT TO_TIMESTAMP( your_string_column, 'DD-MON-RR HH12:MI:SS.FF9 AM' )
FROM your_table
(Note: that this will convert your string to a binary TIMESTAMP and then whatever user interface you are using will use its rules on how to display it; and if the default format it is using is DD-MON-RR HH12:MI:SS.FF9 AM then the query above will look like it has done nothing; it has, the UI is just implicitly converting it back to a string to display it.)
say my usual Date format is '14-jan-2019' and i want my date to only be accepted as 'YYYY-MM-DD' how do i do that? and can i change jan to an actual number?
In my opinion, the right / correct way to do that is to declare your date column (or variable or whatever it is) as DATE, e.g.
create table test (date_column date);
or
declare
l_date_variable date;
begin
...
Doing so, you'd let the database take care about valid values.
You'd then be able to enter data any way you want, using any valid date format mask, e.g.
to_date('06.01.2020', 'dd.mm.yyyy')
date '2020-01-06'
to_date('2020-06-01', 'yyyy-dd-mm')
etc. - all those values would be valid.
A DATE data type has no format - it is stored internally as 7-bytes representing year (2 bytes) and month, day, hour, minute and second (1 byte each).
'14-JAN-2019' is not a date - it is a text literal.
If you want to store date values then use a DATE data type.
If you want to only accept strings of a specific format as input then in whatever user interface you use to talk to the database then accept only that format. If you are converting strings to DATEs and wanting an exact format then you can use:
TO_DATE( '2019-01-14', 'fxYYYY-MM-DD' )
Note: the fx format model will mean that the exact format is expected and the typical string-to-date conversion rules will not be applied.
I have a string "20171110" and I want this to be converted to Date format yyyy-mm-dd of date datatype in Pentaho Data Integration. I tried using select values and calculator but nothing worked. Please suggest.
First, change the String field into date type by using Select values (Metadata Tab). In this case, we use yyyyMMdd as a date format.
After that, drop the 2nd Select values to migrate yyyyMMdd format into yyyy-mm-dd.
And see the result
When querying v$backup_set,I got a set_stamp column value like '938983113'. How can I convert it to normal time, such as YYYYMMDD HH24:mi:ss format.
I am trying to convert a date field 'createdate' to UTC time but the ask is to be -04:00. I have tried a bunch of things and have had no success. I am working on Oracle. Any ideas? Thank you.
This should do it:
cast(createdate as timestamp with time zone) at time zone 'UTC'
This converts the createdate to a timestamp with your current time zone (the one that is defined by your client through SESSIONTIMEZONE). It then converts that to UTC.
It is not possible if you have a DATE or TIMESTAMP value, because those data types do not have any time zone information and thus it is not possible to convert to any other time zone - unless you treat the value as "local time zone".
There are several solutions:
createdate at time zone 'UTC'
SYS_EXTRACT_UTC(createdate)
FROM_TZ(createdate, 'UTC')
The result types are different, e.g. FROM_TZ returns as TIMESTAMP WITH TIME ZONE values, whereas SYS_EXTRACT_UTC returns a TIMESTAMP value.