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,'-','/')
Related
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 am trying to convert this into a period format, so e.g. 2018_05 (YYYY_MM). currently the data is in DD/MM/YYYY format.
I tried a cast code but it returns me YYYY_DD.
SELECT
CASE WHEN RESERVED_FIELD_4 IS NULL THEN NULL
ELSE cast(year(RESERVED_FIELD_4) as Nvarchar (4))
+'_'+right('00'+cast(month(RESERVED_FIELD_4) as Nvarchar (2)),2)
END AS [DATAFEED_PERIOD]
I expect/want to see YYYY_MM.
Assuming RESERVED_FIELD_4 is a string type (char/nchar/varchar/nvarchar) the simplest solution would be to use substring:
CASE
WHEN RESERVED_FIELD_4 IS NULL THEN NULL
ELSE SUBSTRING(RESERVED_FIELD_4, 7, 4) + '_'+ SUBSTRING(RESERVED_FIELD_4, 4, 2)
END AS [DATAFEED_PERIOD]
If it's a date/datetime/datetime2 data type, the simplest solution would be to use format:
FORMAT(RESERVED_FIELD_4, 'yyyy_MM')
But for better performance you can use convert and stuff:
SELECT STUFF(CONVERT(char(6), RESERVED_FIELD_4, 112), 5, 0, '_')
In case your format is actually d/m/y the simplest option is to convert to date and than back to string:
SELECT STUFF(CONVERT(char(6), CONVERT(Date, RESERVED_FIELD_4, 103), 112), 5, 0, '_')
This is the common problem of storing a date with a VARCHAR column. You are guessing that the stored pattern is DD/MM/YYYY but the SQL engine doesn't know that and is currently assuming the MM/DD/YYYY pattern.
Please check these results:
-- MM/DD/YYYY
SELECT
DAY ('05/01/2019'), -- 1
MONTH('05/01/2019') -- 5
-- DD/MM/YYYY
SELECT
DAY ('25/05/2019'), -- Conversion failed when converting date and/or time from character string
MONTH('25/05/2019') -- Conversion failed when converting date and/or time from character string.
To display what you want correctly use string functions:
SELECT
RIGHT(RESERVED_FIELD_4, 4) + '_' + SUBSTRING(RESERVED_FIELD_4, 4, 2)
But you should actually fix the values on your VARCHAR column, cast them to DATE and store the values as DATE.
ALTER TABLE YourTable ADD ReservedField4Date DATE
UPDATE YourTable SET
ReservedField4Date = CONVERT(DATE,
RIGHT(RESERVED_FIELD_4, 4) -- Year
+ '-' + SUBSTRING(RESERVED_FIELD_4, 4, 2) -- Month
+ '-' + LEFT(RESERVED_FIELD_4, 2)) -- Day
ALTER TABLE YourTable DROP COLUMN RESERVED_FIELD_4
EXEC sp_rename 'SchemaName.YourTable.ReservedField4Date', 'RESERVED_FIELD_4', 'COLUMN'
Beware that changing the column type might affect other queries that assume this is a VARCHAR column.
If your data is in DD/MM/YYYY format, then it is being stored as a string. Hence, string functions come to mind:
select right(RESERVED_FIELD_4) + '_' + substrint(RESERVED_FIELD_4, 4, 2)
In SQL-SERVER you can use 'format'
format(dy,#your_date) as day_of_year
month(#your_date) as month
Try this:
Select concat(month(#your_date),'_'year(#your_date)) as your_period
this is a reference
Why not just do conversations ? :
SELECT REPLACE(CONVERT(VARCHAR(7), CONVERT(date, RESERVED_FIELD_4, 101), 102), '.', '_')
This assumes RESERVED_FIELD_4 is date type.
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);
I have sample data like this :
SELECT CONVERT(CHAR(19), CONVERT(DATE, '11/10/1997', 3), 120);
when i execute this I'm getting Error like this :
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character
string.
So then i have removed first 2 characters of year 1997
SELECT CONVERT(CHAR(19), CONVERT(DATE, '11/10/97', 3), 120);
And it gave Result Set like this :
Date
1997-10-11
So it got converted from DD/MM/YY Format to YYYY-MM-DD
Fine But why it has taken 1997 if I'm going to give 17 it will give as 2017.
If i give 37 it will give 2037 and if i give 67 and it is giving 1967 .
And lastly how can DD/MM/YYYY to YYYY-MM-DD Format
You need to use 103 when you specify four digit year:
SELECT CONVERT(DATE, '11/10/1997', 103)
-- 1997-10-11 (DATE)
If you want to convert the date back to a string, convert it again:
SELECT CONVERT(VARCHAR(10), CONVERT(DATE, '11/10/1997', 103), 120)
-- 1997-10-11 (VARCHAR)
Complete list of styles is available here.
Instead of 3 you can pun 103.
So :
SELECT CONVERT(CHAR(19), CONVERT(DATE, '11/10/1997', 103), 120); will return 1997-10-11
Can you try this?
declare #datechar as char(19) ='11/10/1997'
select FORMAT(convert(date,#datechar,103),'yyyy-MM-dd')
I need to convert date format in SQL
The current format is yyyy/mm/dd-hh:mm:ss:sss and I need to convert it to yyyy-mm-dd hh:mm:ss CST
I don't really know SQL but did some research and found that I can use instr to find the string and replace it, however, no matter what I try,there is always something off :(
Could anyone here help me with it? thanks in advance.
By the way, it's in oracle sql developer so syntax are different from previous answer. Thanks again
If your current column is stored as a varchar (which it looks like it is based on your example) you can convert it the following way to a datetime.
declare #date varchar(25)
set #date = '2012/02/16-09:40:30:000'
select Convert(datetime, Left(#date, 4) + '-' +
substring(#date, 6, 2) + '-' +
substring(#date, 9, 2) + ' ' +
substring(#date, 12, 8)) As NewDate
And the result would be 2012-02-16 09:40:30.000
There are lots of sites that have details on converting datetime:
http://www.sql-server-helper.com/tips/date-formats.aspx
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
If the date is stored as a datetime, then you can get your format by doing:
SELECT convert(varchar, getdate(), 120)
thank you so much for your guys help! I got this sorted out by other user's help, the command is,
update b
set first= to_char(substr(FIRST,1,4)||'-'||substr(FIRST, 6, 2)||'-'||substr(FIRST, 9, 2)||' '||substr(FIRST, 12, 8))
Thanks :)
Mylie
The CONVERT() function is normally used for this. The third parameter lets you specify the required date formatting.
If the date is already in a string, then convert it first into a date type and then convert it back to a string in the required format.
http://msdn.microsoft.com/en-us/library/ms187928.aspx
If you're using SQL Server and it's a date field you can do:
convert(varchar,getdate(),120)
Replacing getdate() with the column name
If the date is stored as a DATETIME, then you can convert it to a string in the format you want by doing the following:
SELECT CONVERT(VARCHAR(19),YourColumn,120)
FROM YourTable
CAST AND CONVERT
declare #ds varchar(23)='2012/02/16 12:57:03:002'; --your current format
select convert(varchar(30),cast(#ds as datetime),120)+' CST' --new format you specified