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.
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 have a text field that is in this format May 21 2013 9:45AM. How would I convert that to datetime? I tried the following
UPDATE WaterRevLienInfo
SET LienDate = CONVERT(DATETIME, CONVERT(VARCHAR(30), LienDate), 101)
It works as a select and comes out like 2013-05-21 09:45:00.000 but not as an update. Any help would be great.
You appear to be "round tripping" the the LienDate field by casting it to a varchar and then back to a datetime. I'm not sure what this is accomplishing.
However, if you have a text value in the format you specified (which is, by default, how SQL represents datetime fields when casted to a varchar), you can just do a straight convert:
DECLARE #DateText varchar(30) = 'May 21 2013 9:45AM';
UPDATE WaterRevLienInfo
-- Convert the DateText string value for storage in a datetime field.
SET LienDate = CONVERT(datetime, #DateText);
This should also work with other fields in the same table.
UPDATE WaterRevLienInfo
SET LienDate = CONVERT(datetime, DateTextField);
The datetime hasn't any format it just a data. The presentation must care about the format.
Assume a user tries to cast a number (year of birth) into a date.
select year_of_birth, cast(year_of_birth as date) from visit_occurrence
And gets this error :
cannot cast type smallint to date
What is the proper way?
e.g., cast(cast(YOB as string)+'-01-01' as date) does not work either.
use
select year_of_birth, to_date(cast(year_of_birth as text), 'YYYY') from visit_occurrence ;
A column of varchar data type whose values looks like:
01:23:21
00:45:00
10:00:00
05:01:04
need summing up. But as I do, I get the error:
"Operand data type varchar is invalid for sum operator."
How do I sum the above value while still retaining the format 00:00:00?
Thank you
Time does not support sum but datetime does
select convert(time, CONVERT(datetime, '00:45:00') + CONVERT(datetime, '01:00:04'))
Is there any way that I can convert the DateTime SQL value of this:
2011-10-11 00:00:00.000
to a string formatted exactly the same?
'2011-10-11 00:00:00.000'
I tried doing cast(fl_from_dt as varchar(50) but it converts it to something readable - ex. 'Oct 11 2011 12:00AM'
Are you using SqlServer?
Take a look at the style parameter of CONVERT() here: http://msdn.microsoft.com/en-us/library/ms187928.aspx - in particular have a look at the ODBC canonical (with milliseconds) format.
For short you should use style 121 in the CONVERT() command.
Style: 121
ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffset
yyyy-mm-dd hh:mi:ss.mmm(24h)
Try this one !
select CONVERT(VARCHAR(10), GETDATE(), 112)
+ right('0'+cast(datepart(hh, GETDATE()) as varchar(2)),2)
+ right('0'+cast(datepart(mi, GETDATE()) as varchar(2)),2)
+ right('0'+cast(datepart(ss, GETDATE()) as varchar(2)),2) as 'DateTime_STR2'