bad date format for pivot - sql

together
I can not figure out why in PowerPivot or Excel a Microsoft query does not provide the date fields in the format for Excel as they are necessary. I have already gotten from you in another thread the approach, as in Microsoft Query the fields are output directly as a date.
I use the following query to put the records into Excel as a table:
select to_char(DB_Gen.STRT, 'DD.MM.YYYY') "Date" FROM XXX
So I get the data in the format "DD.MM.YYYY".
So I connected this date column to the automatic calendar table in PowerPivot, but somehow the link does not work. The pivot table can not work with it.
Where exactly is the error? Why is not the field accepted in date format?
How can I find out the error?
Edit:
Somehow, the table created from the query does not get the date format. Although the data is displayed as "DD.MM.YYYY", the column does not have the date format. How can this be changed?
Best Regards
Joshua

Check if the column DB_Gen.STRT is of date type, if so just fetch the data as below
select DB_Gen.STRT "Date" FROM XXX
If the column data type is char, then use TO_DATE to convert it to date type
select TO_DATE(TO_CHAR(DB_Gen.STRT, 'MM/DD/YYYY'), 'MM/DD/YYYY') "Date" FROM XXX

Related

Change data type of column from STRING format to DATE format

I am reading a file from ADLS location, in that one column Period_Ending_Date is having data type as STRING.
The Period_Ending_Date is having many dates in random order, I need to apply filter to get the latest date.
I'm trying this code:
select * from final_table
WHERE Period_Ending_Date = (SELECT MAX(Period_Ending_Date) FROM final_table)
But the problem is I'm getting the day with maximum, not the latest date. I can understand this is happening because of STRING data type. Please guide me how I can change this column to DATE data type or any other alternative to get the solution of this.
I'm working with Scala and SQL on Azure Databricks.
what about changing SELECT MAX(Period_Ending_Date) FROM final_table to SELECT MAX(cast(Period_Ending_Date as date)) FROM final_table - performing explicit casting to date if date format is ISO8601 (YYYY-MM-DD) or using the to_date function (doc) to convert non-standard dates.

Convert YYYYMMDD to MM/DD/YYYY in Snowflake

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

How to re-order number or change to be date format dd/mm/yyyy in PosgrestSQL?

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.

Searching Between Two Dates With A Specific Format

So I have a table where the dates are formatted as such: 15-Jan-13
That would obviously be January 15th, 2013.
The problem is, when I try to search a date range between 01/01/2014 and SYSDATE, it errors out.
Does anyone know the proper way in which I would format this based off of how my dates are stored?
FYI my raw data is stored as: 15-JAN-13 02.23.27.000000000 PM -05:00
I'm converting it as
TRUNC(variable_name)
Given that you are asking about "sysdate" then I would assume you are using Oracle. It would also seem that the the column with the dates are not actual in the date format. Best option would be to change the column to a date type. If for some reason that isn't an option, the next best thing to do is to translate the text to a date as part of your query:
select *
from foo
where to_date(your_column, 'dd-Mon-yy') between to_date('01/01/2014', 'mm/dd/yyyy')
and sysdate
Note that to_date is expecting it's second argument to match the structure of the date specified so '01/01/2014' would match the date format 'mm/dd/yyyy' and your_column (value: '15-Jan-13') would match a different format 'dd-Mon-yy'. Once everything is translated to a date, Oracle can perform appropriate date comparisons.
DISCLAIMER: The translation of your_column to a date using to_date means that if an index is defined on your_column, it won't be used. If this is a large table you can expect sub-optimal performance. One additional reason to change the column data type to a date.

How to retrieve the time column value from oracleDB into .net application

I have a Oracle table where there is one date-time field.
On select query i am able to get all the field values but not timefield value in my .net application.
select abc.Id, abc.Name, abc.When from details abc where abc.Id='"+1234+"'
Could anyone suggest me.
The format of the returned DATE field from Oracle depends upon your default NLS settings in the database.
Oracle stores dates (and times) in an internal representation and when you select the date values you can then format them as you need. An official Oracle explaination is here.
To force the format you can explicitly convert the date to a string representation using:
select abc.Id,
abc.Name,
TO_CHAR(abc.When, 'DD-MON-YYYY HH24:MI:SS') AS when
from details abc
where abc.Id='"+1234+"'
If you are ONLY wanting the time portion of the WHEN column then:
select abc.Id,
abc.Name,
TO_CHAR(abc.When, 'HH24:MI:SS') AS when
from details abc
where abc.Id='"+1234+"'
This will then return it as a string rather than a date and time which may or may not be OK for you depending upon what you then want to do with it.
The format you choose for the date and time could be any of the Oracle date and time formats, see here.
Hope it helps...