Converting date/time in sql to string - tsql

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'

Related

SQL date conversion to mm/dd/yyyy hh:mm

Can't seem to find answer to what seems like a simple question. I have a date that is returned
2022-02-07 18:53:36.000 I need to convert that date to mm/dd/yyyy hh:mm.
Is that possible?
I got the answer from another site. I've tested it and it works.
(CONVERT(VARCHAR(20), ea.event_timestamp, 101) + ' ' + LEFT(CONVERT(VARCHAR(20),ea.event_timestamp, 108) ,5)) AS EventDate
select convert(varchar, getdate(), 13);

Converting Integer values to Date in Presto SQL

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);

How to convert time String to datetime?

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.

Casting DATETIME on concatenated date and time

I'm trying to concatenate a column's date to a fixed time of the day and then CAST the whole thing as DATETIME.
The fixed time is 5:30am.
The date column I'm using needs to be adjusted as it shows the end date/time of when something ran; I want to use the start date/time.
The start date/time time is not available as its own column, but I have another column that has the duration the process took in seconds, so I can use DATEADD to roll the end date/time back to the start date/time.
Here's the full statement:
CAST(CONVERT(VARCHAR(10), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 103) + ' ' + '05:30' as DATETIME)
Here's the error message I'm receiving:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
I've tried testing these statements to investigate the issue, but they all run OK on their own:
CAST(CONVERT(VARCHAR(10), GETDATE(), 103) + ' ' + '05:30' as DATETIME)
CAST('2017-03-02' + ' ' + '05:30' as DATETIME)
DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate])
I'm a bit stuck on how to get round this issue. Any help would be much appreciated.
Clearly, you must have some unexpected values in the column.
I would suggest finding them using a query such as this:
SELECT LastExecutedDuration, LastExecutedDate
FROM ConfTask
WHERE TRY_CONVERT(datetime,
CONVERT(VARCHAR(10),
DATEADD(second,
-ConfTask.[LastExecutedDuration],
ConfTask.[LastExecutedDate]
)
103
) + ' ' + '05:30')
)
You can also simplify the logic, by just using date functions:
select dateadd(minute,
5 * 60 + 30,
convert(datetime,
convert(date,
dateadd(second,
- ConfTask.LastExecutedDuration
ConfTask.LastExecutedDate
)
)
)
)
This worked:
CONVERT(DATETIME, CONVERT(CHAR(8), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 112)) + ' ' + CONVERT(CHAR(8), '05:30:00', 108)

Convert nvarchar to DateTime in this format 'May 21 2013 9:45AM'

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.