Converting date format of column in sql server - sql

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.

Related

SQL Server : change date format

I need to change the date format from 'yyyy-mm-dd' to 'dd.mm.yyyy'.
I have data in my table like this '2018-08-08', I need convert it to '08.08.2018'.
I have tried:
UPDATE daily_tasks
SET date = REPLACE(date, date, CONVERT(VARCHAR(255), daily_tasks.date, 102))
WHERE 1;
But, it doesn't work.
Ideally you should be storing your dates as bona-fide date columns, not as text. That being said, the date text '2018-08-08' is in fact in an ISO format, and would still allow you to do things like sort and compare against other date literals, so it is not so bad.
But converting this text to a '08.08.2018' format is the wrong thing to do. If a anything, you might want to consider adding a new date column new_date to store this date information. Do that, and then populate it with:
UPDATE daily_tasks
SET new_date = TRY_CONVERT(datetime, date);
Store your date as DATE datatype and when you read data from database use
DECLARE #myDate DATE = '2018-08-08'
SELECT FORMAT(#myDate, 'dd.MM.yyyy')
SELECT CONVERT(VARCHAR(10), #myDate, 104)
Your syntax looks like SQL Sever, so i would do :
UPDATE daily_tasks
SET Col = REPLACE(CONVERT(VARCHAR(10), daily_tasks.date, 103), '/', '.')
WHERE . . . ;
However, i would not recommend to do this, just use CONVERT() with SELECT statement whenever necessary :
SELECT REPLACE(CONVERT(VARCHAR(10), daily_tasks.date, 103), '/', '.')
Regardless of the database, dates are stored in an internal format. This is the correct way to store dates. Do not store dates as strings.
You can specify the format when you query:
CONVERT(VARCHAR(255), daily_tasks.date, 102)
Or, you can even add a computed column to provide this information:
alter table daily_tasks
add date_display as ( CONVERT(VARCHAR(255), daily_tasks.date, 102) ) ;
You could convert the date column to a varchar to store the date in your specified format. However I strongly recommend against this. You should leave it stored as a date.
If you want to do a SELECT to get the data out then you can convert it to your specified format like this:
SELECT CONVERT(VARCHAR, daily_tasks.date, 4)

SQL server date convertion

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);

show data with shipmentdate bigger than today

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())

SQL converting Date datatype from Varchar to Date

so I'm trying to convert my date column datatype from varchar to date.
Currently my date is in d/m/yyyy format and I want to convert to standard mm/dd/yyyy
Here's the script that I'm running
update [table]
set [PERIOD]= CONVERT(varchar(20),cast([PERIOD] as date),101)
But I'm getting an error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Any tips or am I stuck?
I suggest instead of updating one string date to another format of string date, that you add a new DATE or DATETIME field to store the dates properly.
ALTER [table] ADD Proper_Date DATE
GO
UPDATE [table]
SET Proper_Date = convert(date, [PERIOD], 103)
If you must, you can wrap the above in another CONVERT():
update [table]
set [PERIOD]= CONVERT(varchar(20),convert(date,[PERIOD], 103),101)
I think if you can segregate the day and month and the year by using substring or any other string functions then use the below query
update tableA
set period = convert(datetime,cast(day(1)as varchar)+'/'+cast(MONTH(1)as varchar)+'/2014',102)
Hope its works.
You may have junk value and can get the junk values by executing the following query
SELECT [PERIOD]
FROM YOURTABLE
WHERE ISDATE([PERIOD]) = 0
To avoid junk values and update the rest try the below
update YOURTABLE
set [PERIOD]= CONVERT(varchar(20),cast([PERIOD] as date),101)
WHERE ISDATE([PERIOD]) = 1
Try this.
update [table]
set [PERIOD]= CONVERT(varchar(20),CONVERT(date,[PERIOD] ,103),101)
Problem in your query is
cast([PERIOD] as date)
You cannot directly convert d/m/yyyy format to date. So use 103 style to convert

Date conversion issue.: yyyy/MM/dd to dd/MM/yyyy

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,'-','/')