How to change a date to a different format - sql

This is a simple question.
How do I transform a date column that comes over as an int.
for example 20190327 convert over to 'MM/dd/yyyy'?
I tried
select *,format(Columnname, 'MM/dd/yyyy') from Table
but this didn't work.
Thank you!

You can try this:
declare #i int
select #i = 20190327
select CONVERT (datetime,convert(char(8),#i))

The following will give you in MM/dd/yyyy format.
SELECT *, CONVERT(nvarchar(30), cast(cast(Columnname as char(8)) as date), 101) as convertedToDate
FROM Table

Related

How to convert string in 'DD/MM/YYYY' or 'YYYY-MM-DD' into date in SQL Server?

I've got a string here which needs to be converted into date but the problem is that it could either be in 'DD/MM/YYYY' or 'YYYY-MM-DD' format.
I've already tried convert which only works for one of the two formats but not both:
declare #string nvarchar(255) = '2019-05-21'
declare #table table (date date)
insert into #table
select convert(date, #string, 111) as date
select * from #table
declare #string nvarchar(255) = '21/05/2019'
declare #table table (date date)
insert into #table
select convert(date, #string, 103) as date
select * from #table
Both of the above solutions result in an error is I use the other format.
Is there a way to get a string converted to date regardless of what format it is in?
Use try_convert():
insert into #table
select coalesce(try_convert(date, #string, 111),
try_convert(date, #string, 103)
) as date
try_convert() returns NULL if the conversion fails. In that case, the conversion will move on to the next pattern. With coalesce(), you can have as many different formats as you like.
You can use TRY_PARSE or PARSE to parse the date literal using a specific culture.
The second format YYYY-MM-DD is an unambiguous date format for the "new" date types like date and datetime2. It's not affected by the DATEFORMAT setting like datetime.
This means you only need to find one culture that can handle the first format. All of the following queries will return the same value :
select parse('21/05/2019' as date using 'en-GB')
-----
2019-05-21
select parse('2019-05-21' as date using 'en-GB')
-----
2019-05-21
select try_parse('21/05/2019' as date using 'en-GB')
-----
2019-05-21
select try_parse('2019-05-21' as date using 'en-GB')
-----
2019-05-21
If you are on SQL 2012 and above, you can use the FORMAT function.
The signature of this function is - FORMAT (value,format[,culture])
Example: SELECT FORMAT (getdate(), 'dd-MM-yyyy') as date and in your case SELECT FORMAT(CAST(<str_value> as DATE), 'yyyy-mm-dd')

sql server convert datetime failed

I want to convert this time_stamp column (nvarchar50) into datetime column in SQL server. the value of time_stamp is "2018-02-16 13:30:27+09:00".
I don't know which datetime code should I use to convert it. Can you help?
This is what I tried:
select convert(datetime,time_stamp, 110) from table;
select convert(datetime,time_stamp, 120) from table;
It is failing because of the timezone embedded in the string. However, it will work if you remove the timezone using string function such as LEFT.
SELECT CONVERT(DATETIME, LEFT(time_stamp, 19), 110)
FROM tableName
Here's a Demo.
There is timezone offset in your sample date. If we want to ignore timezone offset then we can use below code -
declare #x nvarchar(50) = '2018-02-16 13:30:27+09:00'
select convert(datetime,convert(datetimeoffset, #x))
Declare #dt NVARCHAR(100) = '2018-02-16 13:30:27+09:00'
select CAST(SWITCHOFFSET(TODATETIMEOFFSET( LEFT(#dt , 19) ,RIGHT(#dt, 6)),0) AS DATETIME)
Returns:
2018-02-16 04:30:27.000

convert String YYYY_MM_DD to date

For some reason the developer create the date column in my DB as a string and it is stored as YYYY_MM_DD.
Does anyone know how I can convert the YYYY_MM_DD to a date field via SQL. e.g
2014_06_30 to 30/6/2014.
Or any other solutions
Thank you in advance
Please try:
DECLARE #str NVARCHAR(100)='2014_06_30'
select CONVERT (DATETIME, REPLACE(#str, '_', '-'))
To convert it to format 30/6/2014, try:
select CONVERT(NVARCHAR(20), CONVERT(DATETIME, REPLACE(#str, '_', '-')), 103)
Try this,
DECLARE #str NVARCHAR(100)='2014_06_30';
select RIGHT(#str,2)+'/'+SUBSTRING(#str,6,2)+'/'+LEFT(#str,4)
var myDate = '2014_06_30';
var myNewDate = select CONVERT (datetime, Replace(myDate,'_','/'))

DATETIME SQL Query

I am using SQL Server 2008 Management Studio. I have a datetime column in my table.
When I select all contents of the table, the date column has data like 10/10/2013 12:00:00 AM.
I need a query to display all contents from the table with date column data as 10/10/2013.
SELECT CONVERT(VARCHAR(10), column, 101) -- u.s standard format mm/dd/yyyy
SELECT CONVERT(VARCHAR(10), column, 103) -- British/French standard format dd/mm/yyyy
Try this one (2008 and higher) -
SELECT CAST('10/10/2013 12:00:00 AM' AS DATE)
For 2005 -
SELECT CAST('10/10/2013 12:00:00 AM' AS VARCHAR(10))
Output -
10/10/2013
This will help you
cast(floor(cast(#dateVariable as float)) as datetime)
Using CONVERT function along with styles for DATETIME you can choose the way dates are displayed. 101 would give you mm/dd/yyyy, 103 = dd/mm/yyyy
SELECT CONVERT(NVARCHAR(20),your_datetime_col, 103) FROM your_table
SQLFiddle DEMO
You can use FORMAT() function:
SELECT FORMAT(<your_column>, 'MM/dd/YYYY') FROM <your_table>;
For your question i was created one table as
create table dateverify(date_v varchar(20));
Inserted single column value
declare #a varchar(20);
set #a='10/10/2013';
begin
insert into dateverify(date_v) values(#a);
end;
using below query,
SELECT CONVERT(VARCHAR(20), date_v , 103) AS [DD/MM/YYYY] from dateverify;
got an answer like you are asked..10/10/2013

T-SQL String to DateTime-conversion

I need to cast string values of the following formats to DateTime:
2042-04
2011-01
Is there an easy way to do this? I've tried CAST AND CONVERT without much luck.
Thanks!
try appending "-01" to the end of it and then doing the cast or convert
declare #S varchar(7)
set #S = '2042-04'
select cast(stuff(#S, 5, 1, '')+'01' as datetime)
YYYYMMDD is a safe format regardless of SET DATEFORMAT. YYYY-MM-DD is not. http://www.sommarskog.se/wishlist.html#YYYYMMDD
SELECT CAST('2011-01-01' AS DATETIME)
SELECT CONVERT(DATE , '2011-01-01')
It seems you need to add a 'day' to the string.
Declare #Table Table
(
ColDateTime Varchar(100)
)
Insert into #Table
Select '2042-04' UNION ALL
Select '2011-01'
Select ColDateTime As VarcharCol,
Cast(
substring(ColDateTime,0,charindex('-',ColDateTime))+substring(ColDateTime,charindex('-',ColDateTime)+1,len(ColDateTime))+'01'
As DateTime) As DateTimeCol
from #Table