I am writing a SQL query using SQL Server Management Studio and there are some NVARCHAR type values in ISO date format (example: 20130302T164800). I need to convert them to a DATETIME
I tried the Convert() function but it causes an exception:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value
Is there a way I can do this?
The problem is that your string is not an accepted SQL Server datetime format. SQL Server recognises the ISO8601 format, which is:
yyyy-mm-ddThh:mi:ss.mmm
Which would be 2013-03-02T16:48:00 for your date above.
See Date And Time Styles section.
So the following statement will fail:
declare #date nvarchar(max) = '20130302T164800'
select convertedDate = cast(#date as datetime)
If you convert the string to the ISO8601 format, the statement will work:
declare #date nvarchar(max) = '2013-03-02T16:48:00'
select convertedDate = cast(#date as datetime)
SQL Fiddle with demo.
You can update your format to one SQL Server recognises and cast the string to a datetime in one statement:
declare #date nvarchar(max) = '20130302T164800'
select cast(left(#date, 4)
+ '-' + substring(#date,5,2)
+ '-' + substring(#date,7,5)
+ ':' + substring(#date,12,2)
+ ':' + substring(#date,14,2) as datetime)
SQL Fiddle with demo.
This is just an example, you could convert it to any format recognised by SQL Server, but this converts it to ISO8601. Basically, convert it to a different format to allow the conversion to work.
Related
I have the below query
DEClare #Plan_ID nvarchar(100)
set #Plan_ID=REPLACE('2151,1886',',',''',''')
and
SELECT distinct Plan_Dict_Id from REF_Plan_Dictionary WHERE
CAST(Plan_Dict_Id as int) in (#Plan_ID),
Pls help , Plan_Dict_Id datatype is INT, I want to pass the values to where , but getting error "Conversion failed when converting the varchar value '2151','1886' to data type int."
If this is one of the later versions of SQL Server then you could do something like this...
DECLARE #Plan_ID nvarchar(100)
SET #Plan_ID= '2151,1886'
SELECT DISTINCT Plan_Dict_Id
FROM REF_Plan_Dictionary d
JOIN string_split(#Plan_ID, ',') s ON d.Plan_Dict_Id = s.[value]
If you are using an older version you could put the whole statement in a string and then execute it, however you have a problem in your first line as you are not adding quotes around the string... I've accounted for that below.
DEClare #Plan_ID nvarchar(100) set #Plan_ID=REPLACE('2151,1886',',',''',''')
DECLARE #sql nvarchar(max)
SET #SQL = 'select distinct Plan_Dict_Id from REF_Plan_Dictionary where CAST(Plan_Dict_Id as int) in (''' + #Plan_ID + ''')'
EXEC (#sql)
How to write below code in in SQL server 2008 r2
DECLARE #date date
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
select #date = GETDATE()
select format(cast(min(#date + #datetime) as datetime), 'hh:mm:ss tt')
this piece of code doesn't work in SQL Server 2008 r
Your code is confusing. You are trying to add two dates which doesn't make sense. Anyway, here's how to convert a datetime to output the hh:mm:ss format.
DECLARE #datetime datetime
SELECT #datetime = GETDATE()
select
DT = convert(char(10),#datetime,120)
,TimeOnly = left(cast(#datetime as time),8)
Looking to convert datetime format
20150416 12:29:20:845
to
20150416 12:29:20:84.
I have tried a number of date time conversion but no luck.
DECLARE #Date varchar(22)
SET #Date = CONVERT(Varchar(23), GETDATE(),121) --replace getdate with your value
SELECT #DATE --2015-04-17 10:56:55.29 format you need
SELECT GETDATE() --2015-04-17 10:56:55.297 actual value
SELECT CAST(#DATE as datetime) --2015-04-17 10:56:55.290 if u convert it back
Assuming that you already converted your date as varchar, you can use this query:
DECLARE #V VARCHAR(30) = '20150416 12:29:20:845'
SELECT LEFT(#V,20),STUFF(#V,21,1,'.')
I have a strange bug that I cannot resolve (with a one line function).
This code works:
DECLARE #TestDate datetime = '2013-05-01 23:15:11'
select IsNull(convert(varchar(max), #TestDate, 120), 'null') as 'test1'
Displays: 2013-05-01 23:15:11
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate date)
RETURNS VARCHAR(40)
BEGIN
return ISNULL(convert(varchar(40),#InputDate, 120),'null');
END
select dbo.DateOrNullToChar('2013-05-01 23:15:11') as 'result'
Returns: 2013-05-01 (no time)
I have also tried varchar(max).
The purpose of the function is for something like this:
Set #ErrorMessage = ' #ArrivalDate=' + dbo.DateOrNullToChar(#ArrivalDate) +
' #DepartureDate=' + dbo.DateOrNullToChar(#DepartureDate);
If any one value is null, the whole value becomes null. So I want to see the string 'null' when a date has a null value.
#InputDate should be datetime or datetime2 if you want time to be shown
The clues are in the code...
#TestDate datetime
#InputDate date
You need to make the parameter type to be datetime instead of date:
CREATE FUNCTION [dbo].[DateOrNullToChar] (#InputDate datetime)
It's silently converting the string to your date parameter type and thus dropping the time portion.
I'm having a hard time in assigning values that came from my database,
Value to be retrieved :
AppointmentTime
Value = 11:00:00
Data Type = time(0)
I use VS 2010 to assign that value from SQL Server 2008 with a DateTimePicker..
DateTimePicker1
Format = HH:mm
MinDate = 1/1/1770
DateTimePicker1.Value = Appointment Time
It gives me an error of 'Value of 1/1/0001 11:00:00AM is not valid for Value. Value should be between MinDate and MaxDate..
I wonder why SQL Server 2008 gives a value of Date, in my Time(0) Column?
I try this query
declare #x time(0);
set #x = getdate();
print cast(#x as datetime);
It will be give you the time with the date 01/01/1900.
So, try to cast Appointment Time to DateTime before set the DateTimePicker1 value.