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)
Related
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];
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)
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
This question already has answers here:
Get the records of last month in SQL server
(23 answers)
Closed 8 years ago.
sql 2005 server
get previous month records
Date product
24-05-2014 ball
25-05-2014 bat
01-06-2014 hat
i need
Date Product
24-05-2014 ball
25-05-2014 bat
declare #ex datetime
set #ex '06-01-2014'
select * from tabl where DATENAME(m,DATEADD(m,0,Date)) =DATENAME(m, DATEADD(m,0, #ex))- it works
select * from tabl where DATENAME(m,DATEADD(m,0,Date)) =DATENAME(m, DATEADD(m,-1,#ex))-not works
My sample code (tested on 2008). I don't know are YEAR and MOTH function in 2005 if not you need to use some string function to extract date / month part from datetime converted to string
declare #ex datetime = '2014-01-01'
declare #prev_year int
declare #prev_month int
set #prev_year = year(dateadd(month, -1, #ex))
set #prev_month = month(dateadd(month, -1, #ex))
select * from tabl
where year(Date) = #prev_year and month(Date) = #prev_month
You're using dd-MM-yyyy format for your dates, which is a varchar in SQL Server.
Therefor, you must use CONVERT :
declare #ex varchar(10)
set #ex = '06-01-2014'
SELECT DATENAME(m,DATEADD(m,0,GETDATE())),
DATENAME(m, DATEADD(m,-1,CONVERT(date, #ex, 103)));
This yields results:
June | May
I think you can figure out your solution from here.
Note: If you use declare #ex datetime , your results will yield June | December
This question already has answers here:
Convert varchar into datetime in SQL Server
(13 answers)
Closed 10 years ago.
I have a column of dates with no delimiters. The column is nvarchar. The strings are consistent in length and format of MMDDYYYY. How can I convert these values to datetime?
edit - this question is in reference to sql server.
Assuming SQL Server:
DECLARE #A NVARCHAR(10)
SET #A = '11302012'
SELECT CONVERT(DATETIME,LEFT(#A,2) + '/' +
SUBSTRING(#A,3,2) + '/' + RIGHT(#A,4),101)
BEGIN
DECLARE #d DATETIME
DECLARE #s NVARCHAR(32)
SET #s = N'12012013'
SET #d = SUBSTRING(#s, 5,4) + SUBSTRING(#s, 1,2) + SUBSTRING(#s, 3,2)
SELECT #d
END
You just have to mangle the string into a format SQL server can parse correctly into a date. In the above it's the YYYYMMDD format.
EDIT Removed "-"'s because French language settings break them.
First change the format to the one that always works no matter what server settings (YYYYMMDD) using two simple string functions, then convert to datetime:
declare #datestring varchar(8) = '11302012';
select CONVERT(datetime, RIGHT(#datestring, 4) + LEFT(#datestring, 4)) ConvertedDatetime;