Putting a word in the select statement - sql

I want to put the word "days" after the value, but the sql server is reacting that it cannot be converted. How can I also not select the negative integers difference.
SELECT PONo, PODeliveryDate, DATEDIFF(DAY, GETDATE(), PODeliveryDate) AS DayDiff
FROM PurchaseOrder
where POStatus='Complete' OR POStatus='Partially Completed' OR POStatus='Approved'
ORDER BY ABS( DATEDIFF( DAY, GETDATE(), PODeliveryDate ))
It gives me the difference of dates, but I cannot put the word days beside it.
I want it to look like 5 days not just 5

Try to convert the int value from DATEDIFF() to varchar and add your word
SELECT PONo
, PODeliveryDate
, CAST(DATEDIFF(DAY, GETDATE(), PODeliveryDate) as varchar(10)) + ' days' AS DayDiff
FROM PurchaseOrder
WHERE POStatus='Complete'
OR POStatus='Partially Completed'
OR POStatus='Approved'
ORDER BY ABS( DATEDIFF( DAY, GETDATE(), PODeliveryDate ))

Convert this first DATEDIFF(DAY, GETDATE(), PODeliveryDate) into VARCHAR before you concatinate.
CONVERT(VARCHAR(50), DATEDIFF(DAY, GETDATE(), PODeliveryDate)) + 'days'

In sqlserver 2012, you can use CONCAT.
To only get the positive days, you can cast getdate as date and compare with that in the WHERE statement. Then you dont need to order by ABS datediff.
The WHERE clause is easier to read and maintain by using IN instead of OR, it also makes is easier to maintain.
SELECT
CONCAT(DATEDIFF(DAY, GETDATE(), PODeliveryDate), ' days') AS DayDiff
FROM
PurchaseOrder
WHERE
POStatus in ('Complete','Partially Completed','Approved')
AND CAST(GETDATE() as date) <= PODeliveryDate
ORDER BY
PODeliveryDate

Can you try like as follow:
select CONVERT(varchar(10), GETDATE()) + ' Days'
You need to convert or cast into varchar before concate

Related

How do I convert time into an integer in SQL Server

I have a date column and a time column that are integers
I converted the date portion like this
select convert(int, convert(varchar(10), getdate(), 112))
I thought I could do the same with this query that gives the time in HH:mm:ss
SELECT CONVERT(VARCHAR(8), GETDATE(), 108)
How do I convert just the time into an integer?
This should convert your time into an integer representing seconds from midnight.
SELECT (DATEPART(hour, Col1) * 3600) + (DATEPART(minute, Col1) * 60) + DATEPART(second, Col1) as SecondsFromMidnight FROM T1;
Assuming you are looking for the "time" analogy to the "date" portion of your code which takes YYYYMMDD and turns it into an INT, you can:
start with the HH:mm:ss format given by the style number 108
remove the colons to get that string into HHmmss
then convert that to INT
For example:
SELECT REPLACE(
CONVERT(VARCHAR(8), GETDATE(), 108),
':',
''
) AS [StringVersion],
CONVERT(INT, REPLACE(
CONVERT(VARCHAR(8), GETDATE(), 108),
':',
''
)
) AS [IntVersion];
You can use the differece between midnight and the time of the day. For example, using getdate(), you can know the percentage of the time of the day with this query:
select convert(float,getdate()-convert(date,getdate()))
You can then convert this number to seconds
select convert(int,86400 * convert(float,getdate()-convert(date,getdate())))
You'll get the number of seconds from midnight
I think this is easier to understand when using with a SQL Update statement.
UPDATE dbo.MyTable
SET TIME_AS_INT = CAST(REPLACE(CAST(CONVERT(Time(0), GETDATE()) AS varchar),':','') AS INT)
To add/subtract time from the result before converting use dateadd()
SELECT CAST(REPLACE(CAST(CONVERT(Time(0), dateadd(MINUTE, 1, getdate())) AS varchar),':','') AS INT)

SQL Server: Use different date format in Select when selected date equals current date

I have a table with a column "modTime" formatted as datetime.
To fetch dates from this column I use the following line in my Select which returns a date in the format DD MMM YYYY:
CONVERT(VARCHAR(11), G.modTime, 106) AS modTime
Is there a way that I can return the date in a different format (like the following) when it matches the current date and only otherwise use the above format ? This returns the date as Today at hh:mm.
('Today at ' + CONVERT(VARCHAR(5), G.modTime, 108)) AS modTime
Both ways work when I use them separately but I couldn't find a way to combine them using CASE etc.
You can try this:
select iif(G.modTime=getdate(),('Today at ' + CONVERT(VARCHAR(5), G.modTime, 108)),CONVERT(VARCHAR(11),G.modTime, 106) ) from <table name>
Please note that IIF works only with SQL Server 2012 or later.
http://msdn.microsoft.com/en-IN/library/hh213574.aspx
For older versions, this post might help you:
SQL Server 2008 IIF statement does not seem enabled
you will not match to getdate() using equals, and you need to set getdate()'s time to midnight
select
case when G.modTime >= dateadd(day, datediff(day,0, getdate() ), 0)
then ('Today at ' + CONVERT(VARCHAR(5), G.modTime, 108))
else
CONVERT(VARCHAR(11), G.modTime, 106)
end AS modTime
from G
Above I have used: dateadd(day, datediff(day,0, getdate() )
Instead you could use: cast(getdate() as date)
both, have the effect of giving you "today" at 00:00:00

how to find datediff by using date format as ARRDAT(20140523) and DEPDAT(20140815)

Date formats are different can any one help me with different date formats
how to find datediff by using date format as ARRDAT(20140523) and DEPDAT(20140815)
The date format of the 2 values looks like yyyyMMdd which is the ISO date format.
Knowing this we can use the following:
SELECT DATEDIFF(day, ArrivalDate, DepartureDate) AS DiffInDays
FROM (
SELECT CONVERT(DATETIME, CAST(ARRDAT AS VARCHAR(8)), 112) AS ArrivalDate,
CONVERT(DATETIME, CAST(DEPDAT AS VARCHAR(8)), 112) AS DepartureDate
) AS t
CAST(expression AS VARCHAR(8)) changes the BIGINT to a VARCHAR for the CONVERT function to work.
CONVERT(DATETIME, expression, 112) specifies the format of the data is yyyyMMdd.
Try Following query:
SELECT DATEDIFF( DAY,
CAST(SUBSTRING(APPDAT,1,4)+'-'+SUBSTRING(APPDAT,5,2)+'-'+SUBSTRING(APPDAT,7,2) AS DATE),
CAST(SUBSTRING(DEPDAT,1,4)+'-'+SUBSTRING(DEPDAT,5,2)+'-'+SUBSTRING(DEPDAT,7,2) AS DATE))
FROM YourTable

Subtract one day from datetime

I have a query to fetch date diff between 2 datetime as :
SELECT DATEDIFF(DAY, #CreatedDate , GETDATE())
Ex :
SELECT DATEDIFF(DAY, '2013-03-13 00:00:00.000' , GETDATE())
I need to have a query work like this which will subtract a day from created day:
SELECT DATEDIFF(DAY, **#CreatedDate- 1** , GETDATE())
Try this
SELECT DATEDIFF(DAY, DATEADD(day, -1, '2013-03-13 00:00:00.000'), GETDATE())
OR
SELECT DATEDIFF(DAY, DATEADD(day, -1, #CreatedDate), GETDATE())
I am not certain about what precisely you are trying to do, but I think this SQL function will help you:
SELECT DATEADD(day,-1,'2013-04-01 16:25:00.250')
The above will give you 2013-03-31 16:25:00.250.
It takes you back exactly one day and works on any standard date-time or date format.
Try running this command and see if it gives you what you are looking for:
SELECT DATEADD(day,-1,#CreatedDate)
To simply subtract one day from todays date:
Select DATEADD(day,-1,GETDATE())
(original post used -7 and was incorrect)
Apparently you can subtract the number of days you want from a datetime.
SELECT GETDATE() - 1
2016-12-25 15:24:50.403
This should work.
select DATEADD(day, -1, convert(date, GETDATE()))
SELECT DATEDIFF (
DAY,
DATEDIFF(DAY, #CreatedDate, -1),
GETDATE())
Try this, may this will help you
SELECT DATEDIFF(DAY, DATEADD(DAY,-1,'2013-03-13 00:00:00.000') , GETDATE())
To be honest I just use:
select convert(nvarchar(max), GETDATE(), 112)
which gives YYYYMMDD and minus one from it.
Or more correctly
select convert(nvarchar(max), GETDATE(), 112) - 1
for yesterdays date.
Replace Getdate() with your value OrderDate
select convert(nvarchar (max),OrderDate,112)-1 AS SubtractDate FROM Orders
should do it.
You can try this.
Timestamp=2008-11-11 13:23:44.657;
SELECT DATE_SUB(OrderDate,INTERVAL 1 DAY) AS SubtractDate FROM Orders
output :2008-11-10 13:23:44.657
I hope, it will help to solve your problem.

Get Hours and Minutes (HH:MM) from date

I want to get only hh:mm from date.
How I can get this?
I have tried this :
CONVERT(VARCHAR(8), getdate(), 108)
Just use the first 5 characters...?
SELECT CONVERT(VARCHAR(5),getdate(),108)
You can easily use Format() function instead of all the casting for sql 2012 and above only
SELECT FORMAT(GETDATE(),'hh:mm')
This is by far the best way to do the required conversion.
Another method using DATEPART built-in function:
SELECT cast(DATEPART(hour, GETDATE()) as varchar) + ':' + cast(DATEPART(minute, GETDATE()) as varchar)
If you want to display 24 hours format use:
SELECT FORMAT(GETDATE(),'HH:mm')
and to display 12 hours format use:
SELECT FORMAT(GETDATE(),'hh:mm')
Following code shows current hour and minutes in 'Hour:Minutes' column for us.
SELECT CONVERT(VARCHAR(5), GETDATE(), 108) +
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
ELSE ' AM'
END) 'Hour:Minutes'
or
SELECT Format(GETDATE(), 'hh:mm') +
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
ELSE ' AM'
END) 'Hour:Minutes'
The following works on 2008R2+ to produce 'HH:MM':
select
case
when len(replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')) = 4
then '0'+ replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')
else replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','') end as [Time]
You can cast datetime to time
select CAST(GETDATE() as time)
If you want a hh:mm format
select cast(CAST(GETDATE() as time) as varchar(5))
Here is syntax for showing hours and minutes for a field coming out of a SELECT statement. In this example, the SQL field is named "UpdatedOnAt" and is a DateTime. Tested with MS SQL 2014.
SELECT Format(UpdatedOnAt ,'hh:mm') as UpdatedOnAt from MyTable
I like the format that shows the day of the week as a 3-letter abbreviation, and includes the seconds:
SELECT Format(UpdatedOnAt ,'ddd hh:mm:ss') as UpdatedOnAt from MyTable
The "as UpdatedOnAt" suffix is optional. It gives you a column heading equal tot he field you were selecting to begin with.
TO_CHAR(SYSDATE, 'HH')
I used this to get the current hour in apex PL/SQL