Date conversion failed from string - sql

I have a date saved in the format DD/MM/YYYY from a flat file. "20/04/2013"
When I try to insert it into my SQL Server database it changes the value to MM/DD/YYYY.
So of course there in no month 20 and my code fail. How can I work around this?
I have tried stuff like this and I had no luck.
SELECT CONVERT(datetime, CONVERT(varchar, '20/04/2013', 101))

just do this directly,
SELECT CONVERT(datetime, '20/04/2013', 103)

On SQL Server you need to use the SET DATEFORMAT option. (See http://msdn.microsoft.com/en-us/library/ms189491.aspx).
In your case, you would need to issue the following command before executing the above SELECT-statement:
SET DATEFORMAT dmy
Alternatively, it is possible to globally change the DATEFORMAT setting for the server.

Definition and Usage
The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
select convert(datetime, '20/04/2013', 103)
enter link description here

Related

Get DDMMYYYY format in SQL Server without periods or hyphens

I have a little task to do and I need to get ddmmyyyy format for my DateTime values in SQL Server.
I tried to do it with this code
SELECT convert(varchar, getdate(), 104)
and it works fine, but it returns dd.mm.yyyy or 01.11.2021. I need it to be 01112021 but I can't achieve this yet.
I am using this link for getting datetime format codes but I can't find any code that really helps me achieve my goal.
There's no exact match for the required format you need - you can either use
SELECT REPLACE(CONVERT(varchar(20), SYSDATETIME(), 104), '.', '')
(use what you had and just strip out the . after first step of formatting)
or you can use FORMAT which is available in SQL Server 2012 and newer:
SELECT FORMAT(SYSDATETIME(), 'ddMMyyyy')
There is no defined format that fulfills your request. However, you can use the 'Replace' function to strip your value from date separators.
select replace(convert(varchar, getdate(),104),'.','')
OR use the newer FORMAT function (SQL server 2012+)
SELECT FORMAT(getdate(), 'ddMMyyyy')
You can use replace function to complete it.
select replace(convert(varchar, getdate(), 104),'.','')

How to get datepart from datetime in a Column

I want to convert the column (EXECUTION_LOCAL_DATE_TIME) which has datetime format as (YYYY-MM-DD hh:mm:ss.nnnnnnn) to format (YYYY-MM-DD). Ho do i get this. I am working on SQL server management studio
If your intention is to just get the DATE part of a DATETIME then you can just convert the format to DATE (note that this will return a 'Date' datatype, not specifically formatted to a string 'YYYY-MM-DD'.)
eg:
DECLARE #Dt DATETIME = '2019-01-25T12:00:00'
SELECT CONVERT(DATE, #Dt)
Will return '2019-01-25'
You want the CAST() function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
Ideally you should return the datetime format from your query and let your presentation layer handle formatting.
However if you are using SQL server 2012 or higher then you can use the Format() function. See the below answer:
Convert Date format into DD/MMM/YYYY format in SQL Server
If you're using SQL Server 2012 or higher you can use the FORMAT() function.
In your case you'd need
SELECT FORMAT(EXECUTION_LOCAL_DATE_TIME, 'yyyy-MM-dd') FROM TABLE_NAME
You can find additional info here
https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/
https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-2017
Here Is the Code Worked For me
SELECT convert(varchar, EXECUTION_LOCAL_DATE_TIME, 111) from Tablename
The Execution_Local_Date_Time will be Converted to yyyy/mm/dd format.

Format a string as American

Please see the SQL below:
select cast('13/01/2015' as datetime)
The error is: 'The conversion of a varchar data type to a datetime data type resulted in an out-of-range value'. I know I can do this will resolve it:
select cast('01/13/2015' as datetime)
Is there a way of formatting a string of: '13/01/2015' as '01/13/2015'
Don't use cast, use Convert. this way you can choose the date format.
select convert(datetime, '13/01/2015', 103)
select convert(datetime, '01/13/2015', 101)
You can set the dateformat and do something like
set dateformat dmy
select cast('13/01/2015' as datetime)
I would use CONVERT, but if you choose to use CAST then you might want to try this...
SET LANGUAGE british
SELECT CAST('13/01/2015' AS Datetime)
SET LANGUAGE us_english
SELECT CAST('01/13/2015' AS Datetime)
This still wont fix your formatting issue though. That's why I would use ..
CONVERT(DATETIME,'01/13/2015',101)
You could try:
SELECT CONVERT(CHAR(10),CONVERT(DATETIME,'13/01/2015',103),101))
This will convert the string to a date time in the format of DD/MM/YYYY, and then it will convert it to the MM/DD/YYYY format you are looking for.
Edit Note: I noticed you wanted the resultant in a string.

CONVERT(DATETIME, DATETIME, 131)

I can't seem to figure this one out and tried searching stackoverflow.
I have two lines where I want to convert the date. I didn't create these datatypes or tables and don't want to change the date types in case it messes something up.
I have one table where one of the columns is of type (datetime2(7), not null).
I have another table where the column is of type (datetime, null).
when I do a query they return a time (respectively) that looks like
2015-01-25 10:11:50.9050000
2015-01-19 10:24:42.323
The above is before I do any conversion or casting.
I'd like to convert the format of those to
dd/mm/yy hh:mi:ss:mmmAM
So I used the code below (respectively)
SELECT TagName, CONVERT(DateTime, EventStamp, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
and
SELECT TagName, CONVERT(DateTime, DateTime, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
But it doesn't seem to work. It drops the ss (seconds) part and the month/day/year portion does't come out according to style 131. Anyone know why? It looks like below
25/01/2015 10:11 AM
19/01/2015 10:24 AM
I tried a bunch of way and it doesn't seem to work. I'm running MS SQL server 2012 and just doing the query in SQL Server Manager Studio.
If you want to convert to an output format, you want to convert from a datetime to a string.
Try this:
SELECT TagName, CONVERT(varchar(255), EventStamp, 131) AS ConvertedTime
FROM ALMDB.dbo.Hist
Your code tries to convert from datetime to datetime, which isn't what CONVERT is meant to do. If you wish to control the string representation of a date in your output, convert to nvarchar instead: CONVERT(NVARCHAR(25), DateField, 131). If you'd like to change the default string representation of a datetime field on a per-session basis, use the SET DATEFORMAT command. https://msdn.microsoft.com/en-us/library/ms189491.aspx

How to convert varchar to date

I am working in SQL Server 2008 R2
I need to convert a varchar(50) field in a view to a date format.
I have a view that uses the following to create the field delivered_date:
convert(varchar(50),[delvd_dt],110) as [delivered_date]
The formatted field looks like : 2012-03-11 16:24:42.0000000
I need the results to be a date so that I can check for dates within a range. I would prefer to do it within the view so that I can use SSRS to create the range for the report.
Have you tried cast()? The following returns the right date for me:
select CAST('2012-03-11 16:24:42.0000000' as DATE)
Perhaps you can simply truncate the millis and use CONVERT:
SELECT Convert(datetime, LEFT('2012-03-11 16:24:42.0000000', 19), 102) AS [Date]
Demo
This works even in MS SQL-Server 2005.
CAST and CONVERT (Transact-SQL)
Try this link website shows several formatting options.
Example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 105)