One of the columns is present in date datatype. I need to convert that date column in character datatype so that it can be concatenated with another character column.
Right now my date column is present in the following format : 09-JUN-2020.
Please help me in converting this column to character column.This needs to be done sas enterprise guide.
Thank u so much in advance.
You can use PUT() to convert from numeric to character. You need to find the format you want the output to look like and use that as your second parameter. Assuming you want your date to look like 2020-06-02 character this works:
*puts date as 2020-06-02;
newVar1 = put(dateVar, yymmddd10.);
*creates date variable as 02Jun2020;
newVar2 = put(dateVar, date9.);
FYI - You can find the list of formats available here
Related
I have data of transaction dates in one column within varchar format.
This column contains three separate formats of date in dd-mm-yy, dd/dd/yyyy and third format in dd/mm/yyy format with missing the last digit.
How to modify this column in the correct date format column with the dd-mm-yyyy format.
Add a date column and fill with your convert statement to get the desired format. Once everything looks okay you can drop the text column and rename the new proper date column to match the original.
I would love to know the best way to handle data that has been inputted incorrectly as dd/mm/yyyy into a sql database as TEXT and to have it converted into a new column of the table with the datatype as DATE so it is actually stored as yyyy-mm-dd.
Existing text date column name is called "olddate" with an empty column created called "truedate" to house the new data. Each row has the date field, but none are able to be sorted correctly because of this issue.
Any ideas how I can slice and dice the current date into a new DATE field friendly version?
Thanks in advance :-)
That is style 103. So use:
select convert(date, col, 103)
Are you using Oracle? If so, TO_DATE is what you want. You can take in a string that represents a date and convert it to a date using the format you pass it.
My question, now I have table customer in Postgresql and contain the column name is update (for keeping track of update customer info date.)
The date format is ex:20170302 but I want to convert to be 02/03/2017.
Note: the datatype of the update is character varying.
I have tried several times to find all the solutions by google but not fix.
First, you should fix the data type to be a proper date or datetime. Don't store dates as strings!
But you are. You can convert the value to a date and then back to a string:
select to_char(to_date(update, 'YYYYMMDD'), 'DD/MM/YYYY')
The documentation contains the formatting elements that you can use.
Can somebody help me with how to convert varchar column into Date data type?
My createDate column has VarChar2 data type in format example 20050923
I would like to convert this column into format example 2005/09/23
ALTER TABLE test ADD (new_create_date DATE);
UPDATE test SET new_create_date=TO_DATE(createDate,'MM/DD/YYYY');
When I run these sql I get format 23-aug-2005. Why?
Internally date is stored as a 7 byte value. Formatting is not something that is stored there. You get formatting when you pull the data out of the table.
Try this.
SELECT TO_CHAR(NEW_CREATE_DATE,'MM/DD/YYYY')
FROM TEST
You could do this:
update test
set new_create_date to_date(createDate, 'YYYYMMDD');
You just need to give to_date() the right format string.
This is because your database NLS_DATE_FORMAT must be in DD-MON-YYYY format, you can check that by using the below mentioned query.
SELECT *
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
Also by definition to_date(x,format) converts a string i.e. x to a datetime, the format part is an optional part which indicates the format that is present for the string value x.
You can further use to_char(x,format) function in your case which converts the x datetime to a string as mentioned by the EvilTech.
I have a character field that stamps in the order of MMDDYYHHMMSS (note: not a date but character field). I am wanting to kick this out to a date field in my SQL into this format dd.mm.yyyy. hh24:mi.
My problem is that the sql kicks it out to YYYY-MM-DD field without the time. This section of the sql looks like this:
TO_DATE(SUBSTR(MOPACTIVITY.MOPID,3,2)||'.'||SUBSTR(MOPACTIVITY.MOPID,1,2)
||'.'||'20'||SUBSTR(MOPACTIVITY.MOPID,5,2)||'.'||SUBSTR(MOPACTIVITY.MOPID,7,2)
||':'||SUBSTR(MOPACTIVITY.MOPID,9,2)||':'||SUBSTR(MOPACTIVITY.MOPID,11,2)
, 'dd.mm.yyyy. hh24:mi:ss') "XXX",
Any thoughs on how to get the time to convert too?
No need for such a complicated expression:
to_date(MOPID, 'MMDDYYHH24MISS')
will convert the column to a real DATE column assuming the time part is in 24 hour format (00-23, not 00-12). And this will also fail if you don't really have valid dates in the varchar column.
this out to a date field in my SQL into this format
A DATE column does not have "a format"!
The format is only applied when you display it.
In case you mean you want to convert the varchar stored in your column into another varchar that has a different date formatting, the easiest is probably to simply convert the above expression back to a varchar:
to_char(to_date(MOPID, 'MMDDYYHH24MISS'), 'dd.mm.yyyy. hh24:mi')
Before applying something like that, allow me one comment:
Store dates in DATE columns, never ever store them in a VARCHAR column.
If you had done that from the beginning, all you would have to do know is to simply apply a single to_char() to your DATE column to get the display format you want.