This is my first post so any mistakes apologies in advance.
I am trying to convert a character string into a date format. My string is in the format '20110709_000000'.
I have tried the following queries after research but both return me the following error:
'Conversion failed when converting datetime from character string.'
I have tried
Select CAST(REPLACE('20110709_000000','_', '') AS DATETIME)
and
SELECT CONVERT(DATETIME,REPLACE('20110709_000000','_', ''),109).
Any help would be appreciated.
Assuming that 000000 is hhmiss you can use substring to get the parts and build a string on the format yyyymmdd hhmiss that you can cast to datetime.
declare #ds varchar(15) = '20110709_102030'
select cast(substring(#ds, 1, 8)+' '+
substring(#ds, 10, 2)+':'+
substring(#ds, 12, 2)+':'+
substring(#ds, 14, 2) as datetime)
Result:
-----------------------
2011-07-09 10:20:30.000
Do you need the 000000 at the end? This should work:
Select CAST(REPLACE('20110709_000000','_000000', '') AS DATETIME)
Related
Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);
I want to convert a String to datetime.
So I'm building this code:
SELECT CONVERT(datetime, '23:00', 103)
If I try to execute this code I have this:
1900-01-01 23:00:00.000
But if I try to execute this convert:
SELECT CONVERT(datetime, '24:00', 108)
I have this error:
Converting a varchar data type to datetime generated a value not within the range of allowed values.
Datetime type does not support "24th hour". Its time range is 00:00:00 through 23:59:59.997.
Try use instead
SELECT CONVERT(datetime, '00:00', 108)
For more details check datetime (Transact-SQL) page.
I'm going nuts trying to convert a string type column into date.
The column name is StartDate, which contains a string date format dd/mm/yyyy. The field type is varchar(3000).
I tried the following:
CONVERT(datetime, StartDate, 103)
CAST(CONVERT(VARCHAR(10), StartDate, 110) AS DATE)
CONVERT(DATE, RIGHT(StartDate, 4) + '-' + SUBSTRING(StartDate, 4, 2) + '-' + LEFT(StartDate, 2), 126)
and other similar combinations.
I keep getting "out of range" and "conversion failed" error messages.
Does anyone have a creative solution?
I suspect you have some bogus data. For example
Select try_convert(date, '15/07/2014', 103)
Returns
2014-07-15
If 2012+, I would suggest that you
Select *
From YourTable
Where try_convert(date, StartDate, 103) is null
This will identify your problem areas
I'm new to crystal report. I have a date in string format like 2015-03-25 (Wed) and I want to convert it to date format like 03/25/2015. I tried with CDate and DateValue but it returned bad date string format. Any suggestions to convert such date string to proper date format?
If you have a DateTime field in Crystal Reports, you will see Date and Time tab option on the Format Editor when you right click on the field and select Format Field menu item. From the Date and Time tab, you may select the desired format and select OK.
It would be recommended to use the formats you want to use.
For eg : if you are giving string format for money or decimal you may not be able to use it at its full,like you may not be able to auto sum and other properties related to the datatype you intend to use
Not to do any thing in the code, Crystal Report have facility to this type of simple format.
#utility, you are near to answer.
As above image, in last Custom Format option, where you just go in Date tab and give format as
http://www.c-sharpcorner.com/UploadFile/mahesh/DateFormatInCR06132007092248AM/DateFormatInCR.aspx
Updated : sorry for above answer, that will work if you have valid date string.
In your case, where any arbitrary string need to convert into other date format. There is 2 option. In both case you have to extract the date and then format as you need and again combined with other sub-string.
Second you already done ie. crsytal report side, grab the date , format it and concatenate. this will slow down as need to process for each row.
SqlServer side - This option is faster from first option.
declare #t nvarchar(16) = '2015-03-25 (Wed)'
--get the acual date select SUBSTRING ( #t, 1, charindex('(' , #t ) -1 )
--above result give the charter datatype, so you first convert into date and then convert into other format select cast( SUBSTRING ( #t,
1, charindex('(' , #t ) -1 ) as date) --convert into date select
convert (varchar(15) , cast( SUBSTRING ( #t, 1, charindex('(' , #t )
-1 ) as date) , 103) --convert into dd/mm/yyyy format
--Above is for your understand, this is the actual execution of your code (Only write the below line) select convert (varchar(15) , cast(
SUBSTRING ( #t, 1, charindex('(' , #t ) -1 ) as date) , 103) + ' ' +
datename(dw, getdate() )
I suggest, go with Sqlserver side.
Following is my oracle update query that`s throwing error--
Update table Set col1 = To_date(:dateFill, 'mm/dd/yyyy hh24:mi:ss') Where Fil1 = :ID;
dateFill = 01/05/2012,
ID= 15
this statement is executing in a procedure,
error -:ORA-01722: invalid number(date field)
Can someone tell me why is 'select To_date('01/05/2012 00:00:00', 'mm/dd/yyyy hh24:mi:ss') from dual;' giving me result like '05-JAN-2012 00:00:00'.???
Please suggest me some answers.
If dateFill = 01/05/2012, why are you specifying a date format that includes "hh24:mi:ss"?
An ORA-01722 error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a valid number. Valid numbers contain the digits '0' through '9', with possibly one decimal point, a sign (+ or -) at the beginning or end of the string, or an 'E' or 'e' (if it is a floating point number in scientific notation).
to_date always work with 'character' ie
to_date(char[,'format'[,nls_lang])
Your variable 'dateFill' is of Number datatype. Cast or convert this 'dateFill'
field into varchar and things will work.
Can someone tell me why is 'select To_date('01/05/2012 00:00:00',
'mm/dd/yyyy hh24:mi:ss') from dual;' giving me result like
'05-JAN-2012 00:00:00'.???
how a date displays is down to your client / Nls date format setting:
SQL> select To_date('01/05/2012 00:00:00','mm/dd/yyyy hh24:mi:ss') from dual;
TO_DATE('01/05/20120
--------------------
05-jan-2012 00:00:00
SQL> alter session set nls_date_format='mm/dd/yyyy hh24:mi:ss';
Session altered.
SQL> select To_date('01/05/2012 00:00:00','mm/dd/yyyy hh24:mi:ss') from dual;
TO_DATE('01/05/2012
-------------------
01/05/2012 00:00:00
SQL>