SQL Server: Convert a varchar to a datetime - sql-server

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)

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.

Conversion failed when converting date and/or time from character string - when querying the database

The following code:
DECLARE #dateAsString AS nvarchar
SET #dateAsString = '2020-04-28T12:51:33.587Z'
DECLARE #dateObject as DATETIME
SET #dateObject = CAST(#dateAsString as DATETIME)
SELECT DATEDIFF(DAY, #dateObject, CURRENT_TIMESTAMP)
I get this error:
Msg 241, Level 16, State 1, Line 6
Conversion failed when converting date and/or time from character string
How can I ensure that the date stored in the database does not give me this error?
Convert and Cast didn't do the trick... please help!
The reason for the error is that you need to define the size of the #dateAsString nvarchar variable. When the size is not specified the default length is 1 and the actual value of the #dateAsString variable is 2.
Also, as an option, you may use CONVERT() with an appropriate date and time style:
DECLARE #dateAsString AS nvarchar(24)
SET #dateAsString = N'2020-04-28T12:51:33.587Z'
DECLARE #dateObject as DATETIME
--SET #dateObject = CAST(#dateAsString as DATETIME)
SET #dateObject = CONVERT(datetime, #dateAsString, 127)
SELECT DATEDIFF(DAY, #dateObject, CURRENT_TIMESTAMP)
Your code is ok, only your nvarchar was to small to hold you data
DECLARE #dateAsString AS nvarchar(28)
SET #dateAsString = '2020-04-28T12:51:33.587Z'
DECLARE #dateObject as DATETIME
SET #dateObject = CAST(#dateAsString as DATETIME )
SELECT DATEDIFF(DAY, #dateObject, CURRENT_TIMESTAMP)
GO
| (No column name) |
| ---------------: |
| 174 |
db<>fiddle here

Convert NVARCHAR containg Timezone info to DATETIME in 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

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)

SQL declare datetime - 1001-01-01

I am trying to declare a datetime variable with the value 1001-01-01 00:00:00.000
I have tried the following approaches with no luck
declare #d1 datetime = '1001-01-01';
declare #d2 datetime = 10010101;
declare #d3 datetime = '1001-01-01 00:00:00';
declare #d4 datetime = cast ('1001-01-01' as datetime)
I get the following errors
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type datetime.
Msg 242, Level 16, State 3, Line 3
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Msg 242, Level 16, State 3, Line 4
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Is it possible to declare a datetime variable which can hold the value 1001-01-01 00:00:00.000?
The minimum valid date for a DateTime data type is January 1, 1753.
Try DATETIME2, using:
DECLARE #d4 DATETIME2 = '1001-01-01'
The minimum valid date for DATETIME2 is 0001-01-01
You can't do that. The earliest date that a datetime value can have is 1753-01-01.
Recent versions of Sql Server have the data type datetime2 that has a larger range:
declare #d1 datetime2 = '1001-01-01';
The minimum SQL datetime is January 1,1753, so your date is indeed out-of-range.
SQLServer datetimes must be 1/1/1753 < X < 12/31/9999
http://msdn.microsoft.com/en-us/library/ms187819.aspx
you will not be able to set a DateTime to that year, so you may want to store it as a string and leave it to the upper layer langague to deal with.

Resources