Convert NVARCHAR containg Timezone info to DATETIME in SQL Server - sql-server

I have the following NVARCHAR field is SQL Server:
Sun Mar 26 23:47:06 GMT+03:00 2017
I wish to convert it to DATETIME in tSQL. How can I do that??? Using the CONVERT() and CAST() functions returns the following error
Conversion failed when converting date and/or time from character string.
I have tried converting to DATETIME, DATE, DATETIME2, DATETIMEOFFSET and none worked.

try this
Declare #dt varchar(50)
set #dt = 'Sun Mar 26 23:47:06 GMT+03:00 2017'
select CAST(left(stuff(stuff(#dt, 1, 4, ''), 8, 0, right(#dt, 4) + ' '), 20) as DATETIME)
Result
2017-03-26 23:47:06.000

Related

How change this date format "2020-04-30T18:11:39+00:00" to IST format in SQL SERVER

Hi I am stuck in converting this date format "2020-04-30T18:11:39+00:00" into IST datetime format.
declare #fiveandhalf datetime = '05:30'
print(#fiveandhalf)
declare #datetime datetime = (SELECT CONVERT(datetime, '2020-04-30 18:11:39'))
print(#datetime-#fiveandhalf)
I am getting this output
Apr 30 2020 12:41PM
but I need output like "2020-04-30 23:41: "
Assuming you are using the appropriate data type for a value with a timezone component, a datetimeoffset, just use AT TIME ZONE:
DECLARE #YourDate datetimeoffset(0) = '2020-04-30T18:11:39+00:00';
SELECT #YourDate AT TIME ZONE 'India Standard Time';
Which returns the datetimeoffset value 2020-04-30 23:41:39 +05:30.

convert nvarchar to smalldatetime in sql server

I have one table in which nvarchar data are there.
Table
18-FEB-11 10.29.52.000000000 AM
20-FEB-11 04.10.40.000000000 PM
23-SEP-10 10.34.57.714000000 AM
08-OCT-10 09.41.16.921000000 PM
I want to convert this field to small date time in sql server 2008 R2
How can i convert it.
Expected OUTPUT:
2011-02-18 10:29:52
2011-02-20 16:10:40
2010-09-23 10:34:57
2010-10-08 21:41:16
Try this please
declare #s_date as nvarchar(100)= '18-FEB-11 10.29.52.000000000 AM'
SELECT CONVERT(nvarchar(100),CAST(REPLACE(REPLACE(#s_date, SUBSTRING(#s_date, 19, 10),''),'.',':') as datetime), 120)
result : 2011-02-18 10:29:52
this should work too:
DECLARE #datestring NVARCHAR(50) = '08-OCT-10 09.41.16.921000000 PM'
SELECT CAST(REPLACE(LEFT(#datestring,18),'.',':') + RIGHT(#datestring,2) AS SMALLDATETIME)

Conversion failed when converting date and/or time from character string in SQL SERVER 2008

I have below SQL.
UPDATE student_queues
SET Deleted=0,
last_accessed_by='raja',
last_accessed_on=CONVERT(VARCHAR(24),'23-07-2014 09:37:00',113)
WHERE std_id IN ('2144-384-11564')
AND reject_details='REJECT'
when I ran the above SQL the below exception has been throwed.
Conversion failed when converting date and/or time from character string.
If you're trying to insert in to last_accessed_on, which is a DateTime2, then your issue is with the fact that you are converting it to a varchar in a format that SQL doesn't understand.
If you modify your code to this, it should work, note the format of your date has been changed to: YYYY-MM-DD hh:mm:ss:
UPDATE student_queues
SET Deleted=0,
last_accessed_by='raja',
last_accessed_on=CONVERT(datetime2,'2014-07-23 09:37:00')
WHERE std_id IN ('2144-384-11564') AND reject_details='REJECT'
Or if you want to use CAST, replace with:
CAST('2014-07-23 09:37:00.000' AS datetime2)
This is using the SQL ISO Date Format.
Seems like last_accessed_on, is a date time, and you are converting '23-07-2014 09:37:00' to a varchar. This would not work, and give you conversion errors. Try
last_accessed_on= convert(datetime,'23-07-2014 09:37:00', 103)
I think you can avoid the cast though, and update with '23-07-2014 09:37:00'. It should work given that the format is correct.
Your query is not going to work because in last_accessed_on (which is DateTime2 type), you are trying to pass a Varchar value.
You query would be
UPDATE student_queues SET Deleted=0 , last_accessed_by='raja', last_accessed_on=convert(datetime,'23-07-2014 09:37:00', 103)
WHERE std_id IN ('2144-384-11564') AND reject_details='REJECT'
DECLARE #FromDate DATETIME
SET #FromDate = 'Jan 10 2016 12:00AM'
DECLARE #ToDate DATETIME
SET #ToDate = 'Jan 10 2017 12:00AM'
DECLARE #Dynamic_Qry nvarchar(Max) =''
SET #Dynamic_Qry='SELECT
(CONVERT(DATETIME,(SELECT
CASE WHEN ( ''IssueDate'' =''IssueDate'') THEN
EMP_DOCUMENT.ISSUE_DATE
WHEN (''IssueDate'' =''ExpiryDate'' ) THEN
EMP_DOCUMENT.EXPIRY_DATE ELSE EMP_DOCUMENT.APPROVED_ON END
CHEKDATE ), 101)
)FROM CR.EMP_DOCUMENT as EMP_DOCUMENT WHERE 1=1
AND (
CONVERT(DATETIME,(SELECT
CASE WHEN ( ''IssueDate'' =''IssueDate'') THEN
EMP_DOCUMENT.ISSUE_DATE
WHEN (''IssueDate'' =''ExpiryDate'' ) THEN EMP_DOCUMENT.EXPIRY_DATE
ELSE EMP_DOCUMENT.APPROVED_ON END
CHEKDATE ), 101)
) BETWEEN '''+ CONVERT(CHAR(10), #FromDate, 126) +''' AND '''+CONVERT(CHAR(10), #ToDate , 126
)
+'''
'
print #Dynamic_Qry
EXEC(#Dynamic_Qry)

SQL Server: Convert a varchar to a datetime

I would like a date in a varchar(255) data type as below to be converted to a datetime or date format
05 jun 2007
When I use a cast with either the datetime or date:
UPDATE [dbo].[SSIS_TempDump_Episode_Numbers]
SET DischargeDate = CAST([Discharge date] as Date)
I get this error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
or alter column gives me the same result. is it because of the date format?
Select CONVERT(datetime,'05 jun 2007') -->'2007-06-05 00:00:00.000'
DECLARE #foo VARCHAR(255)
SET #foo = '05 jun 2007'
PRINT CONVERT(DATETIME,#foo)

Convert Datetime format in SQL Server 2008

I need to convert the Long value to a date time format.
Eg., Long value - 20080506015600658
datetime format - Tue May 06 01:56:00 IST 2008
Here's the ugly way to do it via string manipulation:
declare #start bigint
set #start = 20080506015600658
select CONVERT(datetime,
STUFF(STUFF(STUFF(STUFF(STUFF(STUFF(
t,15,0,'.'),
13,0,':'),
11,0,':'),
9,0,'T'),
7,0,'-'),
5,0,'-'))
from (select CONVERT(varchar(20),#start) as t) n
Which basically forces it to conform to the pattern YYYY-MM-DD'T'hh:mm:ss.mil before doing the conversion.
And here's the ugly as sin way to do it with maths:
declare #start bigint
set #start = 20080506015600658
select
DATEADD(year, (#start/10000000000000) - 1, --Because we already have 1 on starting date
DATEADD(month, (#start/100000000000)%100 - 1, --Because we already have 1 on starting date
DATEADD(day, (#start/1000000000)%100 - 1, --Because we already have 1 on starting date
DATEADD(hour, (#start/10000000)%100,
DATEADD(minute,(#start/100000)%100,
DATEADD(second,(#start/1000)%100,
DATEADD(millisecond,#start%1000,CONVERT(datetime2,'0001-01-01'))))))))

Resources