SQL Server String To Date
SQL Server String To Date
SQL Server String To Date
-- 2014-02-28
-- 2016-02-29
-- 2016-03-31
------------- SQL Server Date Only from DATETIME column - get date only
-- T-SQL just date - truncate time from datetime - remove time part
-----------DECLARE @Now datetime = CURRENT_TIMESTAMP -- getdate()
SELECT DateAndTime
= @Now
-- Date portion and Time portion
,DateString
= REPLACE(LEFT(CONVERT (varchar, @Now, 112),
10),' ','-')
,[Date]
= CONVERT(DATE, @Now) -- SQL Server 2008
and on - date part
,Midnight1
= dateadd(day, datediff(day,0, @Now), 0)
,Midnight2
= CONVERT(DATETIME,CONVERT(int, @Now))
,Midnight3
= CONVERT(DATETIME,CONVERT(BIGINT,@Now) &
(POWER(Convert(bigint,2),32)-1))
/* DateAndTime
DateString Date Midnight1
Midnight2
Midnight3
2010-11-02 08:00:33.657 20101102
2010-11-02 2010-11-02
00:00:00.000 2010-11-02 00:00:00.000
2010-11-02 00:00:00.000 */
------------
PRINT CONVERT(datetime,'2012/07/10',111)
-- Jul 10 2012
12:00AM
PRINT CONVERT(datetime,'20120710', 112)
-- Jul 10 2012
12:00AM
------------- UNIX to SQL Server datetime conversion
declare @UNIX bigint = 1477216861;
select dateadd(ss,@UNIX,'19700101'); -- 2016-10-23 10:01:01.000
------------- String to date conversion - sql date yyyy mm dd - sql date
formatting
-- SQL Server cast string to date - sql convert date to datetime
SELECT [Date] = CAST (@DateValue AS datetime)
-- 2012-07-18 00:00:00.000
-- SQL convert string date to different style - sql date string
formatting
SELECT CONVERT(varchar, CONVERT(datetime, '20140508'), 100)
-- May 8 2014 12:00AM
-- SQL Server convert date to integer
DECLARE @Date datetime; SET @Date = getdate();
SELECT DateAsInteger = CAST (CONVERT(varchar,@Date,112) as INT);
-- Result: 20161225
-- SQL Server convert integer to datetime
DECLARE @iDate int
SET @iDate = 20151225
SELECT IntegerToDatetime = CAST(convert(varchar,@iDate) as datet
ime)
-- 2015-12-25 00:00:00.000
-- Alternates: date-only datetime values
-- SQL Server floor date - sql convert datetime
SELECT [DATE-ONLY]=CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDAT
E())))
SELECT [DATE-ONLY]=CONVERT(DATETIME, FLOOR(CONVERT(MONEY, GETDAT
E())))
-- SQL Server cast string to datetime
-- SQL Server datetime to string convert
SELECT [DATE-ONLY]=CAST(CONVERT(varchar, GETDATE(), 101) AS DATE
TIME)
-- SQL Server dateadd function - T-SQL datediff function
-- SQL strip time from date - MSSQL strip time from datetime
SELECT getdate() ,dateadd(dd, datediff(dd, 0, getdate()), 0)
-- Results: 2016-01-23 05:35:52.793 2016-01-23 00:00:00.000
-- String date - 10 bytes of storage
SELECT [STRING DATE]=CONVERT(varchar,
SELECT [STRING DATE]=CONVERT(varchar,
-- Same results: 01-02-2012
GETDATE(), 110)
CURRENT_TIMESTAMP, 110)
SELECT LastDayOfWeek
, @Date)+1,-1))
-- 2016-10-30
------------- T-SQL calculate the number of business days function / UDF exclude SAT & SUN
-----------CREATE FUNCTION fnBusinessDays (@StartDate DATETIME, @EndDate
DATETI
ME)
RETURNS INT AS
BEGIN
IF (@StartDate IS NULL OR @EndDate IS NULL) RETURN (0)
DECLARE @i INT = 0;
WHILE (@StartDate <= @EndDate)
BEGIN
SET @i = @i + CASE
WHEN datepart(dw,@StartDate) BETWEEN 2 AND 6 T
HEN 1
ELSE 0
END
SET @StartDate = @StartDate + 1
END -- while
RETURN (@i)
END -- function
GO
SELECT dbo.fnBusinessDays('2016-01-01','2016-12-31')
-- 261
------------
FROM AdventureWorks2008.Sales.SalesOrderHeader
GROUP BY DATENAME(weekday, OrderDate), DATEPART(weekday,OrderDat
e)
ORDER BY DATEPART(weekday,OrderDate)
/* DayName
SalesPerWeekDay
Sunday
4482
Monday
4591
Tuesday
4346.... */
-- DATENAME application for months
SELECT MonthName=DATENAME(month, OrderDate), SalesPerMonth = COU
NT(*)
FROM AdventureWorks2008.Sales.SalesOrderHeader
GROUP BY DATENAME(month, OrderDate), MONTH(OrderDate) ORDER BY M
ONTH(OrderDate)
/* MonthName
SalesPerMonth
January
2483
February
2686
March
2750
April
2740.... */
-- Getting month name from month number
SELECT DATENAME(MM,dateadd(MM,7,-1)) -- July
Dec 31 2008
Jan 1 2009
Jan 2 2009
Jan 3 2009
Jan 4 2009
3:02AM
3:02AM
3:02AM
3:02AM
3:02AM */
an out-of-range value.
*/
SELECT CAST('2018-15-03' AS datetime)
-- 2018-03-15 00:00:00.000
SET LANGUAGE us_english
-- SQL dateformat with language dependency
SELECT name, alias, dateformat
FROM sys.syslanguages
WHERE langid in (0,1,2,4,5,6,7,10,11,13,23,31)
GO
/*
name
alias
dateformat
us_english English
mdy
Deutsch
German
dmy
Franais
French
dmy
Dansk
Danish
dmy
Espaol
Spanish
dmy
Italiano
Italian
dmy
Nederlands Dutch
dmy
Suomi
Finnish
dmy
Svenska
Swedish
ymd
magyar
Hungarian
ymd
British
British English
dmy
Arabic
Arabic
dmy */
------------- Generate list of months
;WITH CTE AS (
SELECT
1 MonthNo, CONVERT(DATE, '19000101') MonthFirst
UNION ALL
SELECT
MonthNo+1, DATEADD(Month, 1, MonthFirst)
FROM CTE WHERE
Month(MonthFirst) < 12
)
SELECT
MonthNo AS MonthNumber, DATENAME(MONTH, MonthFirst) AS MonthName
FROM CTE ORDER BY MonthNo
/* MonthNumber
MonthName
1
January
2
February
3
March ... */
------------
Related articles: