I have date in column of nchar(255) datatype in this format
mm/dd/yyyy
Now I want to change it into
dd/mm/yyyy
I don't think that CONVERT help me in this as I have tried different queries like below
SELECT CONVERT(NCHAR(20), col1, 103) from table1
SELECT CONVERT(VARCHAR(20), col1, 103) from table1
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
In my view 103 which is in UK format converts only from yyyy-mm-dd
So I tried to CAST my col1 but I get an error.
Am I missing something? Is there any easy way to do this?
Do this in two expiicit steps. First, convert the string to a date using the 101 format (which is mm/dd/yyyy). Then explicitly convert the date back to a string using 103 (which is dd/mm/yyyy):
select convert(varchar(255), convert(date, datecol, 101), 103)
Two points. First, I don't think the result needs to be nvarchar() (the string only consists of numbers and a slash). Second, always include a length when using varchar() in SQL Server.
Here is a little SQL Fiddle.
You can try this:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, col1, 101), 103)
Check this:
Select convert(varchar(15), cast('01/26/2015' as datetime), 103) as FormattedDate
Where, '01/26/2015' is your col1
Edited Answer:
Select convert(nchar(255), cast(col1 as datetime), 103) as FormattedDate From table1
Where table1 is your table.
I am answering my own question.(Just in case anyone wants to know what is the solution)
There was no such problem with the query i was using. i.e.
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
The problem was with my nchar field.
It was having a special character (in my case a space) in every entry which was giving "out-of-range" error when tried to convert or cast.
So removing Special character(a space) solved my problem.
Related
I have VARCHAR column (MyValue) in my table. It has date value in two different format.
MyValue
----------
25-10-2016
2016-10-13
I would like to show them in DATE format.
I wrote query like below:
SELECT CONVERT(date, MyValue, 105) FROM MyTable
SELECT CAST(MyValue as date) FROM MyTable
Both are giving me this error. Conversion failed when converting date and/or time from character string.
Is there anyway convert to DATE datatype format even the value stored in different formats like above?
Expecting your answers. Thanks in advance.
Does this help?
declare #varchardates table
(
vcdate varchar(20)
)
INSERT INTO #varchardates VALUES
('25-10-2016'),
('2016-10-13')
SELECT CONVERT(date,vcdate, case when SUBSTRING(vcdate, 3, 1) = '-'
THEN 105 ELSE 126 END) as mydate
FROM #varchardates
Depending on how many different formats you have in your data, you may need to extend the case statement!
See here for list of the different format numbers
You can use TRY_CONVERT and COALESCE. TRY_CONVERT returns NULL if the conversion fails, COALESCE returns the first NOT NULL value:
SELECT COALESCE(TRY_CONVERT(DATETIME, x, 105), TRY_CONVERT(DATETIME, x, 120))
FROM (VALUES('25-10-2016'), ('2016-10-13')) a(x)
I assumed the value 2016-10-13 is in format yyyy-MM-dd.
You mention in a comment you may have other formats as well. In that case it gets very tricky. If you get a value 01-12-2017 and you have no idea about the format, there is no way to tell whether this is a date in januari or in december.
I have a field with date in the format dd.mm.yyyy E.x. 29.05.2016. I want to SELECT it as DATE but I get an error when I try the following:
CAST([PublishedDate] AS DATE) AS PublishedOn
Conversion failed when converting date and/or time from character string.
Reading this I tried the following:
CONVERT(VARCHAR(10), [PublishedDate], 126) AS PublishedDate
But it doesn't change the format.
How do I SELECT to have it in YYYY-MM-DD format.
EDIT: I have 3 different date fields with all three having different formats: dd.mm.yyyy | yyyy-mm-dd | yyyy/mm/dd. I want to select all of them with the same format. Since I use this query to build reports, right now I have dates in different formats. Doing the following:
CONVERT(VARCHAR(10), [PublishedDate], 104) AS PublishedOn
CONVERT(VARCHAR(10), [ValidFromDate], 104) AS ValidFrom
Gives me the following
You should use the right format, in your case it would be:
CONVERT(date, [PublishedDate], 104) AS PublishedDate
Also, once it's in a date, datetime or other date datatype, it doesn't have a format at all.
edit: Once you have your values in a date datatype, of course you can recast to a varchar to get the visual representation of the date you need.
edit2: If you want a date datatype, you should convert to date: CONVERT(DATE, [your column], [your format]).
If you want a nvarchar datatype, you should convert to nvarchar: CONVERT(nvarchar(x), [your column], [your format]).
You have an nvarchar that you want to display in a certain format, so you should first convert to date, then back to varchar (I doubt you need unicode):
CONVERT(VARCHAR(10), CONVERT(date, [PublishedDate], 104), 126)
The 104 you have to change for columns that are currently in a different format.
The best solution by far, is to change the datatypes to date. That is a bit of work, but definitely worthwhile.
If you really want the string do like this:
CONVERT(char(10), CONVERT(date,[date], 104),126) AS PublishedDate
Convert the string to date, using the 104 format (dd.mm.yyyy), as it is your original format, then convert the date into string, using the 126 format (yyy-mm-dd)
If you have a newer version of SQL Server, you may try
SELECT COALESCE(TRY_CAST(adate AS DATE), TRY_convert(DATE, adate, 126), TRY_convert(DATE, adate, 104)), format
FROM (
VALUES ('26.04.2017', 'dkformat') ,
('01.01.2017', 'EU format'),
('12.12.12', 'unknown format')
) a(aDate, format)
But you are going down a dangerous path, when you are trying to guess what format your source is in. Been there, done that, had the t-shirt.
I still hate the 0.1% conversions that are wrong.
This solution will result in NULLs, so you can see where your conversion did not succeed.
I have a table in SQL Server with a column RGSTR_DATE that is of varchar(10) data type, and it has values like 2016-01-23, 1998-08-12, etc...
I want to select and convert those values to 20160123, 19980812, etc...
I tried running these queries:
SELECT CONVERT(DATE, CAST(RGSTR_DATE AS DATE), 112)
FROM [project].[dbo].[my_table];
SELECT CONVERT(VARCHAR(10), RGSTR_DATE, 112)
FROM [project].[dbo].[my_table];
But the results that came back were still 2016-01-23, 1998-08-12 etc...
What am I doing wrong?
Did you try
SELECT CONVERT(VARCHAR(10),cast(RGSTR_DATE as date),112)
You're converting a varchar to a date, but all you need to do is remove the hyphens.
SELECT REPLACE(RGSTR_DATE, '-', '')
FROM [project].[dbo].[my_table]
I am trying to convert varchar date value to datetime format. Showing error
select CONVERT(DATETIME, Convert(varchar, 20/12/2009, 103 ),103)
error: Conversion failed when converting datetime from character string.
Geetha
This should work:
select CONVERT(DATETIME, '20/12/2009', 103)
Not sure what your conversion TO varchar is for...
You need quotes around the string otherwise it treats it as an expression and tries to do the divisions:
select CONVERT(DATETIME, Convert(varchar, '20/12/2009', 103 ),103)
Although having said that only one Convert is really necessary so you can use
select CONVERT(DATETIME, '20/12/2009',103)
I would say you at least have to add quotes arround the string representing the date :
select CONVERT(DATETIME, Convert(varchar, '20/12/2009', 103 ),103)
(Maybe there's another problem too : I don't have an SQL Server instance available, so I cannot test)
You need to quote the date string:
select CONVERT(DATETIME, Convert(varchar, '20/12/2009', 103 ),103)
And you don't need two converts. This is should be enough:
select Convert(varchar, '20/12/2009', 103 )
I need to select a datetime column in a table. However, I want the select statement to return the datetime as a nvarchar with the format DD/MM/YYYY.
Here is the convert documentation:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
Looking through that, it looks like you want style 103:
SELECT CONVERT(nvarchar(10), getdate(), 103)
This should help. It contains all (or most anyway) the different date formats
http://wiki.lessthandot.com/index.php/Formatting_Dates
I think you'd be better off handling the string conversion in client if possible.
You can convert a date in many formats, in your case :
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 103) => 15/09/2016
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 3) => 15/09/16
Syntax:
CONVERT('TheDataTypeYouWant', 'TheDateToConvert', 'TheCodeForFormating' * )
The code is an integer, here 3 is the third formatting option (without century), if you want the century just change the code to 103.
See more at: http://www.w3schools.com/sql/func_convert.asp
select CONVERT (NVARCHAR, GETDATE(), 103)
Look up convert in BOL.
Use Convert with the 103 option.
select convert(nvarchar(10), datefield, 103)