There are two date columns in Teradata table with following definition.
Date1 DATE FORMAT 'DD-MM-YYYY'
Date2 DATE FORMAT 'DD-MM-YYYY'
As you can see, the format of the date is same for both the columns.
The values I see in Teradata SQL Assistant are different for Date1 & Date2 ...
For Date1 it is shows as MM/DD/YYYY
For Date2 it is shown as DD/MM/YYYY
Also, due to this, EXTRACT(MONTH FROM Date...) is not working same for both the columns.
Please note: Date2 column is updated by me with values like '04-28-2016' i.e. 'MM-DD-YYYY'. And this is to acheive the Date1 format as shown in SQL Assistant.
Can you please let me know where I made a mistake ?
Any advise would be helpful.
Thanks,
The FORMAT is used for casting from/to a string, but SQL Assistant uses the format specified under Tools -> Options -> Data Format -> Display dates in this format. And a DATE is stored in an internal format, thus EXTRACT is independent of the it.
Btw, the only recommended way to write a date is Standard SQL's DATE '2016-04-28'
Related
I've date in dd-mm-yyyy format, I need the date in yyyy-mm(month) format. I'm using postgresql.
Thanks in advance.
date values don't have any format. Any formatting you see is applied by your SQL client. To turn a date into a string with a specific format, you can use to_char()
to_char(the_column, 'yyyy-mm')
I am trying to query data for yesterday (or last 24 hrs) in Hive but the filter trans_dt>=date_sub(current_date(),1) only works when the trans_dt format is "YYYY-MM-DD". The current format of trans_dt is "YYYYMMDD" and so I am converting it to "YYYY-MM-DD" by using the below concat command.
select concat(substring(trans_dt,1,4),'-',substring(trans_dt,5,2),'-',substring(trans_dt,7,2)) as dt from xyz
But now I am not sure how to add the ">=date_sub(current_date(),1)" condition on the newly created field "dt". Its failing as it is not a field in the table.
I am new to this querying in Hive and am not sure if I am totally off and there is an easier way? Please advise. All my online searches have only told me that there is no direct function in Hive to convert the date format from "YYYYMMDD" to "YYYY-MM-DD".
have you tried this one?
CAST(CAST( trans_dt AS TIMESTAMP(0)) AS DATE format 'YYYY-MM-DD') as TRANS_DATE
using CAST can be the solution for changing the date format
Using derived column i am adding 3 columns -> 2 columns for date and 1 for timestamp. for the date columns i am passing a string as parameter. for eg: 21-11-2021 and timstamp i am using currenttimestamp fucntion.
i wrote expressions in derived columns to convert them as date and timestamp datatype and also in a format that target table needs which is dd-MM-yyyy and dd-MM-yyyy HH:mm:ss repectively
For date->
expression used: toDate($initialdate, 'dd-MM-yyyy')
data preview output: 2021-01-21 --(not in the format i want)
After pipline Debug Run, value in target DB(Azure sql database) column:
2021-01-21T00:00:00 -- in table it shows like this I dont understand why
For Timstamp conversion:
Expression used:
toTimestamp(toString(currentTimestamp(), 'dd-MM-yyyy HH:mm:ss', 'Europe/Amsterdam'), 'dd-MM-yyyy HH:mm:ss')
Data preview output: 2021-11-17 19:37:04 -- not in the format i want
After pipline Debug Run, value in target DB(Azure sql database) column:
2021-11-17T19:37:04:932 -in table it shows like this I dont understand why
question 1: I am NOT getting values in the format the target requires ???and it should be only in DATE And Datetime2 dataype respectively so no string conversions
question 2: after debug run i dont know why after insert the table values look different from Data preview???
Kinldy let me know if i have written any wrong expressions??
--apologies i am not able post pictures---
toDate() converts input date string to date with default format as yyyy-[M]M-[d]d. Accepted formats are :[ yyyy, yyyy-[M]M, yyyy-[M]M-[d]d, yyyy-[M]M-[d]dT* ].
Same goes with toTimestamp(), the default pattern is yyyy-[M]M-[d]d hh:mm:ss[.f...] when it is used.
In Azure SQL Database as well the default date and datetime2 formats are in YYYY-MM-DD and YYYY-MM-DD HH:mm:ss as shown below.
But if your column datatypes are in string (varchar) format, then you can change the output format of date and DateTime in azure data flow mappings.
When loaded to Azure SQL database, it is shown as below:
Note: This format results when datatype is varchar
If the datatype is the date in the Azure SQL database, you can convert them to the required format using date conversions as
select id, col1, date1, convert(varchar(10),date1,105) as 'dd-MM-YYYY' from test1
Azure SQL Database always follows the UTC time zone. Using “AT TIME ZONE” convert it another non-UTC time zone.
select getdate() as a, getdate() AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time' as b
You can also refer to sys.time_zone_info view to check current UTC offset information.
select * from sys.time_zone_info
I am trying to change the format of a timestamp field to a date field in a certain format with AWS Redshift.
I googled a lot and the common "best practice" that I found was to cast the timestamp to a date and then use to_char to bring it into the right format. In the end I want to use the date field in a Qlik Sense dashboard where it is input for a date picker extension that apparently requires the format DD/MM/YYYY to work.
Current date format in the DB:
9/2/2019 6:38:00 AM (which I would describe as M/D/YYYY H:MM:SS ZZ)
Desired output:
DD/MM/YYYY, resulting in the value 02/09/2019
Current status:
to_char(cast(timestamp_field as date), 'DD/MM/YYYY') --> result: 02/09/2019
However, the date picker extension in Qlik Sense still does not work and I guess that's because the output is a string and not a date. Casting the string to a date returns in an error.
cast(to_char(cast(timestamp_field as date), 'DD/MM/YYYY') as date) as date_picker_date
Connector reply error: SQL##f - SqlState: 57014, ErrorCode: 30,
ErrorMsg: [Amazon][Amazon Redshift] (30) Error occurred while trying
to execute a query: [SQLState 57014] ERROR: Error converting text to
date
I am new to Redshift and would have expected to be able to cast to date with a format string as parameter, but apparently that's not a thing. Can someone enlighten me on how to solve this?
You can try with the timestamp at the beginning of the script of the QlikSense.
It will be at the start of the script. in the Main section as shown below:
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';
here SET is being used to define the time stamp in that application.
You can try changing here to your requirements.
Else 2 options 1 use a stamp which you have already used or create your own extension from the lib
Stamp use can be like this:
Datestamp_Table:
Load *,
Date(Date#(StringDate,'M/D/YY'),'DD/MMM/YY') as Date;
LOAD * INLINE [
StringDate
8/7/97
8/6/97];
Output will be like this:
Stringdate Date
8/7/97 07/Aug/97
8/6/97 06/Aug/97
I store the date in an SQLlite database as a long and want to view it in a ListView. At the moment I have the Database, a content provider, a loadermanager and a custom adapter. At the moment I'm using the custom adapter to format the data. However, there may be other possibilites, so I'm interested what is best practice.
An alternative, assuming that saving it as a long meets the recognised time string formats as per :-
Time Strings A time string can be in any of the following formats (e.g. the very last one):
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
SQL As Understood By SQLite - Date And Time Functions
would be to extract the data in the required display format.
For example consider the following :-
DROP TABLE IF EXISTS mydata;
CREATE TABLE IF NOT EXISTS mydata (mydatetime INTEGER);
INSERT INTO mydata VALUES(1092941466); -- <<<<<<<<<< 2004-08-19 18:51:06
SELECT *, datetime(mydatetime,'unixepoch'), strftime('%d/%m/%Y %H:%M:%S week %W of Year %Y',mydatetime,'unixepoch') FROM mydata;
The result would be :-
Reading the link above should indicate the flexibility of this approach.