Convert number to date in oracle sql - sql

I have different numbers looking like 40825 and I want to convert them to an actual date in Oracle SQL.
I know it should be SELECT TO_DATE(40825 ,'MM-DD-YYYY') in SQL Server, but this does not work with the same syntax in oracle SQL.
Any help?

If this number mean 4 day, 8 month, and year 2025 then, u must use to_date function with string (not nubmer) and string must looks like date mask.
SELECT TO_DATE(to_char(40825,'FM000000') ,'MMDDYY') FROM dual

Related

Display the full Month name from string representation of date stored in “Jan/10/2015” format IN SQL

I want to find the month name in the result from the date given as Jan/10/2015 in SQL.
You can try the DATENAME function in SQL
SELECT DATENAME(month, '2017/09/25');
which in this case will return September.
Date/time functions are notoriously databases dependent -- and you haven't specified the database. That said, your value looks like a string and not a date/time value.
Although you should always store date/time values using appropriate types (which is NOT a string), it looks like you can use string manipulation to do what you want. Most databases support left(), so:
select left(datecol, 3)
In those that don't, use substr() or substring().
You can try this using Oracle:
SELECT TO_CHAR(TO_DATE('Jan/10/2015', 'MM/DD/YYYY'), 'MONTH') as MonthName FROM DUAL;

Calculate age for specific year in oracle sql

Please i need help about oracle sql! I try to get age from birth date but it's not working from the year 1940 ! My query is
select months_between(sysdate, '11/01/40') from dual
and the answer i get is -19!
The calculation works up to year 1950
If you use a proper ANSI date literal, with the full year component, it works:
SELECT MONTHS_BETWEEN(sysdate, date '1940-01-11') FROM dual;
Assuming your dates are stored as text DD/MM/YY, you may use TO_DATE instead:
SELECT MONTHS_BETWEEN(sysdate, TO_DATE(date_col, 'DD/MM/YY)
FROM yourTable;
If the above does not work, it could be because 40 is being interpreted as 2040, not 1940. Please use the full 4 digit date whenever possible, and also avoid storing your dates as text.

check for dates syntax - teradata SQL

I am trying to check for dates but after running the query below, it displays no result. Could someone recommend me the correct syntax?
SELECT TOP 10 * FROM MY_DATABASE.AGREEMENT
WHERE end_dt=12/31/9999
12/31/9999 might look like a date for you but for the database it's a calculation:
12 divided by 31 divided by 9999 and because this involves INTEGER division this results in an INTEGER 0
So finally you compare a DATE to an INT and this results in typecasting the DATE to a INT.
The only reliable way to write a date literal in Teradata is DATE followed by a string with a YYYY-MM-DD format:
DATE '9999-12-31'
Similar for TIME '12:34:56.1' and TIMESTAMP '2014-08-20 12:34:56.1'
Is it a date column? Then try where end_dt = '9999-12-31'.
The question you ask is not very clear. The date you specify is language dependent.
Try
SELECT TOP 10 * FROM MY_DATABASE.AGREEMENT WHERE end_dt='99991231'

MS SQL To Informix 11 - Group By converted datetime

I am attempting to move a stored procedure from Microsoft SQL Server 2000 to Informix 11. The original SP contains a final select statement with a GROUP BY statement that includes a converted datetime:
group by convert(varchar(8), c.startDateTime, 1)
When I convert this to Informix syntax I get a syntax error at run time:
GROUP BY (c.startDateTime::DATETIME YEAR TO DAY)::VARCHAR(10)
Can anyone please point me to how, if possible, this can be done in Informix? If this is not possible, which I suspect, how would you typically handle this in the overall query?
I think you need to convert from this:
SELECT a, b, c
FROM ...
GROUP BY (c.startDateTime::DATETIME YEAR TO DAY)::VARCHAR(10) ;
to something like:
SELECT a, b, c,
(c.startDateTime::DATETIME YEAR TO DAY)::VARCHAR(10) AS d
FROM ...
GROUP BY 4 ; --- meaning: the 4th column in the SELECT clause
In Informix, it is generally not required to convert datetime fields to character strings to manipulate them. Instead, use the extend function.
Example, if c.StartDateTime is defined as a datetime year to second e.g. 2012-10-28 23:00:00 and you want just the date portion, use extend (c.StartDateTime, year to day). This will return 2012-10-28.
Are you converting to Informix syntax for dates to varchar in mm/dd/yyyy (mssql style=1) as
TO_CHAR(c.StartDateTime,"%m/%d/%iY")
???

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...