How to convert string YYYYMM to datetime in sql? - sql-server

I have a string '2016-03-05' which I want convert to a datetime.
Here is my code:
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 112).
Running that I receive the error
Conversion failed when converting datetime from character string.

Conversion format 112 you've used assumes input '20160305' (without dashes as date parts separator).
So either do
select convert(Datetime, '20160305', 112)
or (if your input really contains dashes then just remove them like:
select convert(Datetime, replace('2016-03-05', '-', ''), 112)

That's because the style you're passing to the CONVERT function does not support that format. You can do two things...
1- Drop the dashes
Declare #period as nvarchar(10)
Set #period = '20160305' -- I've dropped the dashes here
Select Convert(Datetime, #period, 112)
2- Change the style to something that supports this format
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
Select Convert(Datetime, #period, 21) -- I've changed the style here
For a complete reference, read the MSDN documentation

SELECT LEFT(201605,4) + '-' + RIGHT(201605,2)
{OR}
SELECT RIGHT(201605,2) + '-' + LEFT(201605,4)

Try this it will work I guess according to your need
Declare #period as nvarchar(10)
Set #period = '2016-03-05'
select CAST(#period as datetime)

Related

SQL query accepting date parameter in MM-DD-YYYY format instead of DD-MM-YYYY

Here is my stored procedure which accepts two date parameters, one for start date and second for end date. It gets a bunch of different data from joined tables:
#FromDate varchar(50),
#ToDate varchar (50)
SELECT DISTINCT
dbo.DefectInspection.DefectInspection_Id,
CONVERT(varchar(50), CAST(dbo.DefectInspection.DefectInspection_CreatedDate AS date), 34) AS CreatedDate
FROM
(bunch of tables)
WHERE
CAST(DefectInspection.DefectInspection_CreatedDate AS date)
BETWEEN CAST( #FromDate AS Date) AND CAST(#ToDate AS Date)
The issue is it will only return date if I input my dates as MM-DD-YYYY instead of the days firsts. This is an issue because the date style sent from client side is always DD-MM-YYYY
Using desired input: no data returedn
Using month format - data returned
When using SQL (and other languages) its best to use an unambiguous format such as yyyy-mm-dd
Also consider using the DATE type instead of VARCHAR
#FromDate DATE(50),
#ToDate DATE(50)
Though using the wrong datatypes is terrible, sometimes we all have to deal with issues which cannot be changed easily.
You can use:
DECLARE #fromDate AS varchar(50);
DECLARE #toDate AS varchar(50);
DECLARE #from AS date;
DECLARE #until AS date;
SET #fromDate = '09-01-2023';
SET #toDate = '10-01-2023';
SET #from = TRY_CONVERT(date, #fromDate, 105);
SET #until = TRY_CONVERT(date, #toDate, 105);
IF #from IS NULL OR #until IS NULL
THROW 51000, 'Parameter is not a valid date ..', 0;
ELSE BEGIN
SELECT DISTINCT
...
FROM
(bunch of tables)
WHERE TRY_CONVERT(date, DefectInspection.DefectInspection_CreatedDate, 110) BETWEEN #from AND #until;
END;
TRY_CONVERT returns NULL if the parsing fails.
The 3rd parameter (style) 105 means format "dd-mm-yyyy" like you use it, 110 means "mm-dd-yyyy".

SQL Server - convert short date in from old database

I have old DBF database that contains date in old 6 digit formats like 291292 or 150793 or even 010302.
I import tables into SQL Server and now I need to convert it to datetime format.
Of course must be converted to similar strings 29.12.1992, 15.07.1993, 01.03.2002 first.
You may try this.
GO
declare #date date
set #date = '901124'
select CONVERT(varchar(10), #date, 102) as [Date] --- for yyyy.MM.dd format
TRY THIS
declare #dd varchar(10)
set #dd = '201292'
select CONVERT(VARCHAR(10), #dd, 2), CONVERT(VARCHAR(10), GETDATE(), 2)
, (SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2))
, CONVERT(varchar(10), cast((SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2)) as DATE) , 102)
Ok it pretty simple:
GO
declare #date varchar(6)
set #date = '241190'
select CONVERT(date, concat(SUBSTRING(#date, 5,2),SUBSTRING(#date, 3,2),SUBSTRING(#date, 1,2)), 101) as [Date]
result is 1990-11-24
declare #date date = '901124'
select CONVERT(date, #date) as [Date]

Convert 24h to 12h datetime format in SQL Server

I want a datetime variable which will be having 12 hour datetime format.
Below example converts date in 12 hour but not helpful because it is in VARCHAR format, if tries to convert in datetime it shows like 2016-03-08 00:00:00.000-
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
SELECT CONVERT(VARCHAR, #t, 100) AS DateTime_In_12h_Format
I want a variable which will be holding 12 hour format something like this
declare #t datetime = NULL
set #t = '2016-03-08 00:00:00'
set #t = CONVERT(datetime, #t, 100)
select #t -> this should be -> Mar 8 2016 12:00AM
If you want to convert the current datetime for example:
SELECT CONVERT(VARCHAR, getdate(), 100) AS DateTime_In_12h_Format
Instead of getdate() you can put your desired column in a query (such as tdate in your example). If you want JUST the time in 12h and not the date and time use substring/right to separate them. It seems that you already know how to =).
This page lists every datetime conversion. It's really handy if you need other types of conversions.
Declare one more varchar to save new datetime
declare #t datetime = NULL
declare #t1 varchar(20) = NULL
set #t = '2016-03-08 00:00:00'
set #t1 = CONVERT(varchar(20), #t, 100)
select #t1 as DateTime_In_12h_Format

Short Date to Date

Good Day
I am trying to convert a short date i.e 780506 to a date i.e 1978/05/06 , I have tried CASTING the date CAST(CONVERT(VARCHAR, 78) + '-' + CONVERT(VARCHAR, 05) + '-' + CONVERT(VARCHAR, 06)
AS DATETIME) but this is not working. I tried Converting the date:CONVERT(VARCHAR(6), 780506), 12) and also not working. Is there am easy way to do this? The reason I need this is that the first 6 digits of out countries ID number = Date of Birth, I am just trying to convert it into a workable date.
Thanks
Convert to date type:
DECLARE #s VARCHAR(10) = '780506'
SELECT convert(DATE, #s)
For the column:
SELECT CONVERT(DATE, ColumnName) FROM TableName
You can use CAST or CONVERT. Like this:
DECLARE #shortdate VARCHAR(10) = '780506'
SELECT CONVERT(DATE, #shortdate)
SELECT CAST(#shortdate AS DATE)
Result:
1978-05-06

datetime varchar yyyymmdd_hhmiss to datetime

I have datetime format in varchar yyyymmdd_hhmiss format. I need to convert it to SQL Server datetime .
Is there any easier way ?
for example, 20150622_012030 into '2015-06-22 01:20:30.000' datetime
I got my code below but not sure how to convert time part
SELECT CONVERT(DATEtime,LEFT('20150617_015814',8)), RIGHT('20150617_015814',6)
-- Change the format to yyyyMMdd HH:mm:ss then do the convert
SELECT CONVERT(datetime,
STUFF(
STUFF(
REPLACE('20150622_012030', '_', ' ')
, 14, 0, ':')
, 12, 0, ':')
)
-- Result
2015-06-22 01:20:30.000
Try this
declare #datetime varchar(6) = right('20150622_012030',6)
declare #date varchar(10) = left('20150622_012030',8)
SELECT convert(datetime,#date+' '+left(#datetime,2)+':'+RIGHT(left(#datetime,4),2)+':'+RIGHT(#datetime,2))

Resources