compare current datetime between 2 date - sql-server

Here is my code:
declare #fdate varchar(50)
declare #tdate varchar(50)
declare #diff int
declare #CalFromdate datetime
declare #CalTodate datetime
set #diff = 360
set #fdate = '2016-12-19 09:30:00'
set #CalFromdate = DATEADD(mi,#diff,CONVERT(DATETIME, #fdate))
print #CalFromdate
set #CalTodate = DATEADD(mi,#diff,CONVERT(DATETIME, #tdate))
print #CalTodate
gives me Dec 19 2016 3:30PM as fromdate
I don't want this format.
Either '2016-12-19 09:30:00' or '2016-12-19 09:30:00:0000'. I want to compare my current date getdate() with 2 dates. If I use above output format with my gatedate format, will it work or both should be in same format?

You are only seeing the date in the format Dec 19 2016 3:30PM because that is the completely arbitrary formatting that is applied in SSMS when printing a datetime value.
If you change your code to select instead of print you will return your datetime value in the format 2016-12-19 15:30:00.000 but again, this is just an arbitrary formatting used to display a type of data.
Within your query, your datetime value doesn't actually have a format. It is simply a date and time. Use it in your comparison and worry about the formatting in whatever application you are presenting the data in.
As an aside, you also don't need to bother using convert on your #fdate or #tdate parameters, as you have already specified them as datetime values in your declare statements.

Related

Convert varchar 'hh:mm:ss' to datetime hh:mm:ss in SQL Server

I tried to convert a varchar variable stored in my database as HH:MM:SS to an actual datetime format HH:MM:SS. I did get the value for HH:MM:SS but the attempt also prefixed the YYYY:MM:DD along with the expected result.
Following is the code that I used to convert this varchar value to HH:MM:SS and the result I got.
Code I tried :
DECLARE #Duration Varchar(10)
SET #Duration = '00:01:23'
SELECT CONVERT(datetime, Duration, 8) AS duration
The output I got :
1900-01-01 00:01:23.000
The expected output:
00:01:23
Please let me know what needs to be changed in this. Thank you!
If you want time, why are you converting to datetime? Given the name it shouldn't be surprising you get both date and time. Try:
DECLARE #Duration char(8) = '00:01:23';
SELECT duration = CONVERT(time(0), #Duration);
Results:
duration
00:01:23
Example db<>fiddle
Just keep in mind that time (nor any date/time type) is not meant to represent a duration or interval. Because what happens when your duration or interval exceeds 24 hours?
What you posted is a time, not a date or datetime. A duration isn't a date. The date types are binary, they don't have prefixes.
You can define a time directly with :
Declare #Duration time ='00:01:23'
Or you can cast a string to a time:
Declare #Duration varchar(10)
Set #Duration = '00:01:23'
Select cast(#Duration as time) as duration
or
Declare #Duration varchar(10)
Set #Duration = '00:01:23'
Select convert(time, #Duration,8) as duration
Unfortunately that's not a duration, it's a time of day. It can only store values between 00:00 and 23:59:59.9999999.
SQL Server has no interval/duration type.
Declare #Duration Varchar(10)
Set #Duration = '00:01:23'
select convert(time,#duration,8);

Change default datetime format when converting to string

When a datetime is automatically converted to a string it uses this undesired format:
mon dd yyyy hh:miAM
how can I change this default so it is a more typical format like:
yyyy-mm-dd hh:mi:ss.mmm
Also, I don't want to have to explicitly use a cast/convert each time:
Convert(char(23),__date__,121)
I'd like to use CONCAT() without wrapping a convert around the datetimes. I generally keep appending a string variable by dumping in local variables, etc. as my stored procedures process. In the CATCH, I write this string out, but whenever datetime values are concatenated in, it uses the undesired format.
Sample code:
Declare #Now datetime=GETDATE()
,#Temp varchar(100)
SET #Temp=CONCAT('It is now: ',#now)
PRINT #Temp -----------------------It is now: Feb 8 2019 1:45PM
SELECT #Now -----------------------2019-02-08 13:45:05.247
Print #Now ------------------------Feb 8 2019 1:45PM
Print Convert(char(23),#Now,121)---2019-02-08 13:45:05.247
Output:
It is now: Feb 8 2019 1:45PM
-----------------------
2019-02-08 13:45:05.247
(1 row affected)
Feb 8 2019 1:45PM
2019-02-08 13:45:05.247
If you are writing new code, and you are ok with creating objects to support your desired format, then you can do it in a function.
CREATE FUNCTION dbo.fn_NewDate()
RETURNS varchar(23)
AS
BEGIN
RETURN(CONVERT(VARCHAR(23), GETDATE(), 121));
END
GO
Declare #Now varchar(23) = dbo.fn_NewDate()
,#Temp varchar(100);
SET #Temp=CONCAT('It is now: ', #now)
print #Temp;
Result:
It is now: 2019-02-08 12:43:38.980

Adding two time fields together

I have two columns within my table they are set as nvarchar fields but contain time values.
one column is a time field one is the duration field
eg.
Time 1 = 15:05:22 (time field)
Time 2 = 00:02:00 (duration field)
I want to output Time 1 + Time 2 = 15:07:22
I have tried CAST(time1 as datetime)+CAST(time2 as datetime)
but I get 1900-01-01 15:07:22.000, and I don't want the date part. I can't use cast as time as I get an error I presume this is because the fields are set as nvarchar and not date/time?
Just cast the result to time to get rid of the date portion:
DECLARE #time_txt varchar(8);
DECLARE #duration_txt varchar(8);
SET #time_txt = '15:05:22';
SET #duration_txt = '00:02:00';
SELECT CAST(CAST(#time_txt as datetime) + CAST(#duration_txt as datetime) as time);
-- yields the time value 15:07:22.0000000
If you need this as a string (for example, in hh:mm:ss format), you can use CONVERT with the appropriate format option:
...
SELECT CONVERT(varchar(8), CAST(#time_txt as datetime) + CAST(#duration_txt as datetime), 108);
-- yields the string 15:07:22
PS: In general, you should use time columns for time values instead of varchar columns. Unfortunately, SQL Server does not have a really good data type for durations (time spans).
select dateadd(second,datediff(second,0,time1),time2) as Time3
from your_table

SQL Server Convert int to Datetime

I'm trying to convert some data that has datatype int. However when I tried to convert 200 into year, it throws an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Code:
declare #MonthStart datetime,
#Day int = 1,
#Month int = 5,
#Year int = 200 /*gives me an error if I input this but when I tried 2016 the code works fine*/
set #MonthStart = Cast(#Month as varchar(10)) +'-'+ Cast(#Day as varchar(10))+'-'+Cast(#Year as varchar(10))
select #MonthStart
I don't know what's the problem on this. Maybe the datetime won't accept this kind of format 200-05-01 as a date format.
As the error pointed out, you have an out-of-range value. The minimum value for DATETIME is:
1753-01-01 00:00:00.000
What you're tring to convert is below the minimum, thus the error.
As commented by marc_s:
And if you're on SQL Server 2008 or newer, you can get around this
problem by using DATETIME2(3) instead of DATETIME. This new datatype
doesn't have those "artificial" range limitations as the old one did.
You can use datetime2 instead of datetime. It supports dates of 1-1-0001 or later. https://msdn.microsoft.com/en-us/library/bb677335.aspx

failed when converting date and/or time

I am getting value from store procedure in date is 3/25/13 10:06:38 AM and i want to convert this 03/25/2013 10:18:28 . How can i convert this in sql server . Because when i cast this in date time i am getting error . Below #PODTimeOnly is varchar(255) Here i am casting only time and after this i want to cast and store date in another variable.
SET #PODTimeOnly = cast(#PODTime as time)
ERROR MSG :
Conversion failed when converting date and/or time from character string.
Thanks
Not sure I totally understand the desired output, but if you're forced to hack strings together to generate differently formatted datetime values, this may work.
DECLARE #strDate VARCHAR(50) = '3/25/13 10:06:38 AM',
#dtDate DATETIME,
#DateOnly VARCHAR(255),
#TimeOnly VARCHAR(255),
#output VARCHAR(255)
-- cast to datetime first to verify its a valid date
SET #dtDate = CAST(#strDate AS DATETIME)
-- parse/cast date and time to seperate variables
SET #DateOnly = CONVERT(VARCHAR(255),#dtDate,101)
SET #TimeOnly = CONVERT(VARCHAR(255),#dtDate,108)
-- duct tape them back together in the desired string format
SET #output = #DateOnly + ' ' + #TimeOnly
-- outputs '03/25/2013 10:06:38'
SELECT #output AS 'NewStringDate'

Resources