convert String YYYY_MM_DD to date - sql

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

Related

How to change a date to a different format

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

Formatting Date in sql

Just have an sql query that has a date in the format:
"2018-05-31"
Need to convert it to:
"May-18"
You can use Format function
Select format(getdate(), 'MMM-yyyy')
Try this:
SELECT LEFT(DATENAME(MONTH,GETDATE()),3) + '-' + RIGHT('00' + CAST(YEAR(GETDATE()) AS VARCHAR),2)
SELECT FORMAT(GETDATE(),'MMM yy')
you can try below way
Declare #Date DateTime = '2018-05-31'
Select Format(#Date, N'MMM-yy')
http://sqlfiddle.com/#!18/433d6/194
FORMAT can be very slow. This looks a little less intuative, however, if you have a large dataset, will probably be much quicker:
SELECT STUFF(STUFF(CONVERT(varchar(11),GETDATE(),13),1,3,''),4,3,'-');
This here could work:
declare #date date = '2018-05-31'
select *, FORMAT(DATEFROMPARTS(1900, right(YearMonthKey,2), 1), 'MMMM', 'en-US') +'-'+ substring(YearMonthKey,3,2) as MonthYearName from (
select LEFT(CAST(CONVERT(VARCHAR(8), #date, 112) AS INT),6) as YearMonthKey
)x
I would use convert() with style code 6 :
select replace(substring(convert(varchar(12), datecol, 6), 4, 6), ' ', '-')
You can change the way that a date or timestamp column is display at any time by altering your session to re-set nls_date_format. This will work in SQL*Plus or PL/SQL:
alter session set nls_date_format = 'MON-YY';

How can I handle a SQL query by using convert decimal string to datetime in SQL Server?

I am trying to convert a decimal string to datetime:
20160709.0000000 => 09-07-2016 (dd-MM-YYYY)
but the code here returns an error:
Conversion failed when converting date and/or time from character string
Code:
select
CONVERT(DATETIME, CONVERT(nvarchar(50), MyBirthDate), 112) BirthDate, Test, Test2
from
tbl
This code also does not work:
select
CONVERT(DATETIME, CONVERT(VARCHAR(50), '20160709.0000000'), 112)
The unseparated format you show (yyyymmdd) will be casted implicitly. I hope I do understand your convert decimal string correctly. Assuming, the value is of string type, you can cast this directly, just cut off the part you need:
DECLARE #s VARCHAR(100)='20160709.0000000';
SELECT CAST(LEFT(#s,8) AS DATETIME);
The result
2016-07-09 00:00:00.000
Hint
This solution would just work the same with the argument as decimal value, due to the implicit cast to string while passing into LEFT:
DECLARE #s DECIMAL(20,10)=20160709.0000000;
SELECT CAST(LEFT(#s,8) AS DATETIME);
At first you need to convert string to int or else you can use replace function
SELECT CONVERT(DATETIME, REPLACE('20160709.0000000', '.0000000', ''))
or
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), CAST(20160709.0000000 AS INT )), 112)
or
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), CAST(20160709.0000000 AS INT )))
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(50), CAST(20160709.0000000 AS INT )), 112)

Converting smalldatetime datatype to varchar datatype

How to convert smalldatetime to varchar? I've tried everything from http://msdn.microsoft.com/en-us/library/ms187928.aspx, but it didn't work.
I want to convert smalldatetime into varchar, because I want to use it in select like this:
select 'Some text'+#Date
thanks in advance
'121' is the format of the date in this case 'yyyy-mm-dd hh:mi:ss.mmm(24h)',
char(16) is the number of characters you wish to include, first 16 in this case.
select 'Some text'+convert(char(16), #date, 121)
Cast and Convert
SELECT CONVERT(VARCHAR(20), YourDateColumn, 103) as NewColumnName
here 103 make the date format as dd/mm/yyyy
if you want mm/dd/yyyy, you have to use 100
Here is a more up to date link.
The expression
'Some text ' + CONVERT(VarChar(20), [Data])
works fine, fiddle here, what style did you want?
Prueba con esto
DECLARE #FechaDesde datetime2
SET #FechaDesde = ''
SELECT LEFT(CONVERT(VARCHAR, #FechaDesde, 120), 10)
print #FechaDesde
---FECHA HASTA
DECLARE #FechaHasta datetime2
SET #FechaHasta = ''
SELECT LEFT(CONVERT(VARCHAR, #FechaHasta, 120), 10)
print #FechaHasta
SELECT * from compras where fec_emis between #FechaDesde and #FechaHasta

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