Add 'minutes' to a text (in date time format) and convert it back to original varchar format SQL Server - sql

I have a value stored as Varchar(50) as "2016-07-21 16:35:05". I want to add 5 minutes to it. How do I do it? Expected output "2016-07-21 16:40:05" as a Varchar.
I've so far tried converting the value to DATETIME and adding 5 minutes. But when I try to convert it back to varchar, it doesn't show up in the same format.
SELECT
CAST(DATEADD(MINUTE, 5, CAST([PickTime] AS DATETIME)) AS VARCHAR(50))
FROM
rawdata
Output I get: Jul 21 2016 4:40PM
But I wanted: 2016-07-21 16:40:05
Could anyone guide me ?
Thanks

SELECT FORMAT(DATEADD(MINUTE, 5, cast([PickTime] as datetime)),'yyyy-MM-dd HH:mm:ss')
FROM rawdata

Related

Convert varchar containing various formats to DATETIME

I have a record_created column of type varchar containing multiple values formatted in two different ways throughout.
2017-04-17 16:55:53.3840460
Sep 18 2015 11:25PM
How can I convert this column into a DATETIME to be compared to GETDATE?
GETDATE() is SQL Server specific if so, then you can use try_convert() :
select cast(try_convert(datetime2, col) as datetime)
from table t
where try_convert(datetime2, col) is not null;
However, if the string date is exactly the same format which you have provide then you can simply do casting :
select cast(cast(col as datetime2) as datetime)
from table t;
If you are using SQL Server, then you may be able to use the CONVERT function here:
SELECT
CONVERT(datetime, LEFT('2017-04-17 16:55:53.3840460', 23), 121) AS date1,
CONVERT(datetime, 'Sep 18 2015 11:25PM', 100) AS date2;
Your first type of timestamp seems to work with mask 121, and the second one works with mask 100. The demo link below shows that the conversions are working.
Demo

Dutch varchar date issue in SQL server 13 month

I have the following datetime format ( as varchar ) in my database 13-04-2018 1:05:00.
I need to convert it to the following format: 2018-04-13 01:05:00. As datetime.
Normal convert functions can't do this because they try to take the 13th month, and that month doesn't exist. This error:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
Does someone know how to convert this date issue?
Using datetimes is always a pain regardless of the language because of all the different formats across the world.
To sort your issue out currently, you need to use a format style which is a third parameter to CONVERT. Personally what I would suggest here is to store as a datetime, because storing datetimes as strings is never a good idea. It just gets too messy later on, but if saved in the format you would like, it would be saved as yyyy-MM-dd hh:mm:ss
SELECT CONVERT(DATETIME, '13-04-2018 1:05:00',103)
You can create your own function to format it in your desired output string.
CREATE FUNCTION FormatMyDate
(#Date DATETIME) RETURNS VARCHAR(20)
AS
BEGIN
RETURN FORMAT(#Date,'yyyy-dd-MM hh:mm:ss')
END
And then you can call it in SELECT statements like this:
SELECT dbo.FormatMyDate(yourDateCol)
FROM yourTable
this takes the date from the format where month comes before day and reverses the 2 values (month and day)
SELECT CONVERT(DATETIME, '2018-13-04 01:05:00', 103);
Results:
2018-04-13 01:05:00.000
This should work for you requirement...
SELECT FORMAT(yourdate, 'yyyy-dd-MM')
Your Solution Bro...
DECLARE #d DATETIME = GETDATE()
SELECT FORMAT ( #d, 'MM-dd-yyyy hh:mm:ss', 'de-de' ) AS 'Hoping your result'

how to convert the 24 hr to 12 hr in sql

I have column of 24 hr and i need to change it to 12 hr, Please help .
Start time
174300
035800
023100
The result should be
Start time
05.43 PM
03.58 AM
02.31 AM
Use STUFF function to convert string to Time format
SELECT CONVERT(VARCHAR,CAST(STUFF(STUFF(ColumnName,3,0,':'),6,0,':') AS TIME),100)
Using one of the examples above - the following will work.
You need to split the data into hours/minutes and cast it to time format, than convert it to the relevant type:
declare #data int
set #data = 174300
select convert(VARCHAR(15),cast(cast(left(#data, 2 )as varchar(2)) + ':' + cast(substring(cast(#data as nvarchar(6)), 3,2 )as varchar(2) ) as time),100)
Don't store time as varchar, instead alter your table and change the column type to datetime.
SELECT right(convert(varchar(25), Start Time, 100), 7)
The 100 you see in the function specifies the date format mon dd yyyy hh:miAM (or PM), and from there we just grab the right characters.
You can see more about converting datetimes here.

Data in SQL not in proper date format, how to change it into the right format?

I have a column called ProcessTimeOnHold that shows how long it takes us to process an order.
Any order that takes less than 24 hours to process is shown in this format HH:MM:SS. As far as I can tell this data is in the proper date format however any order that takes longer than 24 hours shows up in this format D.HH:MM:SS So, if an order took 29 hours and 34 minutes it will show up as 1.05:34:00.
The problem I have is if I try to convert or manipulate the data for example into minutes the orders in the proper data format will work but anything after 24 hours will return an error.
For example if I try to convert to minutes with this:
LTRIM(DATEDIFF(MINUTE, 0, ProcessTimeOnHold))
It will convert all orders into minutes until it runs into an order with the D.HH:MM:SS format and then it will return this error:
Error Message: Conversion failed when converting datetime from
character string
Any way that I can change my entire column into datetime?
You might want to search for a less verbose solution, but this is one option, using string manipulation:
SELECT DATEDIFF(MINUTE, 0, (convert(time,RIGHT(ProcessTimeOnHold,8)))) +
(case when ProcessTimeOnHold like '%.%'
then
(cast((LEFT(ProcessTimeOnHold,CHARINDEX(ProcessTimeOnHold,'.')-1)) AS INT) * 24 * 60)
else 0 end) as TotalMinutes
(Edited based on comment)
Here's a way to get just the minutes:
declare #table table (ProcessTimeOnHold varchar(16));
insert #table (ProcessTimeOnHold)
values ('00:20:00')
, ('01:20:00')
, ('1.05:34:00');
select ProcessTimeOnHold
, datediff(minute,0,right(ProcessTimeOnHold,8))
+ isnull(nullif(replace(left(ProcessTimeOnHold,charindex('.',ProcessTimeOnHold)),'.',''),''),0) * 1440 'Minutes'
from #table
RETURNS:
ProcessTimeOnHold Minutes
----------------- -----------
00:20:00 20
01:20:00 80
1.05:34:00 1774
Try this one-
ANSI format: 2006.10.23
SELECT [ANSI]=CONVERT(char,CURRENT_TIMESTAMP,102)

Converting a numeric value to date time

I am working in SQL Server 2012. My date column in a data set looks like this: 41547. The column is in nvarchar (255). I want to convert it to something like this: yyyy-mm-dd hh:mm:ss (Example: 2013-09-14 12:23:23.98933090). But I can not do this. I am using following code:
select convert(datetime, date_column, 6)
But this is giving following error:
Msg 241, Level 16, State 1, Line 1 Conversion failed when converting
date and/or time from character string.
What am I doing wrong?
Your date is actually a numeric value (float or integer), stored in a char column. So, you need to convert it to a numerical value (in this case, to float) first, like:
select convert(datetime, CONVERT(float,date_column))
A value of 41547.5 will result in:
`2013-10-02 12:00:00`
The style argument, in your case 6 is only necessary when converting from or to char-types. In this case it is not needed and will be ignored.
NB: The float value is the number of days since 1900-01-01.
e.g. select convert(datetime, CONVERT(float,9.0)) => 1900-01-10 00:00:00; the same as select dateadd(day,9.0,'1900-01-01') would.
The decimal part of the number also equates to days; so 0.5 is half a day / 12 hours.
e.g. select convert(datetime, CONVERT(float,.5)) => 1900-01-01 12:00:00. (Here our comparison to dateadd doesn't make sense, since that only deals with integers rather than floats).
There is an easier way to do it as well.
select convert(date,cast (date_Column+ 19000000 as nvarchar(10)))
as date_Column_Formated
from table_Name
I have just found the way to do this.
First I have to covert the nvarchar to int then I have to convert it to date time. I have used following code:
Select convert(datetime, (convert (int, [date_column])), 6) as 'convertedDateTime' from mytable
6 format is: "dd mon yy"
Like this: SELECT convert(datetime, '23 OCT 16', 6)
Other formats will cause your error
SELECT CONVERT(DATETIME,CONVERT(INT,date_column))