Sql Server datetime conversion - sql-server-2005

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 )

Related

Out-of-range value when converting dates separated by dots

How can I convert the following VARCHAR(50) data:
01.01.2020 06:10:15
to a DateTime?
Assuming that the dates are in dd.mm.yyyy format, you can use the date and time style #104 (German) that uses this exact format:
SELECT CONVERT(DATETIME, '13.01.2020 06:10:15')
-- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
SELECT CONVERT(DATETIME, '13.01.2020 06:10:15', 104)
-- 2020-01-13 06:10:15.000
You can try the below method:
SELECT CONVERT(datetime, '2017-08-25')
in place of the date under singled quotes you can place the variable name.
Try Below Code :
select convert(datetime, '01.01.2020 06:10:15', 104)
Help Link
You can achieve by this way
Declare #Str VARCHAR(50) = '01.01.2020 06:10:15'
Select CONVERT(date, REPLACE(#Str, '.', '/'), 103)

How to convert string dd.mm.yyyy to date in SQL

I have column field of varchar type in format dd.mm.yyyy. I am trying to convert this varchar field to date time in SQL server using
CONVERT(Datetime, LTRIM(RTRIM([Completion Date])), 102)
but it gives me error 'Conversion of varchar type to date type results in out of range value'
How to convert this field to datetime format?
use style 104 (dd.mm.yyyy) instead of 102 (yyyy.mm.dd)
CONVERT(Datetime, LTRIM(RTRIM([Completion Date])), 104)
Also the field of type varchar so there could be some bad dates which needs to be eliminated before the conversion .
If you are using SQL SERVER 2012+ then use TRY_CONVERT, for bad dates it will result NULL
TRY_CONVERT(Datetime, LTRIM(RTRIM([Completion Date])), 104)
You can always use this:
SELECT convert(datetime, '23/07/2009', 102)
You can also use this:
SELECT convert(date, '21.12.2016', 104)
You can use convert with date and datetime as below:
SELECT convert(datetime, '27-09-2013', 104)
SELECT convert(date, '27-09-2013', 104)

converting string format to date format

I am not able to convert the string to DATE format
select CONVERT(DATE, '8/17/2016', 103)
Conversion failed when converting date and/or time from character string.
what can we do to resolve this
thanks
Actually you don't need that style, It is the one causing the error. Just CAST/CONVERT it to date. Try this
select CONVERT(DATE, '8/17/2016')
You can try with cast also
select cast( '8/17/2016' as date)
SELECT CONVERT(DATE, '8/17/2016', 101)
SELECT convert(datetime, '8/17/2016', 111)

Selecting date in format dd/mm/yyyy from nchar column

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.

How do I convert a datetime column to nvarchar with the format DD/MM/YYYY?

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)