Good Day
I am trying to convert a short date i.e 780506 to a date i.e 1978/05/06 , I have tried CASTING the date CAST(CONVERT(VARCHAR, 78) + '-' + CONVERT(VARCHAR, 05) + '-' + CONVERT(VARCHAR, 06)
AS DATETIME) but this is not working. I tried Converting the date:CONVERT(VARCHAR(6), 780506), 12) and also not working. Is there am easy way to do this? The reason I need this is that the first 6 digits of out countries ID number = Date of Birth, I am just trying to convert it into a workable date.
Thanks
Convert to date type:
DECLARE #s VARCHAR(10) = '780506'
SELECT convert(DATE, #s)
For the column:
SELECT CONVERT(DATE, ColumnName) FROM TableName
You can use CAST or CONVERT. Like this:
DECLARE #shortdate VARCHAR(10) = '780506'
SELECT CONVERT(DATE, #shortdate)
SELECT CAST(#shortdate AS DATE)
Result:
1978-05-06
Related
I have old DBF database that contains date in old 6 digit formats like 291292 or 150793 or even 010302.
I import tables into SQL Server and now I need to convert it to datetime format.
Of course must be converted to similar strings 29.12.1992, 15.07.1993, 01.03.2002 first.
You may try this.
GO
declare #date date
set #date = '901124'
select CONVERT(varchar(10), #date, 102) as [Date] --- for yyyy.MM.dd format
TRY THIS
declare #dd varchar(10)
set #dd = '201292'
select CONVERT(VARCHAR(10), #dd, 2), CONVERT(VARCHAR(10), GETDATE(), 2)
, (SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2))
, CONVERT(varchar(10), cast((SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2)) as DATE) , 102)
Ok it pretty simple:
GO
declare #date varchar(6)
set #date = '241190'
select CONVERT(date, concat(SUBSTRING(#date, 5,2),SUBSTRING(#date, 3,2),SUBSTRING(#date, 1,2)), 101) as [Date]
result is 1990-11-24
declare #date date = '901124'
select CONVERT(date, #date) as [Date]
I have a varchar column where some values are in mm/dd/yyyy format and some are in yyyymmdd.
I want to convert all mm/dd/yyyy dates into the yyyymmdd format. What is the best way to do this? Thanks
Table is Employees and column is DOB
Assuming your "date" column is not actually a date.
Select convert(varchar(8),cast('12/24/2016' as date),112)
or
Select format(cast('12/24/2016' as date),'yyyyMMdd')
Returns
20161224
DECLARE #v DATE= '3/15/2013'
SELECT CONVERT(VARCHAR(10), #v, 112)
you can convert any date format or date time format to YYYYMMDD with no delimiters
You can do as follows:
Select Format(test.Time, 'yyyyMMdd')
From TableTest test
try this....
SELECT FORMAT(CAST(DOB AS DATE),'yyyyMMdd') FROM Employees;
Select CONVERT(VARCHAR(8), GETDATE(), 112)
Tested in SQL Server 2012
https://www.w3schools.com/sql/func_sqlserver_convert.asp
In SQL Server, you can do:
select coalesce(format(try_convert(date, col, 112), 'yyyyMMdd'), col)
This attempts the conversion, keeping the previous value if available.
Note: I hope you learned a lesson about storing dates as dates and not strings.
SELECT YEAR(getdate()) * 10000 + MONTH(getdate()) * 100 + DAY(getdate())
mm/dd/yyyy corresponds to U.S. standard so if you convert to date using 101 value and then to varchar using 112 for ISO date get the expected result.
declare #table table (date_value varchar(10))
insert into #table values ('03/30/2022'),('20220330')
select date_value
--converted to varchar
,case
--mm/dd/yyyy pattern
when patindex('[0,1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]',date_value)>0 then convert(varchar(10),convert(date,date_value,101),112)
else date_value end date_value_new
--converted to date
,case
when patindex('[0,1][0-9]/[0-3][0-9]/[0-9][0-9][0-9][0-9]',date_value)>0 then convert(date,date_value,101)
else convert(date,date_value,112) end date_value_date
from #table
SELECT TO_CHAR(created_at, 'YYYY-MM-DD') FROM table;
//converts any date format to YYYY-MM-DD
Working in SQL Server, I have a column that contains a year in numeric format. I need to make that year into a January 1st date of that 'year'. I've tried a few commands and the latest attempt is:
cast('01/01/' + X.[YEAR] as datetime)
What am I missing?
DECLARE #Year INT = 2010
SELECT CAST(CAST(#Year AS varchar) + '-1-1' AS DATETIME) -- 2010-01-01
Another way:
select GETDATE(),
DATEADD (day, - DATEPART(dayofyear, GETDATE()) + 1, CONVERT(date, GETDATE()))
I have a string '2016-03-05' which I want convert to a datetime.
Here is my code:
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 112).
Running that I receive the error
Conversion failed when converting datetime from character string.
Conversion format 112 you've used assumes input '20160305' (without dashes as date parts separator).
So either do
select convert(Datetime, '20160305', 112)
or (if your input really contains dashes then just remove them like:
select convert(Datetime, replace('2016-03-05', '-', ''), 112)
That's because the style you're passing to the CONVERT function does not support that format. You can do two things...
1- Drop the dashes
Declare #period as nvarchar(10)
Set #period = '20160305' -- I've dropped the dashes here
Select Convert(Datetime, #period, 112)
2- Change the style to something that supports this format
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 21) -- I've changed the style here
For a complete reference, read the MSDN documentation
SELECT LEFT(201605,4) + '-' + RIGHT(201605,2)
{OR}
SELECT RIGHT(201605,2) + '-' + LEFT(201605,4)
Try this it will work I guess according to your need
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
select CAST(#period as datetime)
I am inserting a datetime field into my SQL database with a format of 2014-10-29 12:05:24.927
My interface needs to display this as 29/10/2014 12:05:24
my tsql statement is :
SELECT Convert(VARCHAR(20), MessageAudit.AuditDate, 120) As AuditDate FROM tableA
Which, produces yyyy-MM-dd hh:mm:ss how, can i format the date portion of this to be dd/MM/yyyy?
You can use something like:
declare #Date datetime
select #Date = cast('20141029 12:05:24.927' as datetime)
select convert(nvarchar(10), #Date, 103) + ' ' + convert(nvarchar(10), #Date, 108)
It will give you precisely 29/10/2014 12:05:24
Concatenate two format dd/mm/yy & hh:mm:ss
e.g
SELECT CONVERT(NVARCHAR(10), GETDATE(), 103) + ' ' +
CONVERT(NVARCHAR(10), GETDATE(), 108) AS [Date]
Here's a pretty extensive list of date formats in sql server:
http://www.sql-server-helper.com/tips/date-formats.aspx
for your specific problem you could do:
SELECT CONVERT(VARCHAR(20), AuditDate, 103) + ' ' +
CONVERT(VARCHAR(20), AuditDate, 108) AS AuditDate
FROM tableA
Though this will return the date as a string/varchar - which has it's own set of issues. You're really better off keeping the date in a date format and formatting it on the front end. This will allow you (depending on how the date is displayed) to still be "date" sortable rather than string sortable.
As http://msdn.microsoft.com/it-it/library/ms187928.aspx, you have to specify 103 as the third parameter of the CONVERT() function.
SELECT Convert(VARCHAR(20), MessageAudit.AuditDate, 103) As AuditDate FROM tableA