SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) –First day of previous month SELECT DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) –Last Day of previous month
Tag: last
How to get the first and last date of the current year in SQL
SELECT DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS LastDayOfYear, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0) AS FirstOfNextYear, DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)) AS LastTimeOfYear Thanks to Jamie F for the answer.
SQL Tip: “@@IDENTITY” should not be used
@@IDENTITY returns the last identity column value created on a connection, regardless of the scope. That means it could return the last identity value you produced, or it could return a value generated by a user-defined function or trigger, possibly one fired because of your insert. In order to access the last identity value created […]
How to get triggers create and update date in SQL
SELECT o.name as [Trigger Name], CASE WHEN o.type = ‘TR’ THEN ‘SQL DML Trigger’ WHEN o.type = ‘TA’ THEN ‘DML Assembly Trigger’ END AS [Trigger Type], sc.name AS [Schema_Name], OBJECT_NAME(parent_object_id) as [Table Name], o.create_date [Trigger Create Date], o.modify_date [Trigger Modified Date] FROM sys.objects o INNER JOIN sys.schemas sc ON o.schema_id = sc.schema_id WHERE (type = […]
IndexOf() and LastIndexOf() in C#
The IndexOf() method returns the index number of the first matched character whereas the LastIndexOf() method returns the index number of the last matched character. using System; public class StringExample { public static void Main(string[] args) { string s1 = “Hello C#”; int first = s1.IndexOf(‘l’); int last = s1.LastIndexOf(‘l’); Console.WriteLine(first); Console.WriteLine(last); } } Output: […]
Get the First and Last Word from a String or Sentence in SQL
You can use the query below: DECLARE @Sentence VARCHAR(MAX) = ‘The quick brown fox jumps over the lazy dog’ DECLARE @first_space_index int = CHARINDEX(‘ ‘, @Sentence) – 1 DECLARE @last_space_index int = CHARINDEX(‘ ‘, REVERSE(@Sentence)) – 1 IF @first_space_index < 0 SELECT @Sentence AS [First Word], @Sentence AS [Last Word] ELSE SELECT SUBSTRING(@Sentence, 1, @first_space_index) […]