I'm using oracle 11g sql developer
I have a varchar2 column with dates as 0523 (mmDD).
I want to convert them to a date column and have them look like 23-05 (dd-mm)..
Any ideas?
Well, you can do string operations directly to get the format you want:
substring(c, 3, 2)||'-'||substring(c, 1, 2)
To convert to a date, you can use:
to_date('2012'||c, 'YYYYMMDD')
To convert a date back to the form you want:
to_char(<date>, 'DD-MM')
Related
I'm using PostgreSQL, but this question is for any modern dbms
I want to basically convert a datetime column which has yyyy/mm/dd into just yyyy/mm
I tried getting months and year separately and using Concat, but the problem is the month comes as a single digit integers for values < 10 and that messes up ordering
select *,
concat(date_part('year' , date_old), '/', date_part('month' , date_old)) as date_new
from table
date _old
date_new
2010-01-20
2010-1
2010-01-22
2010-1
2010-11-22
2010-11
You can use to_char()
to_char(date_old, 'yyyy/mm')
If you want to display your date in the format YYYY-MM then
In PostgreSQL (db<>fiddle) and Oracle (db<>fiddle), use TO_CHAR:
SELECT TO_CHAR(date_old, 'YYYY/MM') FROM table_name;
In MySQL (db<>fiddle), use DATE_FORMAT:
SELECT DATE_FORMAT(date_old, '%Y/%m') FROM table_name;
In SQL Server (db<>fiddle), use CONVERT or, if you are using SQL Server 12 or later, FORMAT:
SELECT CONVERT(varchar(7), date_old, 111) FROM table_name;
SELECT FORMAT(date_old,'yyyy/MM') FROM table_name;
Don't do this.
If you're able to use the date_part() function, what you have is not actually formatted as the yyyy/mm/dd value you say it is. Instead, it's a binary value that's not human-readable, and what you see is a convenience shown you by your tooling.
You should leave this binary value in place!
If you convert to yyyy/mm, you will lose the ability to directly call functions like date_part(), and you will lose the ability to index the column properly.
What you'll have left is a varchar column that only pretends to be a date value. Schemas that do this are considered BROKEN.
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;
Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')
Which date function use to convert date format
YYYYMMDD TO DDMMYYYY
I am using db2 database please help me
assuming you column_with_date is a valid date column and if you have DB2 9.7, you could use
date(to_date(column_with_date,'DDMMYYYY'))
Assuming that your column's data type is VARCHAR, convert it to date and then format it to DDMMYYYY:
varchar_format((to_date(col, 'YYYYMMDD'), 'DDMMYYYY')
replace col with your column's name.
Or use substr() and concatenation:
substr(col, 7, 2) || substr(col, 5, 2) || substr(col, 1, 4)
You can use:
to_char(datecol,'DDMMYYYY')
just to display as DDMMYYYY
a date column doesn't have a format( not stored with a format within a DB table)
2 ways to do it that I can think of, already mentioned above but giving a real command so you can try it.
db2 "select to_char(current_date,'DDMMYYYY') from sysibm.sysdummy1"
or
db2 "select varchar_format(current_date,'DDMMYYYY') from sysibm.sysdummy1"
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