This question already has answers here:
How to subtract 30 days from the current date using SQL Server
(4 answers)
Closed 5 years ago.
I want to subtract 119 days from given date,
i tried dateadd function but it is not working.
select dateadd(dd,-119,'09/05/2017')
DATEADD is correct - I don't know how you have used it, but this would be the way:
SELECT DATEADD(day, -119, #givenDate)
Just saying "it is not working" doesn't help. From what I gather, it does work, but you don't know the syntax. Try this -
SELECT dateadd(d,-119,getdate())
Related
This question already has answers here:
Possible to store value of one select column and use it for the next one?
(4 answers)
How to use a calculated column to calculate another column in the same query using a subquery
(2 answers)
SQL Server Reference a Calculated Column
(6 answers)
Using CASE on a Calculated Column in SQL Server 2012
(2 answers)
Closed 6 months ago.
I am trying to calculate the date difference and keep getting a column name error. I am new to SQL and learning from books and YouTube. Any assistance would be appreciated. I commented out the code not working
declare #rpDT datetime
set #rpDT = getdate()
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,isnull([visDischargeDT],#rpDT)as disDT
,datediff(day,[visAdmitDT],disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
SQL Server and most of the RDBMS do not allow to reference column at the same level. The closest equivalent to achieve similar effect is CROSS APPLY:
declare #rpDT datetime = getdate();
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,s.disDT
,datediff(day,[visAdmitDT],s.disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
CROSS APPLY (SELECT COALESCE([visDischargeDT],#rpDT)) AS s(disDT);
This question already has answers here:
converting Epoch timestamp to sql server(human readable format)
(2 answers)
Closed 5 years ago.
I know how to convert an epoch timestamp to a SQL server timestamp using DATEADD, but I am interested in a function that does the reverse of that.
You can do it using DATEDIFF function like below :
select DATEDIFF(s, '1970-01-01 00:00:00', '2017-11-20 23:12:02.000') as EpochTimeStamp
Output :
EpochTimeStamp
--------------
1511219522
You know already how can we get back the original date :
SELECT DATEADD(ss, 1511219522, '19700101') as OriginalDate
OriginalDate
-----------------------
2017-11-20 23:12:02.000
This question already has answers here:
Isoweek in SQL Server 2005
(4 answers)
Closed 7 years ago.
The code returns:
set datefirst 1
select datepart(wk,'2016-01-01') - 1
but
set datefirst 1
select datepart(wk,'2015-12-31')
returns..53 :/
But in fact - this is the same week. There is more days belonging to 2015 in this week, so it should be "53" or "1" (the same value) for any dates in this particular week. Is this possible to avieve this without building dedicated procedure to analyse date and adjust returned value ?
I use SQL Server 2005
You probably want iso_week:
set datefirst 1
select datepart(iso_week,'2015-12-31') --53
select datepart(iso_week,'2016-01-01') --53
LiveDemo
EDIT:
Looks like that iso_week is supported from SQL Server 2008+.
Is this possible to avieve this without building dedicated procedure
to analyse date and adjust returned value ?
Probably you need to write custom code to calculate it.
This question already has answers here:
getting all chars before space in SQL SERVER
(2 answers)
Closed 8 years ago.
I want to extract a certain part of a row. if there is something like this ..." abcd - productA", " zas1234 - productC", how do I extract everything before the dash(-)?
I tried a left(column name,'-') it didnt work. I am on SQL Server 2012.
The column from which I am trying to extract is a varchar column.
It would be great if someone could help me.
Try this,
select SUBSTRING(column_name, 1, CHARINDEX('-', column_name)-1) from table_name;
DECLARE #varStr VARCHAR(MAX)
SET #varStr='abcd - productA'
SELECT CHARINDEX('-',#varStr)
SELECT LEFT(#varStr,CHARINDEX('-',#varStr)-1)
This question already has answers here:
Omitting the Milliseconds in a Date
(7 answers)
Closed 9 years ago.
I have a DateTimeOffset column in my table named BarcodeTime. A sample value looks like this:
2013-01-20 03:34:36.8930000 -05:00
I'd like to display it as follows:
2013-01-20 03:34:36
I thought the following should do it:
CONVERT(datetime2, CONVERT(datetime2, BarcodeTime, 0), 120) as BarcodeTime
But this isn't working. What I get is this: 2013-01-20 03:34:36.8930000
Can someone show me the correct way?
Set the number of decimals on the datetime2 value to 0.
CONVERT(datetime2(0), BarcodeTime)
Maybe a bit ugly, but if you just want to change the display output you could try:
SELECT LEFT(BarcodeTime, 19)