how to format getdate into YYYYMMDDHHmmSS - sql-server-2005

In SQL Server how do I format getdate() output into YYYYMMDDHHmmSS where HH is 24 hour format?
I've got the YYYYMMDD done with
select CONVERT(varchar,GETDATE(),112)
but that is as far as I got.
Thanks.

Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function:
SELECT FORMAT ( GETDATE(), 'yyyyMMddHHmmss') AS 'Custom DateTime'
This allows any .NET format strings making it a useful new addition.

select replace(
replace(
replace(convert(varchar(19), getdate(), 126),
'-',''),
'T',''),
':','')

Close but not exactly what you are asking for:
select CONVERT(varchar, GETDATE(), 126)
e.g.
2011-09-23T12:18:24.837
(yyyy-mm-ddThh:mi:ss.mmm (no spaces), ISO8601 without timezone)
Ref: CAST and CONVERT
There is no way to specify a custom format with CONVERT(). The other option is to perform string manipulation to create in the format you desire.

Try this:
select CONVERT(varchar, GETDATE(), 120)
e.g.
2011-09-23 12:18:24
(yyyy-mm-dd hh:mi:ss (24h) ,ODBC canonical).
Hth.

Another option!
SELECT CONVERT(nvarchar(8), GETDATE(),112) +
CONVERT(nvarchar(2),DATEPART(HH,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(MI,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(SS,GETDATE()));

converting datetime that way requires more than one call to convert. Best use for this is in a function that returns a varchar.
select CONVERT(varchar,GETDATE(),112) --YYYYMMDD
select CONVERT(varchar,GETDATE(),108) --HH:MM:SS
Put them together like so inside the function
DECLARE #result as varchar(20)
set #result = CONVERT(varchar,GETDATE(),112) + ' ' + CONVERT(varchar,GETDATE(),108)
print #result
20131220 13:15:50
As Thinhbk posted you can use select CONVERT(varchar,getdate(),20) or select CONVERT(varchar,getdate(),120) to get quite close to what you want.

select CONVERT(nvarchar(8),getdate(),112) +
case when Len(CONVERT(nvarchar(2),DATEPART(HH,getdate()))) =1 then '0' + CONVERT(nvarchar(2),DATEPART(HH,getdate())) else CONVERT(nvarchar(2),DATEPART(HH,getdate())) end +
case when Len( CONVERT(nvarchar(2),DATEPART(MI,getdate())) ) =1 then '0' + CONVERT(nvarchar(2),DATEPART(MI,getdate())) else CONVERT(nvarchar(2),DATEPART(MI,getdate())) end

Related

Using Substring and Convert Datetime Sql Server 2017

This is my query:
declare #date char(10)
set #date = '11.08.1982'
select substring(#date,1,2)+ '/'+
SUBSTRING(#date,3,1)+ '/'+ SUBSTRING(#date,4,4) as resultat
I want to use CONVERT function to convert the result to datetime. How can I do this?
You can't convert this to date and time, however you can use replace function :
select replace(#data, '.', '/')
SQL Server is pretty good about picking up the format of a date with no conversion format. So, this works on db<>fiddle, assuming you intend MM.DD.YYYY for the format:
select convert(date, '11.08.1982')
If you want DD.MM.YYYY, then you can explicitly use the "104" format:
select convert(date, '11.08.1982', 104)
You don't have a time component, so I converted these to date. The same works for datetime.

Convert date to nvarchar and merge two columns

I am using SQL Server 2008 and I have two columns in date format:
Column_1: [2014-12-19]
Column_2: [2015-08-31]
I want to merge them and change the the data type to NVARCHAR.
I tried this code
CONVERT(NVARCHAR,[ Column_1])+CONVERT(NVARCHAR,[Column_2])AS TEST
but I get this result:
2014-12-192015-08-31
instead of 2014121920150831 without the hyphens.
Could you please help?
Thanks
Although you can fiddle around with conversion codes, just use replace:
REPLACE(CONVERT(NVARCHAR(255), Column_1) + CONVERT(NVARCHAR(255), Column_2), '-', '') AS TEST
Or, if you don't want to be dependent on the local date format:
CONVERT(NVARCHAR(255), Column_1, 112) + CONVERT(NVARCHAR(255), Column_2, 112) AS TEST
CONVERT has a third parameter which determines the format of the date/time. See here for definition. Code 112 will give you what you want.
You can also use REPLACE to remove the hyphens.
Try this
DECLARE #date1 date = '2014-12-19',
#date2 date = '2015-08-31'
SELECT CONVERT(VARCHAR(8), #date1, 112)+CONVERT(VARCHAR(8), #date2, 112)

SQL - Convert varchar to datetime

Any way to convert this varchar -> "18-Jan-2015 12:43:51" to datetime in SQL database?
Thanks.
Just like everybody said above, the best way to do it is either with convert or cast, besides, this is very basic sql
here are both cases:
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME) AS DATE
SELECT CONVERT (DATETIME, REPLACE('18-Jan-2015 12:43:51', '-', ' '))
Use convert with replace:
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '), 113)
Try this:
SELECT (CONVERT(DATETIME,LEFT('18-Jan-2015 12:43:51',23),101))
You can use statement like below
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '))
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME)

Combining (concatenating) date and time into a datetime

Using SQL Server 2008, this query works great:
select CAST(CollectionDate as DATE), CAST(CollectionTime as TIME)
from field
Gives me two columns like this:
2013-01-25 18:53:00.0000000
2013-01-25 18:53:00.0000000
2013-01-25 18:53:00.0000000
2013-01-25 18:53:00.0000000
.
.
.
I'm trying to combine them into a single datetime using the plus sign, like this:
select CAST(CollectionDate as DATE) + CAST(CollectionTime as TIME)
from field
I've looked on about ten web sites, including answers on this site (like this one), and they all seem to agree that the plus sign should work but I get the error:
Msg 8117, Level 16, State 1, Line 1
Operand data type date is invalid for add operator.
All fields are non-zero and non-null. I've also tried the CONVERT function and tried to cast these results as varchars, same problem. This can't be as hard as I'm making it.
Can somebody tell me why this doesn't work? Thanks for any help.
Assuming the underlying data types are date/time/datetime types:
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), CollectionDate, 112)
+ ' ' + CONVERT(CHAR(8), CollectionTime, 108))
FROM dbo.whatever;
This will convert CollectionDate and CollectionTime to char sequences, combine them, and then convert them to a datetime.
The parameters to CONVERT are data_type, expression and the optional style (see syntax documentation).
The date and time style value 112 converts to an ISO yyyymmdd format. The style value 108 converts to hh:mi:ss format. Evidently both are 8 characters long which is why the data_type is CHAR(8) for both.
The resulting combined char sequence is in format yyyymmdd hh:mi:ss and then converted to a datetime.
The simple solution
SELECT CAST(CollectionDate as DATETIME) + CAST(CollectionTime as DATETIME)
FROM field
An easier solution (tested on SQL Server 2014 SP1 CU6)
Code:
DECLARE #Date date = SYSDATETIME();
DECLARE #Time time(0) = SYSDATETIME();
SELECT CAST(CONCAT(#Date, ' ', #Time) AS datetime2(0));
This would also work given a table with a specific date and a specific time field. I use this method frequently given that we have vendor data that uses date and time in two separate fields.
Cast it to datetime instead:
select CAST(CollectionDate as DATETIME) + CAST(CollectionTime as TIME)
from field
This works on SQL Server 2008 R2.
If for some reason you wanted to make sure the first part doesn't have a time component, first cast the field to date, then back to datetime.
DECLARE #ADate Date, #ATime Time, #ADateTime Datetime
SELECT #ADate = '2010-02-20', #ATime = '18:53:00.0000000'
SET #ADateTime = CAST (
CONVERT(Varchar(10), #ADate, 112) + ' ' +
CONVERT(Varchar(8), #ATime) AS DateTime)
SELECT #ADateTime [A nice datetime :)]
This will render you a valid result.
Solution (1): datetime arithmetic
Given #myDate, which can be anything that can be cast as a DATE, and #myTime, which can be anything that can be cast as a TIME, starting SQL Server 2014+ this works fine and does not involve string manipulation:
CAST(CAST(#myDate as DATE) AS DATETIME) + CAST(CAST(#myTime as TIME) as DATETIME)
You can verify with:
SELECT GETDATE(),
CAST(CAST(GETDATE() as DATE) AS DATETIME) + CAST(CAST(GETDATE() as TIME) as DATETIME)
Solution (2): string manipulation
SELECT GETDATE(),
CONVERT(DATETIME, CONVERT(CHAR(8), GETDATE(), 112) + ' ' + CONVERT(CHAR(8), GETDATE(), 108))
However, solution (1) is not only 2-3x faster than solution (2), it also preserves the microsecond part.
See SQL Fiddle for the solution (1) using date arithmetic vs solution (2) involving string manipulation
Concat date of one column with a time of another column in MySQL.
SELECT CONVERT(concat(CONVERT('dateColumn',DATE),' ',CONVERT('timeColumn', TIME)), DATETIME) AS 'formattedDate' FROM dbs.tableName;
drop table test
create table test(
CollectionDate date NULL,
CollectionTime [time](0) NULL,
CollectionDateTime as (isnull(convert(datetime,CollectionDate)+convert(datetime,CollectionTime),CollectionDate))
-- if CollectionDate is datetime no need to convert it above
)
insert test (CollectionDate, CollectionTime)
values ('2013-12-10', '22:51:19.227'),
('2013-12-10', null),
(null, '22:51:19.227')
select * from test
CollectionDate CollectionTime CollectionDateTime
2013-12-10 22:51:19 2013-12-10 22:51:19.000
2013-12-10 NULL 2013-12-10 00:00:00.000
NULL 22:51:19 NULL
This works in SQL 2008 and 2012 to produce datetime2:
declare #date date = current_timestamp;
declare #time time = current_timestamp;
select
#date as date
,#time as time
,cast(#date as datetime) + cast(#time as datetime) as datetime
,cast(#time as datetime2) as timeAsDateTime2
,dateadd(dayofyear,datepart(dayofyear,#date) - 1,dateadd(year,datepart(year,#date) - 1900,cast(#time as datetime2))) as datetime2;
dealing with dates, dateadd must be used for precision
declare #a DATE = getdate()
declare #b time(7) = getdate()
select #b, #A, GETDATE(), DATEADD(day, DATEDIFF(day, 0, #a), cast(#b as datetime2(0)))
I am using SQL Server 2016 and both myDate and myTime fields are strings. The below tsql statement worked in concatenating them into datetime
select cast((myDate + ' ' + myTime) as datetime) from myTable
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), date, 112) + ' ' + CONVERT(CHAR(8), time, 108))
FROM tablename

Add characters in the variable

I need to make the string/varchar variable with value 20061212 to be string/varchar value 2006-12-12 00:00:00
I can't find the right SQL syntax code to make it happened in SQL Server.
The code can be created as stored procedure or similar in SQL Server.
SELECT
LEFT(#string, 4)
+ '-' + SUBSTRING(#string, 5, 2)
+ '-' + SUBSTRING(#string, 7, 2)
+ ' 00:00:00'
You could cast your string to a DATETIME and then convert it back to a string in your chosen format. But that would likely be slower than this more explicit option.
SELECT
CONVERT(VARCHAR(19), CAST(#string AS DATETIME), 120)
You can use stuff
declare #s varchar(8) = '20061212'
select stuff(stuff(#s, 7, 0, '-'), 5, 0, '-')+' 00:00:00'
You can use the SUBSTRING() function (detailed here: http://www.sql-statements.com/sql-substring.html) to take apart the original string, and then put the pieces back together with the added dashes & time at the end using the concatenation operator +.
Culture independent variant:
SELECT CONVERT(varchar, convert(datetime, '20061212', 112), 120)
BUT this only valid for DATETIME-allowed range of dates, use DATETIME2 instead