DateAdd converting query from sql to oracle(sql developer) - sql

I'm trying to migrate my sql query to oracle but it seems That i cannot convert my query due to DateAdd function.
STRINGVARIABLE = '1361439468476'
output is : Feb 20,2013
convert (char(12), (dateadd(s, convert(bigint, STRINGVARIABLE) / 1000, convert(datetime, '1-1-1970 00:00:00'))), 107)

DATEADD doesn't exist in Oracle. There are various ways to manipulate dates, but this is fairly straightforward:
select date '1970-01-01' + (to_number('1361439468476') / (1000*60*60*24))
from dual;
DATE'1970
---------
21-FEB-13
... which is actually 21/02/2013 09:37:48, so not sure why you have it as 20-FEB-13.
If you want to keep the millisecond precision you can use a TIMESTAMP instead:
select timestamp '1970-01-01 00:00:00'
+ numtodsinterval(to_number('1361439468476')/1000, 'SECOND')
from dual;
TIMESTAMP'1970-01-0100:00:00'+NUMTODSINTERVAL(TO_NUMBER('1361439468476')/10
---------------------------------------------------------------------------
21-FEB-13 09.37.48.476000000
I'm not sure what the 107 in your query is doing though, or convert, perhaps they are formatting the result as a string?
OK, I see what convert(..., 107) is doing; the equivalent is:
select to_char(date '1970-01-01'
+ (to_number('1361439468476') / (1000*60*60*24)), 'Mon DD, YYYY') as dt
from dual;
DT
------------
Feb 21, 2013
... using to_char() with a Mon DD, YYYY format model.

Related

How do I combine YEAR and MONTH into a DATE?

I'm looking for an easier way to do this. My current code is written as below:
CASE WHEN LENGTH(CAST (MTH AS VARCHAR(4))) = 1
THEN CAST(CAST (YR AS VARCHAR(4))||'-0'||CAST (MTH AS VARCHAR(4))||'-01' AS DATE)
ELSE CAST(CAST (YR AS VARCHAR(4))||'-' ||CAST (MTH AS VARCHAR(4))||'-01' AS DATE)
END AS RPT_MTH
The table has a field called YR with the 4 digit Year. MTH field is just a number 1 through 12, with no leading 0 for 1-9.
Is there an easier way to get to the RPT_MTH than what I'm currently doing?
Based on Teradata's internal date:
cast((yr-1900) * 10000 + mth * 100 + 1 as date)
This is a little shorter:
cast(cast(YR*10000+MTH*100+1 as char(8)) as date format 'YYYYMMDD') AS RPT_MTH
Or if you need to stick to character operations:
cast(cast(YR as char(4))||right('0'||cast(MTH as varchar(2)),2) as date format 'YYYYMM')
Consider CONCAT, LPAD, TO_CHAR and TO_DATE which may vary in support depending on your version. Default format for TO_DATE is ISO date format at: 'YYYY-MM-DD'.
TO_DATE(CONCAT(YR, '-', LPAD(TO_CHAR(MTH), 2, '0'), '-01'))

How to convert date : Sep 26 00:15:00 2020 in YYYY/MM/DD HH24:MI:SS' format in oracle sql

Error while converting
select to_date('Sep 26 00:15:00','DD/MM/YYYY HH24:MI:SS') from dual ;
ORA-01841: (full) year must be between -4713 and +9999, and not be 0 .
Looking for solutions
How? By providing correct format masks, of course.
SQL> select
2 to_char(
3 to_date('Sep 26 00:15:00 2020', 'Mon dd hh24:mi:ss yyyy'),
4 'yyyy/mm/dd hh24:mi:ss'
5 ) result
6 from dual;
RESULT
-------------------
2020/09/26 00:15:00
SQL>
With TO_DATE(), you want to convert a string - 'Sep 26 00:15:00' to an Oracle DATE type - that is a type that counts, internally, the number of seconds since an epoch date.
In Unix, that would be '1970-01-01 00:00:00', in some other databases I know '2000-01-01 00:00:00'. I don't know about Oracle.
So you convert the string: 'Sep 26 00:15:00'. That is: the three-letter English month name abbreviation, a space, the day as two digits, a space, two digits for the 24hour-notated hour, a colon, two digits for the minutes, another colon, and two digits for the second. Nothing for the year, to be precise. The second parameter of TO_DATE() must describe that format. It would therefore be: 'Mon DD HH24:MI:SS' .
The second parameter you use makes absolutely no sense in this context. You could, if you want to play, use it to re-format the converted DATE type, back to a string, in the ISO format, using TO_CHAR():
SELECT
TO_CHAR(
TO_DATE('Sep 26 00:15:00','Mon DD HH24:MI:SS')
, 'YYYY-MM-DD HH24:MI:SS'
)
FROM dual;

How to strip out the timestamps in the date field (not from a date field but rather a varchar holding date/timestamp data)

So I have data that looks like this:
DATE
2019 04 19 03:00:00
I want it to look like this:
DATE
2019 04 19
OR
DATE
04-19-2019
The only catch is the field holding the data is a varchar so what I tried below doesn't work.
select to_char(DATE, 'YYYY/MM/DD') as dateonly, from table_name;
I've also tried using the following:
select trunc(DATE) as date_only from table_name;
But I get values that look like this:
2019 04 19 00:00:00
I guess the above could work but is there a way I can get rid of the trailing 00:00:00s that result from the trunc method that I used above?
If your data format is always like yyyy mm dd hh24:mi:ss then you can use following query to get the sub string:
SELECT SUBSTR('2019 04 19 03:00:00', 1, 10)
Ref: https://docs.oracle.com/database/121/SQLRF/functions196.htm#S
You have text. You want a date.
So use TO_DATE.
select trunc(to_date('2019 04 19 03:00:00','YYYY MM DD HH:MI:SS') )
from dual;
The trunc will remove the time period. And then you'll have your data in a proper DATE format so you can use all of the date specific functions offered in the database.
First, convert to a DATE:
SELECT TO_DATE('2019 04 19 03:00:00', 'YYYY MM DD HH24:MI:SS') FROM dual;
Then, from a DATE you can use TRUNC() or you can use TO_CHAR() to format it the way you want it to appear:
SELECT TO_CHAR( TO_DATE('2019 04 19 03:00:00', 'YYYY MM DD HH24:MI:SS'), 'YYYY MM DD' ) FROM dual;
Hope this helps.

How to convert format to12 HRS instead of 24hrs SQL

SELECT
Warehouses.Name, CONVERT(TIME,AirwayBillTrucks.CheckOutTime) AS CheckOutTime,
Assuming you are using SQL Server -
This will fetch you 12hr format
SELECT CONVERT(VARCHAR, Your_column_Name, 100) AS 12_hr_format
To show just the time
SELECT RIGHT(CONVERT(VARCHAR, Your_column_Name, 100), 7) AS time_in_12hr_format
OR simply use the code 108
SELECT CONVERT(VARCHAR, Your_column_Name, 108) AS time_in_12hr_format
Conversion -
100 - mon dd yyyy hh:miAM (or PM)
121 - yyyy-mm-dd hh:mi:ss.mmm(24h)
You can see all the type of format conversion here at Microsoft CAST and CONVERT
If SQL Server 2012+
Select Format(GetDate(),'hh:mm:ss tt')
Returns
03:55:30 PM

Convert date time to below format SQL Server

I tried to obtain date in this format:
'05-31-2014 01:20:25 AM'
I used below code:
Sql Fiddle here, but output date strangely changed to a different date: 30-26-2011 01:30:38 AM
select format(CAST('2011-11-26 01:30:38.000' AS datetime), 'mm-dd-yyyy hh:mm:ss tt')
Sql Fiddle here
When using the FORMAT() function, mm is minutes, MM is month, so change to:
SELECT FORMAT(CAST('2011-11-26 01:30:38.000' AS DATETIME), 'MM-dd-yyyy hh:mm:ss tt')
If there's already an appropriate format available via CONVERT(), that is preferable as it performs better than the FORMAT() function.