SQL Server equivalent of Trunc - sql-server

TRUNC(LD.FECHA_CREA)
I need convert it to SQL Server

You can use following condition in your query :
CONVERT(DATETIME, CONVERT(DATE, LD.FECHA_CREA)) = CONVERT(DATETIME, CONVERT(DATE, GETDATE()))

You could use this equivalent in SQL Server:
CONVERT(DATETIME, CONVERT(DATE, LD.FECHA_CREA));
Check: http://www.sqlines.com/oracle-to-sql-server/trunc_datetime

Related

Migrate Oracle view to SQL Server

I had used this as Where Condition in Oracle
(ProjectDate between trunc(sysdate-1)+15/24 and trunc(sysdate)+8/24)
I was trying to convert view to SQL Server and I used to try
ProjectDate between (GetDate()-1)+15/24 and (GetDate())+8/24
I am not sure if using right function or not?
You need to use following expression:
ProjectDate between
DATEADD(HOUR,15,CONVERT(DATETIME, CONVERT(DATE, GETDATE())) - 1)
and DATEADD(HOUR,8,CONVERT(DATETIME, CONVERT(DATE, GETDATE())))
DATEADD: The DATEADD() function adds a time/date interval to date and then returns the date.
CONVERT(DATETIME, CONVERT(DATE, GETDATE())) returns the same as TRUNC(SYSDATE) in oracle

Format date as 'DD Month YYYY'

Just have a mssql query that has a date in the format:
'2016-03-22 00:00:00.000'
I need to format it as:
'22 March 2016'
I'm using SQL Server 2012. I've tried googling and the usual 106, 112 codes don't seem to work.
Is there a specific code format I can use?
Try the FORMAT function:
SELECT FORMAT(GETDATE(), 'D', 'en-gb')
If your version does not support the FORMAT function, you can do it by concatenating the date parts:
SELECT
RIGHT('00' + CAST(DATEPART(DAY, GETDATE()) AS VARCHAR(2)), 2) + ' ' +
DATENAME(MONTH, GETDATE()) + ' ' +
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4))
Use Format Function in SQL Server 2012
SELECT FORMAT(GETDATE(),'dd-MMMM-yyyy')
See Image for your datatype ref
Try this query:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, '2016-03-22 00:00:00.000'), 106)
You need to convert into DATETIME then you will get your desired output
More Datetime Sql formate
SQLFiddle
This may help you
Try Convert function for SQL server:
select convert(varchar(11),cast('2016-03-22 00:00:00.000' as datetime), 106)
How to change day, month, year in PHP:
echo $purchase_date=date('F-Y',strtotime('2022-03-22 00:00:00.000'));
Answer:
March-2022

How can I convert SQL Server date to custom formats

SELECT GETDATE() AS CurrentDateTime
2015-08-30 19:22:24.830
I need this output (in SQL Server)
2015-08-30 19
2015-08
SELECT
CONVERT(VARCHAR(13), GETDATE(), 120) as "YYYY-MM-DD hh 1"
, CONVERT(VARCHAR(7), GETDATE(), 120) as "YYYY-MM 1"
, FORMAT(getdate(),'yyyy-MM-dd hh') as "YYYY-MM-DD hh 2"
, FORMAT(getdate(),'yyyy-MM') as "YYYY-MM 2"
;
For any version of SQL Server you can use CONVERT(VARCHAR(),datecol,styleno)
just learn the relevant style numbers and adjust the length of the varchar
For SQL Server 2012 onward it's easier with the FORMAT() function
Try this
SELECT CONVERT(VARCHAR(13), GETDATE(), 120)
SELECT CONVERT(VARCHAR(7), GETDATE(), 120)

sql select datetime stamp as m/d/y only. (without h m s)

When I select two rows with a DATETEIME stamp, I only want the m/d/y data and nothing after that.
It has to changed during the select (not afterwards).
To remove the time you just need to do the following Assuming SQL Server
Pre SQL 2008
select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) yourdate
FROM yourtable
SQL 2008
select CAST(getdate() as date) yourdate
FROM yourtable
See Most efficient way in SQL Server to get date from date+time?
or
Best approach to remove time part of datetime in SQL Server
Is this for export? If you only want the text you can use a variety of coversion formats available on MSDN.
select convert(varchar, getdate(), 101)
-- output: 07/05/2011
Otherwise, if you're using sql 2008, you can just cast the datetime to date:
select cast(getdate() as date)

Need only Date from DateTime

I have a variable of DateTime type in SQL.
Just need to have Date part of it.
please Help?
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
The result is: “2009-07-14 00:00:00.000”
Edit: guess the next variant is more common:
SELECT DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
because of the day pattern can be easily changed to week or month pattern. It is very useful when the GROUP BY clause should group by week of month (reports).
This has been asked and answered before on Stack Overflow. In fact, it's been asked over and over:
Most efficient way in MS SQL to get date from date+time?
Best way to check for current date in where clause of sql query.
SQL Drop Time in DateTime
MS SQL Date Only Without Time
How to return the date part only from a SQL Server datetime datatype
Found this using Google
SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDATE())))
If you just need a varchar representation of the date, you can use the convert function, e.g.
select convert(varchar, getDate(), 102) /* 2009.07.14 */
If you need a datetime (midnight on the given date), you can just convert it back.
select convert(datetime, convert(varchar, getDate(), 102), 102)
-- Sneaky CAST/DATEDIFF trick strips off the time to get just the day (midnight)!
CAST(DATEDIFF(d,0,DateField) AS DATETIME) AS DayField
SQL Server 2008 has a date datatype that stores just the date, if you are inthis version, perhaps this would be a better datat type for you to use. Be warned though, Date doesn't work exactly like datetime for data manipulation.
SELECT DATEADD(day, DATEDIFF(day, '19900101', CURRENT_TIMESTAMP), '19900101')
A very useful article:
"The purpose of this article is to explain how the datetime types work in SQL Server, including common pitfalls and general recommendations."The ultimate guide to the datetime datatypes
Note that converting to varchar and back (convert(datetime, convert(varchar, getDate(), 102), 102)) is much slower.
If you want the format 'MM/DD/YY', use "CONVERT(varchar, #datetimevalue, 1) to display just the date. If you need it in datetime format, use "CONVERT(datetime, CONVERT(varchar, #datetimevalue, 1))".
I created an entry in my SQL blog about how to retrieve and display all possible formats of the CONVERT(varchar, ..) function:
http://jessesql.blogspot.com/2009/04/converting-datetime-values-to-varchar.html
A tip:
If you find yourself doing this often, you can create a scalar User Defined Function containing the time-stripping logic of your choice.
Be warned: SQL Server 2000 has some painful bugs involving UDF's in ON clauses.
datepart(day, datetimevalue)

Resources