T SQL SSMS Convert Varchar DD\MM\YYYY to Date format - sql-server-2008

I have a VARCHAR(10) data type column with date in DD\MM\YYYY format.
I tried using convert(datetime,LOCALDATE_T,103) and much more. However throws me an error of
Conversion failed when converting date and/or time from character
string.
I am unsure if my query could read DD\MM\YYYY. I have usually converted column from DD/MM/YYYY format i.e. / but not this \
Request your assistance.

The working solution is:
CONVERT(datetime, REPLACE(LOCALDATE_T, '\', '/'), 103)
or
CAST(REPLACE(LOCALDATE_T, '\', '/') as date)

use REPLACE instead of CONVERT Function
REPLACE(LOCALDATE_T,'\','/')

Related

Keep getting informatica function error invalid string for converting to Date

I am trying to convert a nvarchar into a date, but I keep getting this error below
TT_11132 Transformation [Expression] had an error evaluating output column [Run_Date1]. Error message is [<> [TO_DATE]: invalid string for converting to Date ... t:TO_DATE(u:'20190304',u:'MM/DD/YYYY HH24:MI:SS')].
but my function doesn't include a 'MM/DD/YYYY HH24:MI:SS'. Here is my function for reference.
TO_DATE(TO_CHAR(to_date(RUN_DATE),'mmddyyyy'),'mm/dd/yyyy')
Please Help
Thank you
Samik is right, just use the TO_DATE(RUN_DATE, 'YYYYMMDD')
What I'd like to add is some explanation why your function is wrong. So stripping it from innermost:
to_date(RUN_DATE)
Converts the RUN_DATE string to a DATE trying to guess the format as it has not been given. Let's call the result of it as NewDate and analyze further:
TO_CHAR(NewDate,'mmddyyyy')
Now this will convert NewDate to a string in the mmddyyyy format. Let's call this one as NewString_MMDDYYYY and see where it gets us. This leaves the final function as:
TO_DATE(NewString_MMDDYYYY,'mm/dd/yyyy')
See the error yet? You tell Informatica to convert NewString_MMDDYYYY to a date. And you tell the function that it is written in the mm/dd/yyyy format - which obviously is not being used, as the TO_CHAR function was told to store it in mmddyyyy.
So, if your input RUN_DATE is in YYYYMMDD format, then all you need to do is TO_DATE(RUN_DATE, 'YYYYMMDD'). If another formati is used by RUN_DATE, just replace the second argument accordingly.
Replace everything with just TO_DATE(RUN_DATE, 'YYYYMMDD')

How to convert '10-Mar-1993' to date while inserting records in mysql?

I have written the following query:
INSERT INTO programs
(programId,programName,startDate,annualGoal)
VALUES
(135,'Community Evergreening',STR_TO_DATE('10-Mar-2013','dd-mmm-yyyy'),25000);
But, it says "Error Code: 1411. Incorrect datetime value: '10-Mar-2013' for function str_to_date".
I am not able to understand what I am doing wrong here.
You'll need to represent the date format like this: %d-%b-$Y
STR_TO_DATE('10-Mar-2013', '%d-%b-%Y')
The problem is that you are using the format dd-mmm-yyyy. However, MySQL's date formatting does not use that format.
If you look at the documentation for str_to_date, it says that it is the inverse of date_format, and that you can look at the documentation for date_format for a table of the characters that you can use to represent a date format.
For the specifiers that can be used in format, see the DATE_FORMAT() function description.
Try STR_TO_DATE('10-Mar-2013', '%d %b %Y')
Got from here

SQL statement error in VB.Net

This is the SQL statement which I taken from debug mode in VB.Net.
SELECT dt_Date as 'Date',
s_Time as 'Time',
s_For as 'For',
s_Categ as 'Category',
s_Count as 'Count',
s_Remarks as 'Remarks'
FROM Entry
WHERE (s_ENo = '22' and dt_date BETWEEN '06-05-16' And '27-05-16')
I am not sure what's wrong with the above statement since everything seems to be fine to my eyes.
Description of the Error Message
Additional information: Conversion failed when converting date and/or
time from character string.
Any suggestions how to solve this riddle?
Instead if string literals, use strongly-typed parameters. In addition to avoiding data type conversion issues on the SQL Server side, parameters:
Avoid the need to format date/time string literals in a particular way
Improve performance by providing execution plan resue
Prevent SQL injection (barring dynamic SQL on the server side)
If you are using MS-SQL server than use following instructions.
The basic solution is that you have to provide date into either mm/DD/YYYY format or in YYYY-MM-DD date format into ms-sql query.
So, before passing date to query convert your date into either mm/DD/YYYY format or in YYYY-MM-DD format.
You have to format your date string to YYYYMMDD or if you know the server date format then format same as server

Converting date format using str_to_date

In one of my Mysql database table the dates are stored in the format 31-Jan-05.
I'm trying to convert this format to 2005-01-31 before inserting them in other tables.
I've tried in this way str_to_date(exam_date, '%d%M%Y'), but i encounter the following error
Incorrect datetime value: '31-Jan-05' for function str_to_time
Can't i change the date format from 31-Jan-05 to 2005-01-31 using str_to_date?
Thanks in advance.
Yes. But you have two problems.
The second parameter is the current date format. (i.e. of the string)
You need to have the proper format (i.e. %b instead of %M).
Read the docs the for str_to_date().
str_to_date(exam_date, '%d-%b-%y')
Note: If you don't have a zero padded day, then you need to use %e instead of %d.

Sql Server DATETIME format incorrrect

I have a field type varchar in sql server. It contains data like "010109" etc.
When I try to convert this to DATETIME it returns a value "9 Jan 2001" when the actual value should be "1 Jan 2009".
Does anybody have a fix for this?
Thanks for your help.
I thought there would be some conversion format so you could put:
select(convert(datetime,'010109',<some magic number>))
and get the result, but I can't seem to find one that gives the right date :(
This works, but is pretty nasty:
declare #dt varchar(6)
select #dt = '010109'
select convert(datetime,RIGHT(#dt,2) + SUBSTRING(#dt,3,2) + LEFT(#dt,2))
Yeesh
When you type date in the format of 'xxxxxx' it seems that SQLServer assumess it is an ISO format yymmdd and as such it is not affected by the SET DATEFORMAT
I was aware of 2 such formats - so called safe formats
ISO: yyyymmdd
ISO8601:yyyy-mm-ddThh:mi:ss.mmm
but it seems that yymmdd is also ISO - check BOL Date and Time Styles - format 12
That would explain why the solution posted by Scorpio did not work
You can use the solution provided by butterchicken with the format specification (12) to be on a safe side:
declare #dt varchar(6)
select #dt = '010109'
select convert(datetime,RIGHT(#dt,2) + SUBSTRING(#dt,3,2) + LEFT(#dt,2),12)
If possible I would be ideal if you could change the column to datetime to avoids similar surprises in the future
SQL Server is expecting the date to be in the format YMD
So if your string was like this '090101' you would get the proper date.
select(convert(datetime,'090101',104))
So you will have to substring out the parts and run the convert.