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);
Related
The column is a nvachar(20).
select [shipment_posted_date_arch]
FROM [RxIntegrity].[dbo].[DiscrepancyReport_Receipts]
When I pull this, the date is in this format of 1200819 for that column. I need to convert to normal date.
This seemed to work:
cast(convert(nvarchar(20), (19000000 + CONVERT(int, shipment_posted_date_arch))) as date)
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 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)
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'
I have a .NET 2010 app that bangs against a SQL db. On the app side, a user can search on Begin date and End Date. Bot of these are just Month + Year. I then format them so they are complete dates. So when they go to the stored proc they'll look like this...
Begin Date: 1/1/2011
End Date: 5/31/2011
But the date in the db is broken up into 3 int fields, Month,Day & Year, ...of which Day may or may not be filled in (0 if not). It would be ok for this to always default to one when running this query. So if the values in the db were Month=3, Day=0 Year=2011 I would like the sql statement to render as
Where FORMATTEDDATEHERE between '1/1/2011' and '5/31/2011'
I just can't figure out how to format sql fields in a where clause.
Did you try something like
WHERE CONVERT(DATETIME,
CONVERT(VARCHAR(4), [c_year]) + '/'
+ CONVERT(VARCHAR(2), [c_month]) + '/'
+ CONVERT(VARCHAR(2), [c_day]))
BETWEEN '2011/1/1' AND '2011/5/31'
Hope it helps
you could build the date using datadd functions within the WHERE clause EG.
SELECT ...
WHERE DATEADD(Day, field_days-1, DATEADD(Month, field_months-1, DATEADD(Year, field_years-1900, 0))) BETWEEN '1/1/2011' AND '5/31/2011'
Just replace field_days, field_months, field_years with the int fields on the table for days, months and year. This will return all records within the date range.
Is this what you require?
WHERE CAST(Year AS varchar) + RIGHT(100 + Month, 2) +
RIGHT(100 + COALESCE(NULLIF(Day, 0), 1), 2) BETWEEN ...