Combine two DateTimes - sql

In SQL I have the following code fragment :
DECLARE
#DayPart as datetime,
#TimePart as datetime
SET #DayPart='2012-01-10 00:00:00.000'
SET #TimePart='2012-08-30 15:41:10.403'
Now I Need :
'2012-01-10 15:41:10.403'
How can I get it?

SELECT CONVERT(VARCHAR(10),#DayPart,111) + ' ' +
CONVERT(VARCHAR(10),#TimePart,108);

You should get #DayPart in 'yyyy-mm-dd' format and #TimePart in 'HH:MI:SS:MMM(24H)' format and concatenate two strings.
Try this
SELECT
CONVERT(char(10), #DayPart,126) + ' ' +
CONVERT(VARCHAR(12), #TimePart, 114)
More on SQL Server date formatting
SQL Server Date Formats

SQL2K8;
select #DayPart + cast(#TimePart as time)

SELECT REPLACE(CONVERT(VARCHAR(10),#DayPart,102),'.','-') + ' ' +
CONVERT(VARCHAR(10),#TimePart,108);
Other Date Formatting
But if you are using SQL Server 2008+
SELECT CONVERT(date, #DayPart) + ' ' + CONVERT(time, #TimePart)

Related

How to concatenate declare date variables

How do I concatenate declare date variables so they are in one column? I need to show the dates as between dates. When I run the following I get an error message.
Declare #startdate date = '20180101'
Declare #enddate date = '20180731'
SELECT
'Dates' = #startdate+' - '+#enddate
FROM TABLE
Error Message:
The data types date and varchar are incompatible in the add operator.
Convert them to strings before concatenating them. For the default format on your system:
select dates = convert(varchar(255), #startdate) + ' - ' + convert(varchar(255), #enddate)
To specifically convert to YYYYMMDD use format 112:
select dates = convert(varchar(255), #startdate, 112) + ' - ' + convert(varchar(255), #enddate, 112)
You can use concat() :
SELECT CONCAT(#startdate, ' - ', #enddate) AS Dates
FROM TABLE;

Concatenate Two dates and their times in SQL Server 2008

I want to concatenate two dates with their times like below in SQL Server 2008. Something like this:
2015-09-09 08:30 - 2015-09-09 09:30
I tried this method but didn't work and I used casting as well.
CONVERT(DATETIME, CONVERT(CHAR(8), S.StartTime, 112)+ '-' + CONVERT(CHAR(8), S.endtime, 108)) AS 'OccupiedTime'
it is showing result like this
2015-09-09 09:30:00:000
CONVERT(CHAR(16), s.StartTime, 120) + '-' +
CONVERT(CHAR(16), s.EndTime, 120) AS OccupiedTime
You need 2 parts of date - date only + time. You can have 2 strings and concatenate them:
SELECT
REPLACE(CONVERT(VARCHAR(50),s.StartTime,103),'/','-') + ' ' +
CONVERT(VARCHAR(5),s.StartTime,114) + ' - ' +
REPLACE(CONVERT(VARCHAR(50),s.EndTime,103),'/','-') + ' ' +
CONVERT(VARCHAR(5),s.EndTime,114) AS OccupiedDateTime
You can make quick check how it looks using:
SELECT
REPLACE(CONVERT(VARCHAR(50),GETDATE(),103),'/','-') + ' ' +
CONVERT(VARCHAR(5),GETDATE(),114) + ' - ' +
REPLACE(CONVERT(VARCHAR(50),GETDATE(),103),'/','-') + ' ' +
CONVERT(VARCHAR(5),GETDATE(),114) AS OccupiedDateTime
You can use SQL Convert function and the conversion style parameter together as follows
declare #StartTime datetime = getdate()
declare #EndTime datetime = getdate()
select convert(varchar(16), #StartTime, 120) + ' - ' + convert(varchar(16), #EndTime, 120)
If you check the code, I used varchar(16) which removes unwanted milllisecond information after conversion.
For more on SQL Server Convert datetime function on action please refer to given tutorial

how to get today's date in sql with specific format

I currently have this format in my table:
2015-03-19 10:33:16.983
but I would like to convert it into this format:
3/18/2015 12:00:00 AM.
How can I get that format?
select myDate from myTable
You are looking for the Convert() function.
Something like
SELECT CONVERT(varchar(10),GETDATE(),3) + ' ' + CONVERT(varchar(15),CAST(getdate() AS TIME),100)
You can change 3 as per your local. 3 here will mean it in DD/MM/YYYY format i.e, British and French local.
Copy and paste this and alter as you need:
DECLARE #StartTimestamp datetime
SET #StartTimestamp = CAST((CONVERT(varchar(11), DATEADD(DAY, -1, GETUTCDATE()), 106)) AS datetime)
SELECT CONVERT(varchar, #StartTimestamp, 103) + CONVERT(varchar, #StartTimestamp, 108)
Use Convert
SELECT CONVERT(VARCHAR(25), myDate, 101) + ' ' + CONVERT(VARCHAR(25), myDate, 108) + ' ' + RIGHT(CONVERT(VARCHAR(19),myDate),2)
FROM myTable
This would give you the exact output you requested
mm/dd/yyyy hh:mi:ss AM
(Or PM depending on time)
you need to use two converts with a cast and a right function:
SELECT CONVERT(VARCHAR(10), getdate(), 101) +
' ' + CONVERT(VARCHAR(15), getdate(), 108) +
' ' + RIGHT(CONVERT(VARCHAR(20),getdate()),2)
OUTPUT: 03/19/2015 18:44:19 PM

SQL Server Query Date convert Issue

I want to add where condition in my query like 2012-10-05 00:29:06.338 <= Today'sdatetime however I am getting the issue.
Please help me to sort out this issue.
My query is given below :
Select * from table1
where
CONVERT(varchar(12),'2012-10-05 00:29:06.338', 106) + ' ' + CONVERT(varchar(12),'2012-10-05 00:29:06.338', 108) <= CONVERT(varchar(12), GETDATE(),106) + ' 23:59:00.000'
You can't do date comparisons by converting both values to a varchar. You need to convert the values to datetime and compare.
If you have a column in your database named col1 that is a datetime, you can compare this directly to GETDATE().
For example:
select * from table1 where col1 > GETDATE()
If you really need to compare a date with a string you can explicitly convert to datetime:
Select *
from table1
where CONVERT(DATETIME,'2012-10-05 00:29:06.338') <= GETDATE()
OR you can let SQL Server implicitly do the conversion for you:
Select *
from table1
where '2012-10-05 00:29:06.338' <= GETDATE()
You're trying to evaluate a string as a date:
Select '8'
where
CONVERT(DATETIME,CONVERT(varchar,Col1, 101))
+ ' ' + CONVERT(DATETIME,CONVERT(varchar,Col1,108))
<= CONVERT(DATETIME,CONVERT(varchar, GETDATE(),101))+' ' + CONVERT(DATETIME,CONVERT(varchar,'23:59:00.000', 108))

Date Format conversion in SQL

I want to convert date format from 01/09 to January 2009 , 09/03 to September 2003 etc. Is this possible in SQL? Please let me know if there is a API for the same.
if you have a DateTime column in your table, it's possible
SELECT DATENAME(MM, YOUR_DATE_COLUMN) + ' ' + CAST(YEAR(YOUR_DATE_COLUMN) AS VARCHAR(4)) AS [Month YYYY]
http://www.sql-server-helper.com/tips/date-formats.aspx
You should look here.
It's rather simple.
You should first convert it to a datetime. Then you can easily apply any formating when you read it later.
declare #d varchar(10);
set #d = '01/09'
select
--cast(#d as datetime) as d1, --syntax error converting char string
cast('20' + right(#d, 2) + '-' + left(#d, 2) + '-01' as datetime) as d2
then convert it to mmm yyyy using rm's answer
select datename(month, GETDATE()) + ' '+ substring(convert(varchar, GETDATE(), 100),8,4)