We have a Netezza table that contains dates stored in a numeric YYYYMMDD format (eg 20090731).
What is the best Netezza syntax to use to convert this into date format?
eg
SELECT somefunction(20090731) as NZDATE
?
Easiest way to convert number to date would be
select date(to_char(20090731,'99999999')) as Number_As_DATE;
You can use this one as it's the best one.
SELECT TO_DATE('20090731','YYYYMMDD') as NZDATE
to_date (sk_dim_time ,'YYYYMMDD')
My efforts were thwarted originally due to invalid dates. The code bellow does work as long as you wrap it in a statement to catch bad dates.
select to_date(substring(20090731 from 1 for 8),'YYYYMMDD') as NZDATE
Obviously 20090731 should be replaced with the name of the numeric variable.
select to_date(20090731,'YYYYMMDD') as Number_As_DATE
This will work without converting to char.
Related
I need help in figuring out the date conversion logic in Snowflake. The documentation isn't clear enough on this.
In SQL Server, I would try
SELECT CONVERT(DATE, '20200730', 101)
and it gives me '07/30/2020'.
If I try the following in Snowflake,
to_varchar('20200730'::date, 'mm/dd/yyyy')
it gives me '08/22/1970'. Why would it give an entire different date? Need help in getting the logic with the correct date.
The issue with what you are doing is that you are assuming that Snowflake is converting your string of '20200730'::DATE to 2020-07-03. It's not. You need to specify your input format of a date. So, 2 options based on your question being a bit vague:
If you have a string in a table and you wish to transform that into a date and then present it back as a formatted string:
SELECT TO_VARCHAR(TO_DATE('20200730','YYYYMMDD'),'MM/DD/YYYY');
--07/30/2020
If the field in the table is already a date, then you just need to apply the TO_VARCHAR() piece directly against that field.
Unlike SQL Server, Snowflake stores date fields in the same format regardless of what you provide it. You need to use the TO_VARCHAR in order to format that date in a different way...or ALTER SESSION SET DATE_OUTPUT_FORMAT will also work.
Try select to_varchar(TO_DATE( '20200730', 'YYYYMMDD' ), 'MM/DD/YYYY'); which produces 2020-07-30
You may need to refer to https://docs.snowflake.com/en/user-guide/date-time-input-output.html#timestamp-formats
I have a Varchar like so:
23FEB2025
I am trying to convert it into a format like:
1994-02-23 or YYYY-MM-DD
I have tried select cast ('23FEB2025' as date format 'yyyy-mm-dd'); and sel convert(date,'23FEB2025')
There are other dates in the column that are formatted like 12DEC65.
I am now starting to assume that there is no simple way to convert this so I am asking for a little guidance. Would i need to take sub strings of the date and use a bunch of select case statements?
I was hoping to find a short way to do this but it seems there might not be one. I read on here that storing dates as a string is a bad idea and I fully subscribe to that notion now.
Thank you for any help or advice!
The format portion of casting a date is the input format. The output format is based on your locale and date settings. In your case, you want this:
select
cast ('23FEB2025' as date format 'ddMMMYYYY')
Which will return 2025-02-23.
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
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') ...
I have two columns. ColA and ColB contains char(10) with data "20090520" and "20090521".
I want to select and get the date difference in days. I have tried using Format() and CDate()
but MS Access always display as #ERROR.
Access prefers its dates in this format:
#2009-12-01#
You can convert your date to something Access understands with:
CDate(Format([ColA], "0000-00-00"))
Or alternatively:
DateSerial(Left([ColA],4),Mid([ColA],5,2),Right([ColA],2))
And to display the result in your preferred format:
Format(<date here>, "dd-mm-yyyy")
Try using DateSerial() to convert the dates:
DateSerial(Left([FieldName],4),Mid([FieldName],5,2),Right([FieldName],2))
If at all possible, change the datatype to a date datatype. You should not store dates as character data.
I am connecting to another database which I have no control on. That is why this problem occurred. Thanks for the feedback.