How to change the format of a date field in SQL? - sql

I have a date field in one of my tables that's formatted like this: 20220212.
I want to CAST it like this: 2022/02/12 but a simple CAST(DATE_FIELD AS DATE) doesn't seem to work. ANyone know how I can achieve this with SQL in Snowflake?
Thanks!

Please try to_date()
to_date(DATE_FIELD, 'YYYY/MM/DD')
Documentation

Select to_date('20220212','YYYYMMDD')::date as dt, to_char(dt,'YYYY/MM/DD') ;

Related

SQL: Casting text to date

I have a TEXT column of dates and need to convert them to dates, but the two methods I'm using are not working correctly. See below.
SELECT CAST("12/01/2009" as date);
12
This only returns the first digit before stoping at the '/'.
SELECT DATE("12/01/2009");
Returns nothing
I also tried CONVERT, but I'm using SQLite and it doesn't appear to support it as I'm getting a syntax error. Any suggestions on how to solve this?
Try using STR_TO_DATE function
SELECT STR_TO_DATE('12/01/2009','%m/%d/%Y');
SQL FIDDLE DEMO
SqLite doesn't have date type. You need to do string manipulation do achieve this.
SELECT CAST('2009-01-12' AS DATE);
Use it.
It returns 2014-02-28

How do I convert this oracle date

I am trying to convert from one date format to another. I am not sure how to write the functions.
My source date looks like 01/15/2009 01:23:15
The format I need is 01152009.
Thanks
Try this.
TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY HH:MI:SS'),'MMDDYYY')
More info here,
http://psoug.org/reference/date_func.html
Does this work for you? It assumes the date is in date format but will work with timestamp
select to_char(YourDateField,'DDMMYYYY') from dual;
You can always convert it back to a date using the TO_DATE function if you need that format.
select TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY MI:HH:SS'),'MMDDYYYY') from dual
if your field is already of data type date then you should only do:
select TO_CHAR(<fieldname>,'ddmmyyyy') ...

Formatting date sql

I want to format existing dates in a column called mydate in a table called empl. However, I am going no where with this. I have tried
SELECT CONVERT(VARCHAR(10), mydate,103) from empl
but get a missing expression error.
I want to change the current format in the column which is in the format
dd-mm-yy to mm/dd/yy
EDIT: im using oracle
Any suggestions?
If the datatype of the column is DATE, you can use the TO_CHAR() function:
SELECT to_char(mydate, 'mm/dd/yy')
FROM empl ;
Are you able to change the column type to date? Then you could use date_format
The Convert statement you show is for MS-SQL, and you would want code 3 not 103, the 100 series give 4 digit years.
Try:
select to_char(mydate,'MM/DD/YY') from empl
Assuming mydate is stored in date datatype, and using oracle, easy way to do that is:
select to_char(mydate, 'dd/mm/yy') from empl
In Oracle, convert is used to change character set with parameters
(char, destination_char_set, source_char_set)
select TO_CHAR(sysdate, 'MM/DD/YYYY') CHRDATE from dual;

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 ...

Mysql strip time component from datetime

I need to do a date comparison in Mysql without taking into account the time component i.e. i need to convert '2008-11-05 14:30:00' to '2008-11-05'
Currently i am doing this:
SELECT from_days(to_days(my_date))
Is there a proper way of doing this?
Yes, use the date function:
SELECT date(my_date)
select date(somedate) is the most common.
If you need to accommodate other formats, you can use:
SELECT DATE_FORMAT(your_date, '%Y-%m-%d');
In PostgreSQL you use the TRUNC() function, but I'm not seeing it for MySQL. From my brief Googling, it looks like you'll need to cast your DATETIME value to be just DATE.
date_col = CAST(NOW() AS DATE)
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html
Just a simple way of doing it date("d F Y",strtotime($row['date'])) where $row['date'] comes from your query
You could use ToShortDateString();