How to truncate a date to the first day of the month in Snowflake? - sql

I am not able to find any good conversion code to get 2014-02-17 into 2014-02-01 without having to use concatenation and a ton of formatting.
I wonder if someone can help me find a good command to achieve this. Thanks!

Snowflake supports date_trunc() for datatypes DATE, TIME, and TIMESTAMP:
SELECT DATE_TRUNC(month, CURRENT_DATE()) AS first_day_of_month;

This is another option:
SELECT TRUNCTIMESTAMPTOMONTH(CURRENT_DATE());

Sounds like you're working with strings. If that's the case and they'll always be in the format 'yyyy-MM-dd', you can just take the first 8 characters and add '01':
left(MyStringValue, 8) + '01'
If you're working with date fields, I like the trick of doing datediff to get the months from 0, then use dateadd with 0 to get back to the first of the month:
dateadd(month, datediff(month, 0, MyDateValue), 0)

Related

How can I get a timestamp or a data using datediff function?

I am reading some sql code from somebody where DATEDIFF function should return a timestamp. Here is the code:
DATEDIFF(wk, 6, GETDATE())
I do not see any sql syntax like this anywhere. Can anyone explain how this code should return date or timestamp?
You use DATEDIFF() function to get the difference between two dates, and it returns integer value. Looking at what you are trying to do, it seems you want to add 6 weeks to the current date. In such cases you should use DATEADD() function instead.
Below are some some examples:
SELECT DATEADD(ww, 6, GETDATE()),
DATEDIFF(ww,'6/1/2022', getdate())

How to use SQL - DATEDIFF function

need quick help here.
I'm coming up with a column to track how many days a event is back-logged.
So I'm using this syntax which is working
DATEDIFF(DAY, GETDATE(), ESI.EventStatusDate) AS BackloggedDays
However, the modification I want to use is, the event status date + 3 days. Any ideas how i would add that to this syntax. Thanks
I think you need DATEADD() :
DATEDIFF(DAY, GETDATE(), DATEADD(DAY, 3, ESI.EventStatusDate)) AS BackloggedDays

how to remove time from datetime

The field DATE in the database has the following format:
2012-11-12 00:00:00
I would like to remove the time from the date and return the date like this:
11/12/2012
First thing's first, if your dates are in varchar format change that, store dates as dates it will save you a lot of headaches and it is something that is best done sooner rather than later. The problem will only get worse.
Secondly, once you have a date DO NOT convert the date to a varchar! Keep it in date format and use formatting on the application side to get the required date format.
There are various methods to do this depending on your DBMS:
SQL-Server 2008 and later:
SELECT CAST(CURRENT_TIMESTAMP AS DATE)
SQL-Server 2005 and Earlier
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SQLite
SELECT DATE(NOW())
Oracle
SELECT TRUNC(CURRENT_TIMESTAMP)
Postgresql
SELECT CURRENT_TIMESTAMP::DATE
If you need to use culture specific formatting in your report you can either explicitly state the format of the receiving text box (e.g. dd/MM/yyyy), or you can set the language so that it shows the relevant date format for that language.
Either way this is much better handled outside of SQL as converting to varchar within SQL will impact any sorting you may do in your report.
If you cannot/will not change the datatype to DATETIME, then still convert it to a date within SQL (e.g. CONVERT(DATETIME, yourField)) before sending to report services and handle it as described above.
just use, (in TSQL)
SELECT convert(varchar, columnName, 101)
in MySQL
SELECT DATE_FORMAT(columnName, '%m/%d/%Y')
I found this method to be quite useful. However it will convert your date/time format to just date but never the less it does the job for what I need it for. (I just needed to display the date on a report, the time was irrelevant).
CAST(start_date AS DATE)
UPDATE
(Bear in mind I'm a trainee ;))
I figured an easier way to do this IF YOU'RE USING SSRS.
It's easier to actually change the textbox properties where the field is located in the report. Right click field>Number>Date and select the appropriate format!
SELECT DATE('2012-11-12 00:00:00');
returns
2012-11-12
Personally, I'd return the full, native datetime value and format this in the client code.
That way, you can use the user's locale setting to give the correct meaning to that user.
"11/12" is ambiguous. Is it:
12th November
11th December
For more info refer this: SQL Server Date Formats
[MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 101) from tbl
[DD/MM/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 103) from tbl
Live Demo
TSQL
SELECT CONVERT(DATE, GETDATE()) // 2019-09-19
SELECT CAST(GETDATE() AS DATE) // 2019-09-19
SELECT CONVERT(VARCHAR, GETDATE(), 23) // 2019-09-19
In mysql at least, you can use DATE(theDate).
You may try the following:
SELECT CONVERT(VARCHAR(10),yourdate,101);
or this:
select cast(floor(cast(urdate as float)) as datetime);
Use this SQL:
SELECT DATE_FORMAT(date_column_here,'%d/%m/%Y') FROM table_name;

Convert DateTime SQL

I need to get only date and hours from datetime field. How do I do that?
My query:
select ImpFile, convert(nvarchar,ImpDate,21) as ImpDate
from nol_artikula_izmaina
The output from this query:
What I need is that it only shows me the date and the hour for example the field ImpDate should look like this: 2012-05-11 14:00:00:000 and 2012-05-11 16:00:00:000
Is that possible?
This works by getting the number of [whole] hours between "date 0" and ImpDate, then adding that to "date 0".
It's very similar in principle to removing the time (which gets the number of [whole] days) and can be used to "truncate" to any date part (though for seconds or smaller, you may need to use a different "floor" date than 0 to avoid an overflow).
SELECT ImpFile, DATEADD(hh, DATEDIFF(hh, 0, ImpDate), 0)
FROM nol_artikula_izmaina
Select using datepart.
http://msdn.microsoft.com/en-us/library/aa258265(v=sql.80).aspx

How do you extract just date from datetime in T-Sql?

I am running a select against a datetime column in SQL Server 2005. I can select only the date from this datetime column?
Best way is:
SELECT DATEADD(day, DATEDIFF(Day, 0, #ADate), 0)
This is because internally, SQL Server stores all dates as two integers, of which the first one is the ****number of days*** since 1 Jan 1900. (the second one is the time portion, stored as the number of seconds since Midnight. (seconds for SmallDateTimes, or milleseconds for DateTimes)
Using the above expression is better because it avoids all conversions, directly reading and accessing that first integer in a dates internal representation without having to perform any processing... the two zeroes in the above expression (which represent 1 Jan 1900), are also directly utilized w/o processing or conversion, because they match the SQL server internal representation of the date 1 jan 1900 exactly as presented (as an integer)..
*NOTE. Actually, the number of date boundaries (midnights) you have to cross to get from the one date to the other.
Yes, by using the convert function. For example:
select getdate(), convert(varchar(10),getdate(),120)
RESULTS:
----------------------- ----------
2010-05-21 13:43:23.117 2010-05-21
You can use the functions:
day(date)
month(date)
year(date)
Also the Datepart() function might be of some use:
http://msdn.microsoft.com/en-us/library/ms174420(SQL.90).aspx
DECLARE #dToday DATETIME
SET #dToday = CONVERT(nvarchar(20), GETDATE(), 101)
SELECT #dToday AS Today
This returns today's date at 12:00am : '2010-05-21 00:00:00.000'
Then you can use the #dToday variable in a query as needed
CONVERT (date, GETUTCDATE())
CONVERT (date, GETDATE())
CONVERT (date, '2022-18-01')
I don't know why the others recommend it with varchar(x) tbh.
https://learn.microsoft.com/de-de/sql/t-sql/functions/getdate-transact-sql