convert varchar to datetime in SQL Server [duplicate] - sql

This question already has answers here:
Convert varchar into datetime in SQL Server
(13 answers)
Closed 9 years ago.
Have date in varchar (dayname,month date,year) like (wednesday, january 16, 1013) and i have to convert it in datetime format. i have tried options like convert function and some other. but didn't get it.

The conversion works without the name of the day in the string. Remove it then cast to datetime:
declare #dateString varchar(50)
set #dateString = 'wednesday, january 16, 2013'
declare #index int
set #index = charindex( ',', #dateString )
set #dateString = substring( #dateString, #index + 1, len(#dateString) - #index)
select cast(#dateString as datetime)

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: import date in yyy-mm-dd from csv in dd-mmm-yy [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm receiving a csv with dates in a dd-mmm-yy format
05-SEP-19
I want to convert them into yyyy-mm-dd
2019-09-05
When I import the file with Microsoft Server Management Studio.
You can try this:
This converts string to date format, after this you can format date as you want...
DECLARE #String nvarchar(30);
SET #String = '05-SEP-19'
SELECT #String, CONVERT(date,substring(#String, 1, 2) + ' ' +
substring(#String, 4, 3) + ' ' +
substring(#String, 8, 4), 6)
Results:
05-SEP-19 2019-09-05
DECLARE #dt DATETIME = '05-SEP-19'
PRINT #dt
DECLARE #dts VARCHAR(20) = FORMAT(#dt, 'yyy-MM-dd')
PRINT #dts
gives
Sep 5 2019 12:00AM
2019-09-05
Completion time: 2019-11-22T11:16:43.8140327+02:00
Note that #dt has type DATETIME. What this piece of script does is (i) create DATETIME #dt initialised from a string containing an "acceptably recognisable" representation of a date (ii) display #dt default format (iii) create string initialised with custom-format of this value (iv) display that.
You have to distinguish between the (date) type and its formatted-for-display (string) representation.
DECLARE #Date VARCHAR(50)
SET #Date='05-SEP-19'
SELECT CONVERT(CHAR(10), CONVERT(DATETIME, #Date), 120) AS Date
Output
Date
-----
2019-09-05

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)

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value - Leap Year

I get this error in stored procedure code and I discovered one of the records is from a leap year date 2008-02-29. How do I get around this with the code I have. Here is the code:
set #ContractDate = cast(cast(#Year as nvarchar(4))
+ '/' + cast(datepart(mm,#ContractDate) as nvarchar(2))
+ '/' + cast(datepart(dd,#ContractDate) as nvarchar(2)) as datetime)
First off:
What values do you have for #Year and #ContractDate?
And what format/type is #ContractDate?
I ask because this works for me on us_english SQL Server 2012
SELECT cast('2008/02/29' as datetime)
However, if I change SET DATEFORMAT then I can break the above code
-- breaks
SET DATEFORMAT DMY;
SELECT cast('2008/02/29' as datetime)
Using ISO date format is OK no matter what I use for SET DATEFORMAT
SET DATEFORMAT DMY;
SELECT cast('20080229' as datetime);
SET DATEFORMAT MDY;
SELECT cast('20080229' as datetime);
SET DATEFORMAT YDM;
SELECT cast('20080229' as datetime);
Conclusion: you are using non ISO date formats and relying on implicit conversions
It is better to get the string in ISO format (yyyymmdd) before converting to datetime/date and it will work in any server regardless of culture settings
If #ContractDate is a datetime/date type and #year is like 2012/'2012' then following should work. Working Example
Set #ContractDate = convert(datetime,
convert(varchar(4),#year) + right(convert(varchar(8),#ContractDate,112),4))
ALTER PROCEDURE [dbo].[Sden_report1]
#fromdt nvarchar(13),
#todt nvarchar(13)
AS
BEGIN
select AD_PID,AD_Appointmentdate,AD_Patientname,AD_Patientphno,AD_Visitpurpose
from Appointment_Details1
where
--PL_CID = #CID and PL_Itemcode = #JCode AND
(#fromdt = '' OR (DATEDIFF(DAY,AD_Appointmentdate, #fromdt) <= 0))
AND (#todt = '' OR (DATEDIFF(DAY,AD_Appointmentdate, #todt) >= 0))
end
GO
When i Execute the procedure
i will get below error:
EXEC Sden_report1 '20-11-2012','20-05-2013'
Error
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
Anyone Help...`enter code here`

convert string to datetime in sql server [duplicate]

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;