I have a column with strings as datetimes. I want to convert the whole column to datetime format.
Example
"19.03.2020 08:14:13"
"30.04.2020 08:57:45"
I tried this:
select *,
PARSE_DATETIME("%d%m%y %H:%M:%S", Example) as Date_example
from xx.yy.zz
but i got an error
Failed to parse input string "19.03.2020 08:14:13"
Can anyone help me and tell me why this is not working? Is it because day,month and year are separated by the dot instead of slash?
Consider:
PARSE_DATETIME('%d.%m.%Y %H:%M:%S', example) as Date_example
Rationale:
your original format specifier is missing the dot separator between the date components
you want %Y rather than %y for the year component (the former is a 4 digits year, while the later is 2 digits)
We can shorten the query a little with %T:
PARSE_DATETIME('%d.%m.%Y %T', example) as Date_example
Related
I am trying to convert April 2, 2012, 12:00 AM into a DATETIME/DATE/TIMESTAMP type I dont really mind just not a string.
The problem here however is that within the PARSE_DATETIME function the format string, specifically the %e element string which is for the single digit day is always preceded by a space and in my string I need a comma. The docs read The day of month as a decimal number (1-31); single digits are preceded by a space..
My parse function is as follows PARSE_DATETIME('%B %e, %Y, %I:%M %p','Timestamp) AS 'Date' but I get back the error No matching signature for function PARSE_DATETIME for argument types: STRING, DATETIME. Supported signature: PARSE_DATETIME(STRING, STRING) at [4:5]
Any help would be much appreciated.
You are likely seeing the error message because the timestamp field you are using is already a datetime. Your format string functions as expected with sample data:
with sample_data as (
select 'April 2, 2012, 12:00 AM' as input_string
)
select PARSE_DATETIME('%B %e, %Y, %I:%M %p', input_string) from sample_data
Which results in:
I would make sure your input data is as expected
My date type looks like 7-Feb-20 (character) data, and I need to convert this into a data/time, so eventually I can use "As.Date() or as.POSIXct()" to move it into the SQL server as datetime. SQL server uses datetime as default when moving from R to SQL.
The as.Date() function will convert character data into a date format.
You can specify the input format by using the format = argument.
In this case, it looks like your format is %d-%b-%y
%d will give you day
%b will give you abbreviated month name (compare with %B, which gives full month name)
%y will give you two-digit year.
You will also need to include the hyphen in your format.
I Want to parse or cast the following column which is the format of this example STRING:
18 Apr 2016 10:17:50
into one new column as a DATETIME in this format:
YYYY-MM-DDTHH:MM:SS
So the above would read as an example of one of the thousands of rows:
2016-04-18T10:17:50
I'm not sure what's throwing this one off but I either get a column full of nulls or a message saying:
Mismatch between format character ':' and string character ' '
I've tried parse_Datetime and safe.parsed_datetime with the correct syntax and format elements as well as casting. I'm obviously doing something wrong.
use PARSE_DATETIME you find there also more parameters
SELECT PARSE_DATETIME("%d %b %Y %T", "18 Apr 2016 10:17:50") as dt
We have a file that needs to be imported that has dates in it. The dates are in a format that I have not seen before and the day part can vary in length (but not the month or year seemingly) and position based on wether the number is double digit or not, i.e.
Dates:
13082014 is 13th February 2014
9092013 is 9th September 2013
The current script tries to substring the parts out, but fails on the second one as there is not enough data. I could write an if or case to check the length, but is there a SQL format that can be used to reliably import this data?
To clarify this is MSSQL and the date format is ddmmyyyy or dmmyyyy
One of the simple way is using STUFF.
example:
select STUFF(STUFF('13082014 ',3,0,'/'),6,0,'/');
//result: 13/08/2014
Good luck.
LPAD a zero when it is missing so to always get an eight character date string. Here is an example with Oracle, other DBMS may have other string and date functions to achieve the same.
select to_date(datestring, 'ddmmyyyy')
from
(
select lpad('13082014', 8, '0') as datestring from dual
union all
select lpad('9092013', 8, '0') as datestring from dual
);
Result:
13.08.2014
09.09.2013
you can convert the dates to a relevant date format then import data(based on the dateformat change the logic).
something like this :
select Convert(varchar(10),CONVERT(date,YourDateColumn,106),103)
I am trying to get the date portion of a datetime field. I know I can get it with date_format, but that returns a string or "varchar" field. How can I convert the result to date and not as varchar?
This is my query returning the varchar:
(Select Date_Format(orders.date_purchased,'%m/%d/%Y')) As Date
I tried several combinations from this question, but could not make it to work:
mysql query - format date on output?
Any help is appreciated.
Try to cast it as a DATE
SELECT CAST(orders.date_purchased AS DATE) AS DATE_PURCHASED
Use the DATE function:
SELECT DATE(orders.date_purchased) AS date
Either Cybernate or OMG Ponies solution will work. The fundamental problem is that the DATE_FORMAT() function returns a string, not a date. When you wrote
(Select Date_Format(orders.date_purchased,'%m/%d/%Y')) As Date
I think you were essentially asking MySQL to try to format the values in date_purchased according to that format string, and instead of calling that column date_purchased, call it "Date". But that column would no longer contain a date, it would contain a string. (Because Date_Format() returns a string, not a date.)
I don't think that's what you wanted to do, but that's what you were doing.
Don't confuse how a value looks with what the value is.
I see the many types of uses, but I find this layout more useful as a reference tool:
SELECT DATE_FORMAT('2004-01-20' ,'%Y-%m-01');
syntax of date_format:
SELECT date_format(date_born, '%m/%d/%Y' ) as my_date FROM date_tbl
'%W %D %M %Y %T' -> Wednesday 5th May 2004 23:56:25
'%a %b %e %Y %H:%i' -> Wed May 5 2004 23:56
'%m/%d/%Y %T' -> 05/05/2004 23:56:25
'%d/%m/%Y' -> 05/05/2004
'%m-%d-%y' -> 04-08-13