SQL Server: import date in yyy-mm-dd from csv in dd-mmm-yy [closed] - sql

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

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];

DynamicSQL query for date calculation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
In Dynamic SQL I need to calculate the difference in Date and a Number
Error while running the below query,
Declare #sqlstring VARCHAR(MAX)
Declare #GCValue INT
Set #GCValue=10
set #sqlstring = 'SELECT Id, getdate() - (FirstDate + ' + #GCValue +' ) from
Table where Id=1234'
EXECUTE(#Sqlstring)
E.g. let say GetDate() = 26 July 2019 and FirsDate = 1 July 2019 and #GCValue=10
Query Should return Difference of Days between above i.e. 26 July 2019 - (1 July 2019 + added 10 days so it became 11July2019) = 15 Days.
Whatever your reasons, here is a solution (not an elegant one :-) ) assuming SQL Server:
DECLARE #sqlstring VARCHAR(MAX);
DECLARE #GCValue varchar(10);
SET #GCValue = '10';
SET #sqlstring = 'SELECT Id, DATEDIFF(dd, getdate(), FirstDate + ' + #GCValue + ' ) from Table where Id=1234';
EXECUTE(#sqlstring);
You have to change #GCValue's data type to varchar (or explicictly convert it from int, as in cast(#GCValue as varchar(10)) to use it in string concatenation operations. And you can use DateDiff to compute the difference in days (dd parameter).

get last previous month records [duplicate]

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

convert varchar to datetime in SQL Server [duplicate]

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)

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;