Convert datetime to date using a function - sql-server

I have one column in my database that I need to convert from datetime to the date data type using a SQL Server function. I can't figure out how to get the correct syntax. Could I get some help with the syntax so IntelliSense will stop yelling at me and I can get the query to run?
CREATE FUNCTION fChangeDateFormat (#date01 date)
RETURNS DATE
AS
RETURN(
SELECT
Convert(DateTime, OrderDate, 101)
FROM
Orders
WHERE
OrderDate = #date01
)

You can use convert as,
select CONVERT(date,#date01,101) as dd from Orders
If you are using SQl server 2012 + use FORMAT,
'd' is to get the short date format.
SELECT FORMAT(#date01,'d','en-US') as dd from Orders

Related

convert sql server AlphaToDate4 to oracle date which has 5 numbers

Hi good day I have the following code I am trying to convert from sql server to oracle, It works perfectly on sql but not on oracle. How can I convert the ff?
This is the code from sql
Select CONVERT(VARCHAR, RLL.dbo.AlphaToDate4(A.DATE_R), 103) CaptureDate
from Employees
This is the format of Date_R (78684)
I am trying to write the following code in oracle but gives me an error
select TO_DATE((A.DATE_R),'DD/MM/YYYY') CAPTUREDATE
Assuming that your number represents the number of days from 31/12/1799 (are you sure?), you may need to add the column value to this date, that is
select date '1799-12-31' + date_R
If you need a formatted string:
select to_char( date '1799-12-31' + date_R, 'dd/mm/yyyy')

Get date from int (YYYYMMDD)

By doing a mistake a few months ago I got myself in a situation, where I have to find a workaround for the following problem:
I am saving dates as integer in a common format inside my SQL Server database (YYYYMMDD). I want to have my select statement giving me the integer in a format, that looks like a date (or even is in a true date type) to the user, so I can use the received datatable directly as datasource for my DataGridView.
I do not want to use clientside formatting
Let's call the table myTable and the integer/date column myIntDate
myIntDate can be NULL
sample myIntDate-value : 20160803 for the 3rd of August 2016
Select cast(cast(20160729 as varchar(10)) as date)
Returns
2016-07-29
Or
Select cast(left(20160729,8) as date)

Fetch Previous day record using sql server using Date field

I want to fetch previous day record from the table using a date field in Sql Server.
However I'm using the below sql statements but it's not giving any record.
TDTE = CAST(DATEADD(DD,-1,CURRNT_TIMESTAMP) AS DECIMAL(8,0))
OR
TDTE=CAST(DATEADD(DD,-1,GETDATE())AS DECIMAL(8,0))
where TDTE column is in YYYYMMDD format.
Remove the Cast function Dateadd returns Date not a Integer value. Try this.
Where TDTE = Cast(DATEADD(DD,-1,GETDATE()) as Date)
Try this.
Where TDTE = CONVERT(VARCHAR(10), DATEADD(DAY,-1,GETDATE()), 112)
OR(in decimal format)
Where TDTE = CAST(CONVERT(VARCHAR(10), DATEADD(DAY,-1,GETDATE()), 112)AS DECIMAL(18,0))

Convert a SQL Server datetime to a shorter date format

I have a datetime column in SQL Server that gives me data like this 10/27/2010 12:57:49 pm and I want to query this column but just have SQL Server return the day month and year - eg. 2010 10 27 or something like that.
What are the functions I should be researching?
Should I be trying to convert to another date data type? Or simply convert it to a string?
Have a look at CONVERT. The 3rd parameter is the date time style you want to convert to.
e.g.
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) -- dd/MM/yyyy format
Try this:
print cast(getdate() as date )
If you need the result in a date format you can use:
Select Convert(DateTime, Convert(VarChar, GetDate(), 101))
In addition to CAST and CONVERT, if you are using Sql Server 2008, you can convert to a date type (or use that type to start with), and then optionally convert again to a varchar:
declare #myDate date
set #myDate = getdate()
print cast(#myDate as varchar(10))
output:
2012-01-17
If you have a datetime field that gives the results like this 2018-03-30 08:43:28.177
Proposed: and you want to change the datetime to date to appear like 2018-03-30
cast(YourDateField as Date)
With SQL Server 2005, I would use this:
select replace(convert(char(10),getdate(),102),'.',' ')
Results: 2015 03 05
The shortest date format of mm/dd/yy can be obtained with:
Select Convert(varchar(8),getdate(),1)
Just add date keyword.
E.g. select date(orderdate),count(1) from orders where orderdate > '2014-10-01' group by date(orderdate);
orderdate is in date time.
This query will show the orders for that date rather than datetime.
Date keyword applied on a datetime column will change it to short date.
For any versions of SQL Server: dateadd(dd, datediff(dd, 0, getdate()), 0)
The original DateTime field : [_Date_Time]
The converted to Shortdate : 'Short_Date'
CONVERT(date, [_Date_Time]) AS 'Short_Date'

SQL Server 2008 script - how to to acquire current date from system and store it into a date column

Hey fellas, I'm having difficulty obtaining only the date from the system and inserting it into a column, is there a built-in function that can acquire it?
On top of that, how do I add years to the current date?
I know I'm pushing it right now, but I'm also wondering what's the format for the date datatype?
Because sometimes I'd like to manually insert values into a column with that type in mind.
Any help would greatly be appreciated.
Thanks.
To get date only (SQL Server 2008 only) CAST to date type
SELECT CAST(GETDATE() AS date)
To add years, use DATEADD
SELECT DATEADD(year, 2, CAST(GETDATE() AS date))
Formats: use yyyymmdd or ISO yyyy-mm-dd (for newer datetime types) for safety.
Read this for everything about date+time in SQL Server
To add a year to the current date, look at the dateadd() function.
To just get the date from sql w/o the time, you can do this:
DECLARE #Date DATETIME
SELECT #Date = CONVERT(VARCHAR, GETDATE(), 101)
SELECT #Date
Sql will implicity convert the VARCHAR back to DATETIME. Look up the CONVERT function in BOL and it will give you all kinds of different styles for the 3rd parameter.
Bender

Resources