datetime varchar yyyymmdd_hhmiss to datetime - sql-server

I have datetime format in varchar yyyymmdd_hhmiss format. I need to convert it to SQL Server datetime .
Is there any easier way ?
for example, 20150622_012030 into '2015-06-22 01:20:30.000' datetime
I got my code below but not sure how to convert time part
SELECT CONVERT(DATEtime,LEFT('20150617_015814',8)), RIGHT('20150617_015814',6)

-- Change the format to yyyyMMdd HH:mm:ss then do the convert
SELECT CONVERT(datetime,
STUFF(
STUFF(
REPLACE('20150622_012030', '_', ' ')
, 14, 0, ':')
, 12, 0, ':')
)
-- Result
2015-06-22 01:20:30.000

Try this
declare #datetime varchar(6) = right('20150622_012030',6)
declare #date varchar(10) = left('20150622_012030',8)
SELECT convert(datetime,#date+' '+left(#datetime,2)+':'+RIGHT(left(#datetime,4),2)+':'+RIGHT(#datetime,2))

Related

SQL Server - convert short date in from old database

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]

Convert 24h to 12h datetime format in SQL Server

I want a datetime variable which will be having 12 hour datetime format.
Below example converts date in 12 hour but not helpful because it is in VARCHAR format, if tries to convert in datetime it shows like 2016-03-08 00:00:00.000-
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
SELECT CONVERT(VARCHAR, #t, 100) AS DateTime_In_12h_Format
I want a variable which will be holding 12 hour format something like this
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
set #t = CONVERT(datetime, #t, 100)
select #t -> this should be -> Mar 8 2016 12:00AM
If you want to convert the current datetime for example:
SELECT CONVERT(VARCHAR, getdate(), 100) AS DateTime_In_12h_Format
Instead of getdate() you can put your desired column in a query (such as tdate in your example). If you want JUST the time in 12h and not the date and time use substring/right to separate them. It seems that you already know how to =).
This page lists every datetime conversion. It's really handy if you need other types of conversions.
Declare one more varchar to save new datetime
declare #t datetime = NULL
declare #t1 varchar(20) = NULL
set #t = '2016-03-08 00:00:00'
set #t1 = CONVERT(varchar(20), #t, 100)
select #t1 as DateTime_In_12h_Format

How to convert string YYYYMM to datetime in sql?

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)

How to convert datetime string without delimiters in SQL Server as datetime?

I am looking to convert a string like this: 20160520191959550 - that is actually date-time for 2016-05-20 19:19:59.
I tried using CAST as datetime in a SQL statement, but got this error:
Conversion failed when converting date and/or time from character string.
Here is the SQL:
Select Cast(vbon.DATE_INS As datetime) As DINS
From vbon
I routinely use CAST on date strings without delimiters and am looking for a similar solution. Is there a simple way to get the job done without reverting to LEFT, RIGHT and SUBSTRING?
Addtional variant to already posted:
SELECT CONVERT(DATETIME, FORMAT(CONVERT(BIGINT, '20160520191959550'),
'####-##-## ##:##:##"."###'))
In SQL Server 2012 and later, you can use DATETIMEFROMPARTS:
DECLARE #DateTimeString varchar(19) = '20160520191959550';
SELECT DATETIMEFROMPARTS(
SUBSTRING(#DateTimeString,1,4)
, SUBSTRING(#DateTimeString,5,2)
, SUBSTRING(#DateTimeString,7,2)
, SUBSTRING(#DateTimeString,9,2)
, SUBSTRING(#DateTimeString,11,2)
, SUBSTRING(#DateTimeString,13,2)
, SUBSTRING(#DateTimeString,15,3)
);
In earlier versions, one method is to build a ISO 8601 formatted string and use CAST or CONVERT:
SELECT CAST(
SUBSTRING(#DateTimeString,1,4)
+ '-' + SUBSTRING(#DateTimeString,5,2)
+ '-' + SUBSTRING(#DateTimeString,7,2)
+ 'T' + SUBSTRING(#DateTimeString,9,2)
+ ':' + SUBSTRING(#DateTimeString,11,2)
+ ':' + SUBSTRING(#DateTimeString,13,2)
+ '.' + SUBSTRING(#DateTimeString,15,3)
AS datetime2(3));
SELECT CAST(
STUFF(
STUFF(
STUFF(
STUFF('20160520191959550', 9, 0, ' ')
, 12, 0, ':')
, 15, 0, ':')
, 18, 0, '.') AS DATETIME)
I suggest you to create function:
CREATE FUNCTION dbo.datestringtodatetime
(
-- Add the parameters for the function here
#d NVARCHAR(max)
)
RETURNS datetime
AS
BEGIN
-- Declare the return variable here
DECLARE #table TABLE (
id int,
[pos] int,
[sym] char(1)
)
DECLARE #output datetime
--In this table we determine were to put delimeters
INSERT INTO #table VALUES
(1, 5,'-'),
(2, 8,'-'),
(3, 11,'T'),
(4, 14,':'),
(5, 17,':'),
(6, 20,'.')
;WITH cte AS (
SELECT STUFF(#d,t.[pos],0,t.[sym]) as d_, 1 as l
FROM #table t
WHERE t.id = 1
UNION ALL
SELECT STUFF(c1.d_,t.[pos],0,t.[sym]), l+1
FROM cte c1
INNER JOIN #table t
ON t.id = l+1
)
SELECT #output = TRY_CAST(d_ as datetime)
FROM cte
WHERE l = 6
-- Return the result of the function
RETURN #output
END
GO
You can call it like:
SELECT [dbo].[datestringtodatetime] ('20160520191959550')
Output:
2016-05-20 19:19:59.550
If you pass it wrong value you will get NULL
I think convert would work for you. I can't test right now but I have done what you are looking for with dates. Here is a good article:
http://www.sqlusa.com/bestpractices/datetimeconversion/
You can try this:
SELECT CONVERT(VARCHAR(8), vbon.DATE_INS, 112) + REPLACE(CONVERT(varchar, vbon.DATE_INS, 108), ':','')
Ref: Convert DateTime to yyyyMMddHHmm in T-SQL

Obtain below date format in SQL server

Please suggest how to format date into this format '05/23/2014 09:56:07 AM' in SQL server.
Is there a style code to be passed to CONVERT function ?
From Sql Server 2012 onwards you can use following query:-
SELECT FORMAT(GETDATE(), 'G');
Otherwise convert function is used to coverts the date into desired fpormat.
try this
select convert(varchar(50), getdate(), 131)
for detail please read http://msdn.microsoft.com/en-us/library/ms187928.aspx
Check this
declare #d datetime = getdate()
select
#d,
CONVERT ( varchar(30), #d, 101 ) ,
convert( varchar(12), #d, 101) + stuff( right( convert( varchar(26),#d, 109 ), 15 ), 7, 7, ' ' ) as formatedDate

Resources