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

MSSQL replace string function

-- SQL date & time eliminating dividing characters
------------
-- MSSQL replace string function
-- T-SQL string concatenate (+)
USE AdventureWorks2008;
SELECT replace(convert(VARCHAR(10),getdate(),102),'.','')
-- 20120315
SELECT replace(convert(VARCHAR(10),getdate(),111),'/','')
-- 20120315  
-- SQL triple replace
SELECT replace(replace(replace(convert(VARCHAR(25),
       getdate(),20),'-',''), ':',''),' ','')
-- 20120529090427
-- T-SQL concatenating from a date and a time conversion
SELECT replace(convert(VARCHAR(10),getdate(),111),'/','') +
       replace(convert(VARCHAR(8),getdate(),108),':','')
-- 20120315085654


SQL date styles and SET Date format in SQL

 USE AdventureWorks
-- Selected named date styles
DECLARE @DateTimeValue varchar(32)

-- US-Style
-- Convert string to datetime sql - sql convert string to datetime
SELECT @DateTimeValue = '10/23/2016'
SELECT StringDate=@DateTimeValue,
[US-Style] = CONVERT(datetime, @DatetimeValue)

SELECT @DateTimeValue = '10/23/2016 23:01:05'
SELECT StringDate = @DateTimeValue,
[US-Style] = CONVERT(datetime, @DatetimeValue)

-- UK-Style, British/French
SELECT @DateTimeValue = '23/10/16 23:01:05'
SELECT StringDate = @DateTimeValue,
[UK-Style] = CONVERT(datetime, @DatetimeValue, 3)

SELECT @DateTimeValue = '23/10/2016 04:01 PM'
SELECT StringDate = @DateTimeValue,
[UK-Style] = CONVERT(datetime, @DatetimeValue, 103)

-- German-Style
SELECT @DateTimeValue = '23.10.16 23:01:05'
SELECT StringDate = @DateTimeValue,
[German-Style] = CONVERT(datetime, @DatetimeValue, 4)


SELECT @DateTimeValue = '23.10.2016 04:01 PM'
SELECT StringDate = @DateTimeValue,
[German-Style] = CONVERT(datetime, @DatetimeValue, 104)


-- Double conversion to US-Style 107 with century: Oct 23, 2016
SET @DateTimeValue='10/23/16'
SELECT StringDate=@DateTimeValue,
[US-Style] = CONVERT(varchar, CONVERT(datetime, @DateTimeValue),107)

-- SQL dateformat setting
USE AdventureWorks2008;
SELECT convert(datetime,'14/05/08')
/* Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
*/
SET DATEFORMAT ymd
SELECT convert(datetime,'14/05/08')   -- 2014-05-08 00:00:00.000
-- Setting DATEFORMAT to UK-Style
SET DATEFORMAT dmy
SELECT convert(datetime,'20/05/14')   -- 2014-05-20 00:00:00.000
-- Setting DATEFORMAT to US-Style
SET DATEFORMAT mdy
SELECT convert(datetime,'05/20/14')   -- 2014-05-20 00:00:00.000
SELECT convert(datetime,'05/20/2014') -- 2014-05-20 00:00:00.000
GO


------------