How to convert a date from nvarchar in irregular format - date-formatting

I am trying to convert a date from a format that is irregular. It is an nvarchar(20) with dates and times shown as
26-10-2006 11:38
6-11-2006 9:39
8-9-2005 14:13
Is it possible to extract the date part?

Related

Reading weird date format in hive

I have a column which contians date as string but in many formats like - dd/MM/yy, dd/MMM/yyy .. etc etc. And I am using the following code to convert all strings to one specific date format (yyyy-MM-dd) in hive :
select
from_unixtime(unix_timestamp('31/02/2021','dd/MM/yyyy'),'yyyy-MM-dd')
but this gives me 2021-03-03 in HIVE.
Is there any other way to identify such invalid dates and give null.
Assume, you recognized format correctly and it is exactly 'dd/MM/yyyy' and date is invalid one '31/02/2021'.
unix_timestamp function in such case will move date to the next month and there is no way to change it's behavior. But you can check if the date double-converted from original string to timestamp and back to original format is the same. In case it is not the same, then the date is invalid one.
case
-- check double-converted date is the same as original string
when from_unixtime(unix_timestamp(date_col,'dd/MM/yyyy'),'dd/MM/yyyy') = date_col
--convert to yyyy-MM-dd if the date is valid
then from_unixtime(unix_timestamp('31/02/2021','dd/MM/yyyy'),'yyyy-MM-dd')
else null -- null if invalid date
end as date_converted

Tableau Date function and Y0Y comparsion

I have Column that is in String format such as 2018-03 and I want to convert this to Date format in tableau. Once I have convert these 4 years data I want to compare 2019 data with 2018 and 2021 data with 2020s
To convert to a date use the DATE function:
DATE(LEFT([DateField],5)+RIGHT([DateField],2)+"01")
Check here to see 2 different ways to do YoY - do whichever works better for your use case.
Assuming that 03 in your date 2018-03 corresponds to month, you can convert the string column to date column with the following calculation, where my_date is your string column
DATE(DATEPARSE("yyyy-MM", [my_date]))
See the results
Note, if however, 03 is quarter, use this calculation instead
DATE(DATEPARSE("yyyy-qq", [my_date]))

How to convert string 'yyyy-mm-dd' to date format in presto sql

In presto SQL, the date is saved as a string like '2020-06-10'. I want to convert into a date format (yyyy-mm-dd)
This is how I did it:
select date_format(date_parse('2020-06-10', '%Y-%m-%d'),'%Y-%m-%d')
First I convert string to a timestamp format, then convert the new timestamp to date_format.
My question is that is there a function such that I convert only once?
For example
date_parse(string, format, expect_out_put_time_format)
You can use date function, which is a shortcut for CAST(x AS date).
presto> SELECT date('2020-06-10');
_col0
------------
2020-06-10

BCB6 How to display long date from DataSet Field in label?

I have a problem with displaying the correct date from a TDataSet.
In a TDataSet, I have a date in 'YYYY-MM-DD' format. On the computer, I have set the date in the d.mm.yy format, and so it has to stay, I can not change it.
Now, I have to take the date in the format 'YYYY-MM-DD' from the TDataSet and display it in a TLabel component in this format, but it always shows me the date in the 'YY-MM-DD' format, and that if the date is before 1969 then it adds 100 years.
I have my date in:
MyData->DataSet->Fields->FieldByName("date")
In my DB, my date is in YYYY-MM-YY format.
On my machine, I have the short date set to the 'DD-MM-YY' format, but I need to display my date in the label in 'YYYY-MM-DD' format.
I have no ideas for how to handle this.
TDateTime has a method named FormatString(). You can do this:
MyData->DataSet->Fields->FieldByName(L"date")->AsDateTime.FormatString("yyyy-mm-dd");

Converting string to date in Crystal Reports

I'm new to Crystal Reports, I'm trying to extract and display month and year from the string (In DB the data type of the column is varchar). Following is an example of the data.
05-JAN-12 11.49.28.000000000 AM
I need it in following format
Jan-12
I have used cDate to convert the string to date format but was unsuccessful, maybe I didn't do it right way.
Alter the formula to extract the date portion, then convert it to a date:
DateValue(Split("05-JAN-12 11.49.28.000000000 AM")[1])
Apply formatting to the field as desired.
This may work
Date (Year ({reportfield}) * 10000 + Month ({reportfield}) * 100 + Day ({reportfield}))
Then format the field to display the date as your choosing.
MonthName(DatePart ("m", <<Date field>>))+"-"+totext(DatePart ("yyyy", <<DAtefield>>),0,"")