I am trying to convert a varchar to date using the below code.
SELECT CAST('14/08/2018' as date) --This code does not work
SELECT CAST('09/08/2018' as date) --This code works
It appears that when the day part of the date gets to '13' that is where it starts breaking.Is there a logical explanation for this?
The error given is :
Msg 241, Level 16, State 1, Line 7670
Conversion failed when converting date and/or time from character string.
You should decide your date component before conversation :
I would considered date with style dd/mm/yyyy :
SELECT CONVERT(DATE, '14/08/2018', 103)
However, it seems SQL has set date mm/dd/yyyy.
If so, you can change it :
set dateformat dmy
The cause of your problem is that you have a mm/dd/yyyy format and the first value is the month. You will either need to swap the first and the second value. You can use convert for this purpose with option 101, which converts a mm/dd/yyyy to a mm/dd/yyyy:
select convert(DATE, '14/08/2018', 101);
This is the US standard and this converts your varchar to a DATE. Now, if you want to display this in a format of dd/mm/yyyy, then just do
select convert(varchar(10), convert(DATE, '14/08/2018', 101), 103);
Related
Trying to select items from a database where the date that is being entered is converted to a different timezone.
In this case, the date is entered into the server in UTC time, and I've got a select query offsetting this date to meet the NZST time zone:
Code attempted
I'm also trying to select the items where the date in this 'NZSTdate' column match with today's date, and have had no such luck casting both items in the date format in a WHERE clause.
Just says the conversion failed from a character string.
SELECT *, convert(datetime, switchoffset(fulldate, datepart(tzoffset, fulldate AT TIME ZONE 'New Zealand Standard Time'))) AS 'NZSTdate'
FROM [Analytics].[dbo].[Call logs]
where cast('NZSTdate' as date) = cast(getdate() as date)
order by 'fulldate' desc
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I think you have a few things going on here.
"cast('NZSTdate') as date" looks suspicious. You almost certainly don't want the apostrophes around the field name like that.
Similarly, you don't want "fulldate" in apostrophes either.
Does this get you closer to what you're attempting?
create table #kl (dt datetimeoffset )
insert #kl values (getdate())
-- changing timezone
select switchoffset(dt, '-04:00')
from #kl
-- getting rid of time time component of dt and getdate()
where convert(date, dt) = convert(date, getdate() )
I'm trying to change the date format of data in column, that is had been inserted in this format
dd/mm/yyyy. I wanna change to this format yyyy-MM-dd in select statement.
I've tried by this query :
select CONVERT(nvarchar,cast(date1 as DATE),23) from Table1
On first two second I getting the result correctly then this Error appear:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
If you have inserted the value as a string, then you need to convert from that format. For the one you specify, you can try:
convert(date, datecol, 103)
Next, if the column is already a date, then your SQL Server settings might be controlling how the data is displayed. In this case, you can add a computed column for the format you want:
alter table t add datecol_yyyymmdd as (convert(varchar(10), datecol, 120));
If the column is a string and you want to convert the format, first check to see if all the values are as you expect. To return bad values:
select datecol
from t
where try_convert(date, datecol, 103) is null and datecol is not null;
If all are fine, then you can use:
update t
set datecol = convert(varchar(10), convert(date, datecol, 103), 120);
You can then alter the type to be a date.
I have a varchar column with the following format ddmmyyyy and I'm trying to convert it to date in the format dd-mm-yyyy. I'm using the query below but I get the error:
Conversion failed when converting date and/or time from character string.
select *, coalesce(try_convert(date, newdate, 105), convert(date, newdate))
from mydate
You don't have a date, you have a string. So, you can use string operations:
select stuff(stuff(newdate, 5, 0, '-'), 3, 0, '-')
If you want to convert to a date, you can do:
select convert(date, concat(right(newdate, 4), substring(newdate, 3, 2), left(newdate, 2)))
You could then format this as you want.
However, you should not be converting the value to a date. You should be storing it as a date in the first place.
To turn your string to a date, you can just [try_]cast() it; SQL Server is usually flexible enough to figure out the format by itself:
try_cast(newdate as date)
If you want to turn it back to a string in the target format, then you can use format():
format(try_cast(newdate as date), 'dd-MM-yyyy')
Compared to pure string operations, the upside of the try_cast()/format() approach is that it validates that the string is a valid date in the process.
Have to agree with the others. Why are you storing a date as a string in the first place? In a non-standard format, no less? Here's one way, but you should really fix the data model. Store dates as dates.
DECLARE #badIdea table (dt char(8));
INSERT #badIdea(dt) VALUES('21052020');
SELECT newdate = TRY_CONVERT(date, RIGHT(dt,4) + SUBSTRING(dt,3,2) + LEFT(dt,2))
FROM #badIdea;
BTW 105 won't work because it requires dashes. This works:
SELECT CONVERT(date, '21-05-2020', 105);
That's a bad format too, IMHO, because who knows if 07-08-2020 is July 8th or August 7th. But at least that one is supported by SQL Server. Your current choice is not.
SQL doesn't store date data types in different formats, and it's probably not a good idea to try and adjust this.
If, however, you are wanting a result set to simply display the date in a different format, you are on the right track. You just need to convert your date data type to a string.
SELECT *
, COALESCE ( TRY_CONVERT ( CHAR(10), newdate, 105 ), CONVERT ( CHAR(10), newdate ) )
FROM mydate
i am trying to pulling data for all shipments with shipmentdate greater than todays date. However, I cant figure out an easy conversion of the format of the nvarchar, i get a out of range value error when trying to run this:
select *
from dbo.BAS_CT_RAW_ARCHIVE_TBL
where SHIPMENTDATE > GETDATE()
Msg 242, Level 16, State 3, Line 1 The conversion of a nvarchar data
type to a datetime data type resulted in an out-of-range value.
Try:
select *
from dbo.BAS_CT_RAW_ARCHIVE_TBL
where convert(date, SHIPMENTDATE, 103) > GETDATE()
To see how to use convert with non-standard dates see this.
Further consideration: use proper datatypes for columns, i.e. don't store dates as strings, but as date datatype - it will prevent you from having such problems.
according to your data format that got from comment below should work
select * from dbo.BAS_CT_RAW_ARCHIVE_TBL
where CONVERT(date, SHIPMENTDATE, 103) > convert(date, GETDATE())
I have a date column where the date format is
2010-04-14
in SQL Server. Is there any possible way to retrieve the date format as
14/04/2010
in a select statement?
Try this:
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]
Source Link
Addendum 1:
As described in my comment, you need to convert your field to a DATETIME type before the above will work. This example should work on your SQL Server:
SELECT CONVERT(VARCHAR(10), CAST('2010-04-14' AS DATETIME), 103) AS [DD/MM/YYYY]
If you get this exception:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Then you need to change your date field to be in YYYY-MM-DD Format, EG: 2010-14-04
OR
Add the following line before the select statement:
SET DATEFORMAT MDY -- Input dates are in the MM/DD/YYYY format, change to DMY to handle UK dates
Example:
SET DATEFORMAT MDY;
SELECT CONVERT(VARCHAR(10), CAST('2010-04-14' AS DATETIME), 103) AS [DD/MM/YYYY]
Another way to solve your problem:
select substring(columnName, 9, 2) + '/' + substring(columnName, 6, 2) + '/' + substring(columnName, 1, 4)
If you are displaying that date outside of SqlServer, a better way would be to retrieve that date as date and convert it to the string-representation you want just before displaying it.
How to do that depends on your situation.
select replace(datetime,'-','/')