Datetime pattern for yyyy.mm.dd.hh.mm.ss pattern code? - sql

What is the DATE FORMAT CODE for "yyyy.mm.dd.hh.mm.ss"?
I know that 34 (date format code) is "yyyymmddhhmmss", but what about the code for "yyyy.mm.dd.hh.mm.ss"?
This is on SQL 2005.

CAST and CONVERT on MSDN says "no".
You have to CONVERT twice with styles 102 and 108, with a concatenation and REPLACE.
Where did you get the "34" date format code from?

As gbn said, using one of the existing formats with some string concatenation would work. Another option is:
SELECT
CAST(YEAR(my_date) AS CHAR(4)) + '.' +
RIGHT('0' + CAST(MONTH(my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DAY(my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(HOUR, my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(MINUTE, my_date) AS VARCHAR(2)), 2) + '.' +
RIGHT('0' + CAST(DATEPART(SECOND, my_date) AS VARCHAR(2)), 2)

Considering
SELECT CONVERT(VARCHAR(16), GETDATE(), 120) AS [YYYY-MM-DD]
---returns--- yyyy-mm-dd hh:ss
and you can use REPLACE to convert strings
REPLACE('a b c',' ','.')
----returns--- a.b.c
and you can recursively stack things you get to this
Select (
replace((replace((replace(CONVERT(VARCHAR(16), GETDATE(), 120),' ','.')), ':', '.')), '-', '.')
)
which returns: yyyy.mm.dd.hh.ss
works great for datetime stamps or filenames!

Related

SQL Syntax Error When Adding Convert Function

This works
SELECT
LEFT(DATENAME(DAY, GETDATE()), 3) + '-' +
LEFT(DATENAME(MONTH, GETDATE()), 3) + ' ' + '-' +
RIGHT('00' + CAST(YEAR(GETDATE()) AS VARCHAR), 2)
This also works
SELECT CONVERT(TIME(0), GETDATE())
But when I combine both then I get an error
SELECT
LEFT(DATENAME(DAY, GETDATE()), 3) + '-' +
LEFT(DATENAME(MONTH, GETDATE()), 3) + ' ' + '-' +
RIGHT('00' + CAST(YEAR(GETDATE()) AS VARCHAR), 2) + ' '
CONVERT(TIME(0), GETDATE())
This doesn't answer your question, but your first query is overcomplicated. I don't understand the date format, but whatever you need, this is a simpler method:
SELECT DATENAME(DAY, GETDATE()) + '-' +
LEFT(DATENAME(MONTH, GETDATE()), 3) + ' -' +
RIGHT(DATENAME(YEAR, GETDATE()), 2)
Notes:
The DAY component is never more than 2 characters, so LEFT(. . . , 3) is unnecessary.
' ' + '-' can be simplified to ' -'.
You can use DATENAME() on the year as well.
You can try below - you need to add + operator and also cast it to varchar()
SELECT LEFT(DATENAME(Day,GETDATE()),3) + '-' +
LEFT(DATENAME(MONTH,GETDATE()),3) + ' ' + '-' +
RIGHT('00' + CAST(YEAR(GETDATE()) AS VARCHAR),2) + ' ' +
cast(convert(time(0),getDate()) as varchar(10))

SQL SERVER -- YEAR

Select (SELECT YEAR(GETDATE())+(Select('-'))+(SELECT YEAR(GETDATE())+1));
I want output as "2018-2019".
Anyone?
You can use + to append string since you are working with SQL 2008, higher version has CONCAT() function :
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) + ' - ' + CAST(YEAR(DATEADD(YEAR, 1, GETDATE())) AS VARCHAR(4))
Use datename():
select (datename(year, getdate()) + '-' + datename(year, dateadd(year, 1, getdate()))
That way, you don't have to deal with converting integers to strings.
SELECT CONCAT(YEAR(GETDATE()), '-', YEAR(GETDATE()) + 1)
CONCAT takes care of conversion to string, so you don't have to worry about that.
edit: Unfortunately I have been made aware that CONCAT is only available in sql server 2012 and higher.
You can also use
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(YEAR(GETDATE()) + 1 AS VARCHAR(4))
OR
SELECT CAST(DATEPART(Year,GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(DATEPART(Year, GETDATE()) + 1 AS VARCHAR(4))

DATETIME losing hh:mm when changing CREATE TABLE to SELECT INTO

I am currently migrating all of my company's reports into Splunk Data Labs input for ingestion. The reports create temp tables using the CREATE TABLE format, which is incompatible with Splunk, however, SELECT INTO format works just fine.
The error that I am getting however when changing to the SELECT INTO format, is the DATETIME variable which should be MM/DD/YYYY hh:mm format loses the hh:mm end, and instead shows MM/DD/YYYY MM/DD/YYYY:
Original SQL:
CREATE TABLE #Stats#(date_slice DATETIME NULL, raw_value REAL NULL)
INSERT INTO #Stats#
SELECT CONVERT(CHAR(11), data_datetime, 111) + ' ' +
CASE WHEN DATEPART(MINUTE, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END AS date_hour
,SUM(ship_qty) AS moves
FROM #tmpAllData
GROUP BY CONVERT(CHAR(11), data_datetime, 111) + ' ' +
case when DATEPART(minute, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END
ORDER BY 1
Modified SQL:
--CREATE TABLE #Stats#(date_slice DATETIME NULL, raw_value REAL NULL)
SELECT CONVERT(CHAR(11), data_datetime, 111) + ' ' +
CASE WHEN DATEPART(MINUTE, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END date_slice
,SUM(ship_qty) raw_value
INTO #Stats#
FROM #tmpAllData
GROUP BY CONVERT(CHAR(11), data_datetime, 111) + ' ' +
case when DATEPART(minute, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END
ORDER BY 1
Original Output: "07/12/2018 10:00:00 "
Modified Output: "2018/07/12 2018/07/12"
You second statement is creating the temporary table #Stats without any preset column definitions. It instead creates the columns based on the return data type of the SELECT
This means that the SQL Server is not reading the output in question as a DATETIME but instead as a STR.
I would try to use a CONVERT in your modified statement to see if you get different functionality.
It sounds like this question is mostly for your curiosity so I will add that the original statement is the standard way to accomplish what you've outlined.
This is because SELECT... INTO statements are harder to read and revise for new users and because they can lead to some unexpected functionality as you have displayed above.
So, similar to what Edward had said. The query I had posted was returning a DATETIME data type, however, at the very end of the query when it selects all information to display to the user, it got converted to CHAR somewhere in between, so I just converted it to DATETIME before converting to CHAR and splitting up the values.

Concatenating two TIME columns in SQL Server 2012

I want to concatenate two TIME columns and show as one column.
Example:
FromTime: 9:00
ToTime: 12:00
Result should be:
9:00-12:00
Generic SQL:
-- hh:mm:ss
SELECT 'result:' + CONVERT(CHAR(6), FromTime, 8) + '-' + CONVERT(CHAR(6), ToTime)
FROM yourTable
MySQL:
-- hh:mm
SELECT 'result:' + DATE_FORMAT(FromTime, '%H:%i') + '-' + DATE_FORMAT(ToTime, '%H:%i')
FROM yourTable
SQL Server:
-- hh:mm
SELECT 'result:' + convert(char(2), DATEPART(hh, FromTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, FromTime)) + '-' +
CONVERT(CHAR(2), DATEPART(hh, ToTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, ToTime))
FROM yourTable
declare #FromTime time
declare #ToTime time
set #FromTime='9:00'
set #ToTime='12:00'
select cast(#FromTime as varchar(10))+ '-' + cast(#ToTime as varchar(10)) as result
sql demo
You can use convert
select convert(VARCHAR(5),getdate(),108) + ' - ' + convert(VARCHAR(5),getdate()-1,108)

How to convert date into DD-MON-YYYY HH24:MI:SS format?

Can someone help me with SQL Date format?
The following statement
SELECT convert(VARCHAR(20),GETDATE(),113)
returns
04 Aug 2011 08:08:08.
I want the results like
Aug-04-2011 08:08:08
Thank you!
SELECT
LEFT(DATENAME(MONTH, Date), 3) + '-' +
RIGHT(100 + DAY(Date), 2) + '-' +
DATENAME(YEAR, Date) + ' ' +
CONVERT(varchar, Date, 108)
FROM (SELECT Date = GETDATE()) s
Off the top of my head, I think its:
SELECT convert(VARCHAR(20),GETDATE(),120)
EDIT:
This will work:
SELECT datename(day, GETDATE()) + '-'
+ substring(datename(month, GETDATE()),0,4) + '-'
+ datename(year, GETDATE()) + ' '
+ datename(hh, GETDATE()) + ':'
+ datename(mi, GETDATE()) + ':'
+ datename(ss, GETDATE())
SECOND EDIT:
SELECT substring(datename(month, GETDATE()),0,4) + '-'
+ datename(day, GETDATE()) + '-'
+ datename(year, GETDATE()) + ' '
+ datename(hh, GETDATE()) + ':'
+ datename(mi, GETDATE()) + ':'
+ datename(ss, GETDATE())
THIRD EDIT:
select substring(datename(month, GETDATE()),0,4) + '-'
+ right(datename(day, GETDATE())+100,2) + '-'
+ datename(year, GETDATE()) + ' '
+ right(datename(hh, GETDATE())+100,2) + ':'
+ right(datename(mi, GETDATE())+100,2) + ':'
+ right(datename(ss, GETDATE())+100,2)
The built-in convert won't allow you to format your date exactly as you desire, unfortunately.
With a little manipulation, you can get there though:
SELECT stuff(stuff(convert(VARCHAR(20),GETDATE(),113), 3, 1, '-'), 7, 1, '-')
You could put this in a UDF and call that whenever you want your date formatted in this manner.