Showing posts with label Convert. Show all posts
Showing posts with label Convert. Show all posts

Monday, March 31, 2014

SQL Server convert from string to smalldatetime

------------
-- SQL Server convert from string to smalldatetime
------------
-- T-SQL convert from format mm/dd/yyyy to smalldatetime
SELECT CONVERT(smalldatetime, '10/23/2016', 101)
-- 2016-10-23 00:00:00
-- MSSQL convert from format dd/mm/yyyy to smalldatetime
SELECT CONVERT(smalldatetime, '23/10/2016', 103)
-- 2016-10-23 00:00:00
-- Month 23 is out of range
SELECT CONVERT(smalldatetime, '23/10/2016', 101)
/* Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a smalldatetime data type resulted
in an out-of-range value.
*/
------------
-- Translate/convert string/text hours and minutes to seconds
------------
DECLARE @TimeStr varchar(16) = '20:49:30'
SELECT   PARSENAME(REPLACE(@TimeStr,':','.'),1)
       + PARSENAME(REPLACE(@TimeStr,':','.'),2) * 60
       + PARSENAME(REPLACE(@TimeStr,':','.'),3) * 3600
-- 74970
------------


Converting string dates from a table

------------
-- Converting string dates from a table
------------

-- Create and populate a test table with a string date
USE tempdb;
SELECT
      DepartmentID,
      LastUpdate=CONVERT(varchar,
                 dateadd(day, DepartmentID, ModifiedDate),100)
INTO DeptInfo
FROM AdventureWorks.HumanResources.Department

SELECT * FROM DeptInfo
/* Partial results

DepartmentID      LastUpdate
1                       Jun  2 1998 12:00AM
2                       Jun  3 1998 12:00AM
*/

-- Convert string date column to datetime
SELECT
      DepartmentID,
      LastChangeDate=convert(datetime, LastUpdate)
FROM DeptInfo
/* Partial results

DepartmentID            LastChangeDate
1                       1998-06-02 00:00:00.000
2                       1998-06-03 00:00:00.000
*/
DROP TABLE DeptInfo

GO

Wednesday, March 26, 2014

SQL DateTime Conversion to many style (Formats)

USE AdventureWorks
SET LANGUAGE us_english
-- Convert string to date using style (format) numbers - sql dates format
-- SQL convert text to date - SQL convert string to date / datetime
SELECT convert(datetime,'15/03/18',3) -- 2018-03-15 00:00:00.000
SELECT convert(datetime,'15.03.18',4) -- 2018-03-15 00:00:00.000

-- Convert datetime to text style (format) list - sql time format
-- SQL Server without century (YY) date styles (there are exceptions!)
-- Generally adding 100 to style number results in century format CCYY / YYYY
SELECT convert(varchar,getdate())    -- Mar 15 2018 10:35AM
SELECT convert(varchar,getdate(),0)  -- Mar 15 2018 10:35AM
SELECT convert(varchar,getdate(),1)  -- 03/15/18
SELECT convert(varchar,getdate(),2)  -- 18.03.15
SELECT convert(varchar,getdate(),3)  -- 15/03/18
SELECT convert(varchar,getdate(),4)  -- 15.03.18
SELECT convert(varchar,getdate(),5)  -- 15-03-18
SELECT convert(varchar,getdate(),6)  -- 15 Mar 18
SELECT convert(varchar,getdate(),7)  -- Mar 15, 18
SELECT convert(varchar,getdate(),8)  -- 10:39:39
SELECT convert(varchar,getdate(),9)  -- Mar 15 2018 10:39:48:373AM
SELECT convert(varchar,getdate(),10) -- 03-15-18
SELECT convert(varchar,getdate(),11) -- 18/03/15
SELECT convert(varchar,getdate(),15) -- 180315
SELECT convert(varchar,getdate(),13) -- 15 Mar 2018 10:41:07:590
SELECT convert(varchar,getdate(),14) -- 10:41:25:903
SELECT convert(varchar,getdate(),20) -- 2018-03-15 10:43:56
SELECT convert(varchar,getdate(),21) -- 2018-03-15 10:44:04.950
SELECT convert(varchar,getdate(),22) -- 03/15/18 10:44:50 AM
SELECT convert(varchar,getdate(),23) -- 2018-03-15
SELECT convert(varchar,getdate(),24) -- 10:45:45
SELECT convert(varchar,getdate(),25) -- 2018-03-15 10:46:11.263

-- T-SQL with century (YYYY or CCYY) datetime styles (formats)
SELECT convert(varchar, getdate(), 100) -- Oct 23 2016 10:22AM (or PM)
SELECT convert(varchar, getdate(), 101) -- 10/23/2016
SELECT convert(varchar, getdate(), 102) -- 2016.10.23
SELECT convert(varchar, getdate(), 103) -- 23/10/2016
SELECT convert(varchar, getdate(), 104) -- 23.10.2016
SELECT convert(varchar, getdate(), 105) -- 23-10-2016
SELECT convert(varchar, getdate(), 106) -- 23 Oct 2016
SELECT convert(varchar, getdate(), 107) -- Oct 23, 2016
SELECT convert(varchar, getdate(), 108) -- 09:10:34
SELECT convert(varchar, getdate(), 109) -- Oct 23 2016 11:10:33:993AM (or PM)
SELECT convert(varchar, getdate(), 110) -- 10-23-2016
SELECT convert(varchar, getdate(), 111) -- 2016/10/23
SELECT convert(varchar, getdate(), 112) -- 20161023
SELECT convert(varchar, getdate(), 113) -- 23 Oct 2016 06:10:55:383
SELECT convert(varchar, getdate(), 114) -- 06:10:55:383(24h)
SELECT convert(varchar, getdate(), 120) -- 2016-10-23 06:10:55(24h)
SELECT convert(varchar, getdate(), 121) -- 2016-10-23 06:10:55.383
SELECT convert(varchar, getdate(), 126) -- 2016-10-23T06:10:55.383
GO