How to set value on varchar to datetime variable [duplicate] - sql

This question already has answers here:
SQL Server Convert Varchar to Datetime
(9 answers)
Closed 8 years ago.
I have two variables, one is varchar another one is datetime. I need to convert varchar to datetime
declare #startdate as varchar(10)= '1/1/2013 1:60AM'
declare #enddate as varchar(10)= '1/1/2014 1:60AM'
declare #startdTime datetime
set # startdTime =cast(#startdate as datetime)
This conversion however causes an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
How to convert this without error? I see several example on stackoverflow but fail to get solution,need help on this issue.
IF have any query please ask, thanks in advance

The problem with your input string 1:60AM to 1:06AM
declare #startdate as varchar(20)= '1/1/2013 1:06AM'
declare #enddate as varchar(20)= '1/1/2014 1:06AM'
declare #startdTime datetime
SELECT CONVERT( VARCHAR(20), #startdate ,101)
SELECT CAST(CONVERT( VARCHAR(30), #startdate ,101) AS DATETIME)

make Sure about time . is it 1.60AM
declare #startdate as varchar(50)= '01/01/2013 1:00 AM'
declare #enddate as varchar(50)= '01/01/2014 1:00 AM'
declare #startdTime datetime
set #startdTime =cast(#startdate as datetime)
print #startdTime

Here '1/1/2013 01:60 AM' is more than 10 character. Increase it to 20 or more
Like
#startdate AS VARCHAR(25)
#enddate AS VARCHAR(25)
N:B:
There is nothing like '1:60 AM',Make it "2:00 AM" or "1:06 AM"

Different type of Date time formats for MS SQL

Related

Convert NVARCHAR to DATE SQL [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am having trouble converting this nvarchar to date: I want to convert '2021-02-01 00:00:00.0000000' nvarchar to 2021-02-01 date
DECLARE #var nvarchar(255) = '2021-02-01 00:00:00.0000000'
CONVERT(date, #var, 103)
I have used this convert function but I'm getting:
Conversion failed when converting date and/or time from character string
error. Any help on how to solve this problem?
Try 120 instead of 103. 103 expects a string in the format d/m/y.
DECLARE #var nvarchar(255) = '2021-02-01 00:00:00.0000000';
SELECT CONVERT(date, #var, 120);
Result (db<>fiddle):
2021-02-01
Also:
Why nvarchar? Dates don't need to support Unicode characters.
Try to get the source to correct the format to a standard, unambiguous format, like:
yyyy-mm-ddThh:mm:ss.nnnnnnn
yyyymmdd hh:mm:ss.nnnnnnn
Much more on dates: Dating Responsibly
You can CAST that format directly to a DATE or DATETIME2 type.
DECLARE #var nvarchar(255) = '2021-02-01 00:00:00.0000000'
DECLARE #date date = CAST(#var AS DATE);
SELECT #var as var, #date as [date];
Or even CONVERT it without a format number.
DECLARE #var nvarchar(255) = '2021-02-01 00:00:00.0000000'
DECLARE #datetime2 datetime2 = CONVERT(DATETIME2, #var);
SELECT #var as var, #datetime2 as [datetime2];
But to CAST or CONVERT it to a DATETIME, then it needs to be truncated.
The default format number is 121 (ODBC canonical with milliseconds)
DECLARE #var nvarchar(255) = '2021-02-01 00:00:00.0000000'
DECLARE #datetime datetime = CONVERT(datetime, LEFT(#var, 23), 121);
SELECT #var as var, #datetime as [datetime];

SQL SERVER Convert UTC DATETIME to local date time

I have a table with the column CreatedDateTime as shown below :
[CREATED_DATE_TIME] [datetime] NOT NULL DEFAULT GETUTCDATE()
When I am retrieving the created date time, it is giving me datetime value in UTC, but I want to show the datetime value in local time.
How can this be achieved?
Looking forward to your answers and thanks in advance.
Try like below
— Convert a UTC Time to a Local Time
DECLARE #UTCDate datetime
DECLARE #LocalDate datetime
DECLARE #TimeDiff int
— Figure out the time difference between UTC and Local time
SET #UTCDate = GETUTCDATE()
SET #LocalDate = GETDATE()
SET #TimeDiff = DATEDIFF(hh, #UTCDate, #LocalDate)
— Convert UTC to local time
DECLARE #DateYouWantToConvert datetime
DECLARE #ConvertedLocalTime datetime
SET #DateYouWantToConvert = '4/25/2007 18:00'
SET #ConvertedLocalTime = DATEADD(hh, #TimeDiff, #DateYouWantToConvert)
— Check Results
PRINT #ConvertedLocalTime

SQL date conversion HHMMSS.CCCNNNNNN to yyyy-mm-dd hh:mi:ss.mmm

I have data in this format : 114643.052303537 (HHMMSS.CCCNNNNNN).
I need to convert it to this format : 2018-04-25 12:40:59.573 (yyyy-mm-dd hh:mi:ss.mmm), strip of the date part ( i.e. 2018-04-25 ) and calculate the time difference between two formats.
Could you please help with this?
I need the time difference in hh:mi:ss.mmm format
The way to get this is to convert BOTH values to milliseconds (looking at only the time portion for the value that has a date); calculate the difference with simple subtraction, and then convert the result to hh:mi:ss.mmm with division and modulo operations.
declare #dt datetime = '2018-04-25 12:40:59.573'
declare #dunno varchar(16) = '114643.052303537'
Strip the date off the datetime and give it today's date
getdate() + right(convert(varchar,#dt,113),12)
Convert the varchar to time and give it today's date
getdate() + left(stuff(stuff(#dunno,3,0,':'),6,0,':'),8)
Find the milliseconds between them
datediff(millisecond,getdate() + left(stuff(stuff(#dunno,3,0,':'),6,0,':'),8),getdate() + right(convert(varchar,#dt,113),12))
Put it all together in your format
select
convert(char(13),
dateadd(millisecond,
datediff(millisecond,getdate() + left(stuff(stuff(#dunno,3,0,':'),6,0,':'),8),getdate() + right(convert(varchar,#dt,113),12)),
'01/01/00'),
14)
Depending on the speed of your server and other code, it'd be wise to use a variable for GETDATE() at the beginning to prevent millisecond, or even second differences during conversion.
declare #dt datetime = '2018-04-25 12:40:59.573'
declare #dunno varchar(16) = '114643.052303537'
declare #today datetime = getdate()
declare #dunno2 datetime
declare #dt2 datetime
set #dt2 = #today + right(convert(varchar,#dt,113),12)
set #dunno2 = #today + left(stuff(stuff(#dunno,3,0,':'),6,0,':'),8)
select
convert(char(13),
dateadd(millisecond,
datediff(millisecond,#dunno2,#dt2),
'01/01/00'),
14)

How to get datetime without time with t-sql? [duplicate]

This question already has answers here:
Best approach to remove time part of datetime in SQL Server
(23 answers)
Closed 8 years ago.
Suppose I have variable as:
declare #dt datetime
declare #d datetime
set #dt = getdate()
then I want to get date from #dt and assign it to #d. For example, if #dt='2014-07-02 2:30:22'
#t should be #d = '2014-07-02 00:00:00'
data type for #d should be same datetime, not varchar.
You could cast it to DATE datatype
SELECT CAST(GETDATE() AS DATE)

Convert string date to YYYYMMDD - VB.NET/SSIS SQL

I have got this line
Declare #Startdate as Varchar(50)
Set #StartDate = dateadd(dd,-1,convert(datetime, convert(varchar, getdate(),101)))
This returns me
2014-05-19 00:00:00.000
Now I want to convert above to
20140519
Can someone please help.
Regards
How about this..
Declare #Startdate as Varchar(8)
Set #StartDate = CONVERT(VARCHAR(8), GETDATE()-1, 112)
SELECT #StartDate
RESULT: 20140519
t-sql's convert method lets you format dates in various ways.
112 looks like the style code you want.