convert oracle date format based on user input - sql

I have a oracle form that the user loads data. The date format the user uses is different than what I have in my list of values. Need help in adding this format
SELECT
TO_CHAR(TO_DATE( 'Sunday, November 4, 2018', 'DD MON YYYY' ))
FROM
DUAL;
Convert to '11/4/2018'.

You can convert a string like 'Sunday, November 4, 2018' to a DATE datatype with this expression:
TO_DATE( 'Sunday, November 4, 2018', 'Day, Month DD, YYYY' )
Then it is possible to convert the date to a string in another format with TO_CHAR().
You seem to be looking for:
SELECT
TO_CHAR(TO_DATE( 'Sunday, November 4, 2018', 'Day, Month DD, YYYY' ), 'MM/DD/YYYY')
FROM DUAL;
This yields:
11/04/2018
Demo on DB Fiddle

Related

SQL Oracle to Microsoft SQL Server trunc(current_date, 'DD') converting to trunc(current_date, 'DD')

I'm converting from SQL Oracle to Microsoft SQL Server
>= trunc(current_date, 'DD') converting to >= CONVERT(DATE, GETDATE())
But it's not giving me the same result.
My question is what does trunc(current_date, 'DD') look like (What format?)? I'm not able to test it as the old database has been deleted.
Should the conversion be convert(varchar, getdate(), 105) instead?
if you are going to store this as string type in mssql, this is the nearest datetime format that I can think of.
SELECT CONVERT(VARCHAR(50),DATEADD(day, DATEDIFF(day, '19000101', GETDATE()), '19000101'), 126) + 'Z'
Trunc in oracle will always return date as an output.
Trunc by DD will truncate time portion and give the floor date, not the nearest date.
So consider that your_date is 24-sep-2019 05:30:00 PM.
Select trunc(your_date, 'DD') from dual
Will give following result
24-sep-2019
-- see time portion is truncated
-- and return type is also date
Now, you can convert this query accordingly in mssql.
Cheers!!
In Sql Server for following query you get the out put as '25 Sep 2019'
select convert(varchar, getdate(), 106)
In Oracle to get the same date output as '25 Sep 2019' you can use the below query.
SELECT
TO_CHAR(
TRUNC(TO_DATE( '25 Sep 2019 15:35:32', 'DD Mon yyyy HH24:MI:SS' )),'DD Mon yyyy')
FROM
dual;

String date format convert to date

I need to convert this string to yyyy-MM-dd date format:
December 31, 2014 to 2014-12-31
May 31 , 2018 to 2018-05-31
Any suggestion?
Regards!
You can just use convert():
select convert(date, 'December 31, 2014')
SQL Server is pretty good about doing such conversions.
So this works in both cases:
select convert(date, datestr)
from (values ('December 31, 2014'), ('May 31 , 2018')) v(datestr);
You can also use as shown Here
Select cast('December 31, 2014' as date) as [Date]
Select cast('December 31, 2014' as date) as [Date]
or in higher version of SQL Server provide format string value for different date format as you want.
SELECT FORMAT(cast('December 31, 2014' as Date),'yyyy-MM-dd','en-US') AS[DATE IN US FORMAT]

Convert SQL Server Date Format

I'm using this query:
select convert(nvarchar(MAX), getdate(), 100);
It's returning
Aug 16 2018 3:45PM
I'm trying to get this date format instead:
16 AUG 15:45
Please help me achieve it. Thanks
Try this: with format function
SELECT upper(FORMAT( getdate(), 'dd MMM HH:mm', 'en-US'))
This gets you what you want, and without using the awfully slow FORMAT function:
SELECT D, CONVERT(varchar(6),DATEADD(HOUR, -1, D),13) + ' ' + CONVERT(varchar(5),CONVERT(time(0),DATEADD(HOUR, -1, D)),14)
FROM (VALUES(CONVERT(datetime2(0),'2018-08-16T15:45:00'))) V(d);
Edit: Seems the OP moved the goals posts again (initially they wanted the time changed from 01:38 AM to 13:38, and then 03:45 PM to 14:45), and as a result DATEADD isn't required. I haven't bothered removing this though, as it was correct at the time; and I don't trust the goal posts won't move again.
Try this
upper case 'Aug' to 'AUG'
SELECT UPPER( FORMAT( GETUTCDATE(), 'dd MMM HH:mm', 'en-US' ) )
use upper and 'dd MMM HH:mm', 'en-US' format
SELECT Upper( FORMAT( getdate(), 'dd MMM HH:mm', 'en-US' ))
it reutrns 16 AUG 11:03
Try this
Query:
select UPPER(FOrmat(GETDATE(),'dd MMM HH:mm'))
Result:
16 AUG 17:04

big query conversion from string to date format

I have string dates that I need converted to the date format. How do I take dates like "Jan 1, 2012 12:40:17 AM" and convert it to 1/1/2012? It doesn't work when I try to cast it as a date or when I use the date function.
Try (play with) below
#standardSQL
WITH yourTable AS (
SELECT 'Jan 1, 2012 12:40:17 AM' AS dt
)
SELECT
dt,
PARSE_TIMESTAMP('%b %d, %Y %r', dt) AS dt_as_timestamp,
DATE(PARSE_TIMESTAMP('%b %d, %Y %r', dt)) AS dt_as_date,
FORMAT_DATE('%m/%d/%Y', DATE(PARSE_TIMESTAMP('%b %d, %Y %r', dt))) AS dt_as_string
FROM yourTable
see more

How can I convert the varchar2 format value to datetime type in Oracle in my case?

I have data in following format-
Fri, May 13, 2016 at 10:54 PM
The value is inserted as varchar2 type.
I wanted to convert it to datetime type to insert in another column.
How can this be possible?
You don't need regular expressions at all:
WITH your_table AS (
SELECT 'Fri, May 13, 2016 at 10:54 PM' text_date FROM DUAL
)
SELECT
TO_DATE(text_date, 'DY, Mon DD, YYYY "at" HH:MI AM', 'NLS_DATE_LANGUAGE = ENGLISH') adate
FROM your_table
You can extract the day, month, year and time from the text string using a regular expression and use to_date to convert it to a date:
WITH your_table AS (
SELECT 'Fri, May 13, 2016 at 10:54 PM' text_date FROM DUAL
)
SELECT TO_DATE(REGEXP_REPLACE(text_date, '(\w{3}), (\w{3}) (\d{1,2}), (\d{4}) at (\d{1,2}:\d{2}) (AM|PM)','\3-\2-\4 \5 \6'),'DD-MON-YYYY HH:MI AM') adate
FROM your_table