Format 1800 date in SQL Server & Oracle - sql

I have the following date in our SQL Server 2008 database: 06-24-1881 00:00:00:000 as a DOB and it's stored as datetime.
I need to copy this data into our Oracle 11g database and from my understanding Oracle does not accept milliseconds into a Datetime column. For my data after 1900 I converted the data to smalldatetime and it worked but for this piece of data, it won't convert to smalldatetime since it doesn't allow for dates prior to 1/1/1900.
How do I get many rows of data into my Oracle database?
I tried this:
left(DOB, 19) as DOB
but that rendered the data as "Jun 24 1881 12:00AM", so I tried inserting with:
to_date('Jun 24 1881 12:00AM', 'MON-DD-YYYY HH:MI:SSAM')
and that didn't work either. I am stuck and need help.

Jun 24 1881 12:00AM matches the Oracle format string MON DD YYYY HH12:MIAM. Your format mask has an added seconds :SS mask that Oracle is trying to match but not finding in the input so will throw an ORA-01861: literal does not match format string exception.
So, if the output from SQL Server is in that format then in Oracle you should be able to do:
TO_DATE( 'Jun 24 1881 12:00AM', 'MON DD YYYY HH12:MIAM' )

CONVERT(VARCHAR(20),#Date,101) will give you 06/24/1881 I am looking for code now.
REPLACE(CONVERT(VARCHAR(200),#Date,101),'/','-') + ' 00:00:00:000'
Weird I tried a bunch of the century codes with CONVERT and it worked for 101 and some others but the 113 you want failed but because you don't have minutes, hours, seconds etc. you can just take the the 101 format and manipulate the string to what you want.
DECLARE #Date DATE = '06-24-1881 00:00:00:000'
SELECT #Date
,FORMAT(#Date, 'MM-dd-yyyy HH:mm:ss')
,REPLACE(CONVERT(VARCHAR(200),#Date,101),'/','-') + ' 00:00:00:000'
Note the FORMAT(#Date, 'MM-dd-yyyy HH:mm:ss') is SQL 2012 or newer and works well.

Related

How to insert DD MON YYYY format value into a date column in a snowflake table

My csv file has a date column having values in DD MON YYYY format eg: 28 Nov 2022.
When i tried inserting it into a date column(datatype= DATE) it is showing the below error. I have also tried using TO_DATE , TO_VARCHAR but getting the same error.
Kindly help me to resolve this.
Error: Date '28 Nov 2022' is not recognized
I want to insert the value in the same format (DD MON YYYY) into a column of date data type ,without changing the format i.e '28 Nov 2022'.
I was reading documentation (https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#date) and i read: "DATE accepts dates in the most common forms (YYYY-MM-DD, DD-MON-YYYY, etc.)."
So i think the format you are trying to write is a no-supported date format.
you can:
format your date in a supported date format before write field in db.
write in a varchar datatype field, but in this case you'll lose all tools on date type.
I don't see other ways!

How to convert oracle to_timestamp to sql?

I want to convert Oracle
to_timestamp(coloum_name,'DD-MM-YYYY') to sql
required output : 24-APR-17 12.00.00.000000000 PM
I know this is old, but it has an very searchable title, and there's no accepted answer.
The TO_TIMESTAMP function converts text representations of dates to a standard date/time format. From your question, it sounds like you have dates stored as characters in the format 'DD-MM-YYYY', but you want SQL Server DATETIME2(7) (based on the number of decimals in the seconds) as your output. It also seems you want the default time to be noon, rather than midnight, since your sample output shows 12:00 PM, not AM.
Using CONVERT with style 103 will change the European styled date to a DATETIME2(7), as shown below. But then you'll need to do a DATEADD to move from midnight (which will be the default value) to noon, which is twelve hours later.
DECLARE #DateSample NVARCHAR(10) = '17-04-2017';
SELECT CONVERT( DATETIME2(7), #DateSample, 103 );
--Results
--2017-04-17 00:00:00.0000000
SELECT DATEADD( HOUR, 12, CONVERT( DATETIME2(7), #DateSample, 103 ));
--Results
--2017-04-17 12:00:00.0000000
The SQL Server default is 24 hour time, so if you absolutely must switch to AM/PM designators, you'll have to convert it back to a string, which seems to be the opposite of what you're trying to do.
This is a way to convert a date/timestamp into varchar2 in Oracle with the format you want
select to_char(yourColumn, 'DD-MON-YY HH.MI.SS.FF9 PM')
from yourTable
SELECT FORMAT(SYSDATETIME(), 'dd-MMM-yyyy h.mm.ss.fffffff tt')

Date Format Issue, ORACLE

I am writing this query, to find data between two dates. The time format I have is exactly like the one I am using in the query
select TO_CHAR(REC_NO),(FIRSTNAME ||' '|| LASTNAME) as NAME,
LOC_NAME,TO_CHAR(START_TIME,'yyyy/mm/dd/HH:MI:SS'),
TO_CHAR(END_TIME,'yyyy/mm/dd/HH:MI:SS'),
TT_NO,CUST_ID,CUST_MOB,MAC_ADDR,EMAIL_ID,s.STATUS
from vw_rtb_visit_assn v
left join vu_issue_status#jiradb s on v.TT_NO = s.TICKETNUMBER and TT_NO = '123'
and v.ASSN_TIME between to_date('Tue Dec 16 00:00:00 PKT 2014','yyyy/mm/dd')
and to_date('Wed Dec 17 00:00:00 PKT 2014','yyyy/mm/dd')
My query doesn't execute and gives me a format exception.
your date are not in actual format of that particular character you pass in where condition
to_date('Tue Dec 16 00:00:00 PKT 2014','yyyy/mm/dd')
it should be
to_date('Tue Dec 16 00:00:00 PKT 2014','DY MON DD HH24:MI:SS TZD YYYY')
the format of DATE_IN_CHAR and FORMAT in to_date('DATE_IN_CHAR','FORMAT') should match.
kindly try the below
and v.ASSN_TIME between to_date('2014/12/16','yyyy/mm/dd')
and to_date('2014/12/17','yyyy/mm/dd')
Dates do not have formats. Formats are used for parsing strings as dates or generating strings from dates. You don't have to do any of these, you simply need to specify the interval as a date literal, as described in the documentation, eg:
and v.ASSN_TIME between DATE '2014-12-16' AND DATE '2014-12-17'
Date literals are actual date values, not strings that have to be parsed using a specific format.
You can also specify TIMESTAMP literals with
TIMESTAMP '1997-01-31 09:26:50.124'
or
TIMESTAMP '1997-01-31 09:26:56.66 +02:00'
for a TIMESTAMP WITH TIMEZONE.

Issue in removing time stamp in PL/SQL

I want to convert 12/8/2006 12:30:00 to 12/8/2006
I tried -
1. trunc(TO_DATE (effective_date_time,'DD/MM/YYYY HH24:Mi:SS'))
2. TO_DATE (effective_date_time,'DD/MM/YYYY')
But all these are returning values as 12/8/0006.
Why Oracle is returning year 0006 instead of 2006.
If effective_date_time is a date column using to_date() is totally useless.
It will first (implicitely!) convert the date to a varchar (based on the NLS settings, just to convert it back to a date again.
If you want a specific format for your date column use to_char()
to_char(effective_date_time,'DD/MM/YYYY HH24:Mi:SS')
Never use to_date() on date or timestamp columns!
Try this:
trunc(effective_date_time)
It's a date, you don't need TO_DATE
When you're using TO_DATE(effective_date_time, 'format') on a DATE column, effective_date_time is converted to a char using NLS params. I suppose your NLS settings is something like 'dd/mm/yy'. That's why you get a wrong year.
A simple example:
alter session set nls_date_format = 'dd/mm/yy';
select trunc(TO_DATE (sysdate,'DD/MM/YYYY HH24:Mi:SS')) from dual;
November, 22 0014 00:00:00+0000
alter session set nls_date_format = 'dd/mm/yyyy';
select trunc(TO_DATE (sysdate,'DD/MM/YYYY HH24:Mi:SS')) from dual;
November, 22 2014 00:00:00+0000
Your NLS_DATE_FORMAT has year as 'YY' in year.. And then you specify the format at YYYY again in to_date, so 2006, first interpretted as 06 again ended up as 0006.
Sincere advice, dont do To_DATE() on a date. Just TRUNC(yourdate) is what you need.
Try this:
trunc(effective_date_time)
Trunc will directly remove the time stamp from the date format

Date Time Difference in SQL 2008

How to get date and time difference between 10/12/2010 07:35:02 PM and 2010-11-19 21:51:01.713. Where first date is in MM-DD-YYYY format and Second date is in YYYY-MM-DD Format Rest is time it is also in different format as first format has "pm" in it. Please let me know how to write a query in sql 08 to calculate date and time difference?
The datetime data type in SQL Server is actually a 8-byte number. It may be represented in different formats to please humans but the format has no meaning to SQL Server itself.
To calculate the time difference between to datetime values you can use the built-in DATEDIFF function, which you can find details about here: http://technet.microsoft.com/en-us/library/ms189794.aspx
This will work thanks to the SQL Server ability to parse formatted dates for us:
select datediff(day, '10/12/2010 07:35:02 PM', '2010-11-19 21:51:01.713')
-----------
38