Out of range when converting data SQL Server - sql-server

I have a varchar column with string like date dd.mm.yyyy and I need it to convert to same string only with different format like yyyy.mm.ddT00:00:000
SOLVED: 1st Test:
SELECT REPLACE(CONVERT(varchar, CAST(table.DATE AS datetime), 126),'-','.') AS date
FROM tabletest.DBO.TABLE
Result:
I get 5 values (from more than 1000) and msg:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
SOLVED: 2nd Test:
SELECT REPLACE(CONVERT(varchar, (RIGHT(GREMPG1.DAT,4)+SUBSTRING(GREMPG1.DAT,3,4)+LEFT(GREMPG1.DAT, 2) + ), 126),'-','.') AS Datums
FROM tabletest.DBO.GREMPG1
Cannot find solution on how to get second part to this.
Maybe there is some shortest way yo convert columns record format?
Thanks.
Edited:
For second query:
Old value: 01.03.2014
New value: 2014.03.01
Need to get: 2014.03.01T00:00:00

For your first test try this way -
SELECT REPLACE(CONVERT(varchar, CONVERT(datetime,table.DATE,103), 126),'-','.') AS date FROM tabletest.DBO.TABLE
For your 2nd try -
SELECT REPLACE(CONVERT(varchar, CAST((RIGHT(GREMPG1.DAT,4)+SUBSTRING(GREMPG1.DAT,3,4)+LEFT(GREMPG1.DAT, 2)) AS DATETIME), 126),'-','.') AS Datums FROM tabletest.DBO.GREMPG1

Use this:
SELECT REPLACE(CONVERT(varchar, CONVERT(datetime,'15.02.2012',104), 126),'-','.') AS date
You get confision with the date when you cast. 15.02.2012 is calculated as mm.dd.yyyy and because there is not month 15 you get error.
So you query will look like:
SELECT
REPLACE(CONVERT(varchar, CONVERT(datetime,table.DATE,104), 126),'-','.') AS date
FROM
tabletest.DBO.TABLE;

Your SELECT seems to work. Try this:
declare #d VARCHAR(100)='13.12.2015';
SELECT REPLACE(CONVERT(varchar, CAST(#d AS datetime), 126),'-','.') AS date
/*
Result: 2015.12.13T00:00:00
*/
If it does NOT work, I'd assume a culture issue (date formats are handled quite differently according to your system's language and country).
If it works, I'd assume at least one value in your db being bad...

Related

How to convert from DD/mm/yyyy in varchar(max) to yyyymmdd format in SQL Server

Dates in DD/mm/yyyy format stored in a varchar(max) column need to be converted to yyyymmdd format.
The below options to convert / cast are not supported.
CONVERT(VARCHAR(19), CONVERT(DATETIME, wfm.date, 3), 112)
CONVERT(VARCHAR(50), CAST(wfm.date AS DATETIME2), 112)
CAST(CAST(wfm.date AS DATETIME2(15)) AS DATETIME)
I'm getting an error message for CONVERT syntax:
Conversion failed when converting date and/or time from character string.
For CAST syntax, I'm getting this error:
Line 1: Specified scale 15 is invalid. 'dd/mm/yyyy/ in varchar to be converted to yyyymmdd format
Ideally, don't store dates as a varchar, store them as a date and worry about the format they are displayed in in your presentation layer. Therefore, you should just do:
SELECT TRY_CONVERT(date,YourVarcharMAXDate,103);
If you must get the format yyyyMM`` (i don't recommend, change your data type of your column todate), then you can use a furterhCONVERT`:
SELECT CONVERT(varchar(8),TRY_CONVERT(date,YourVarcharMAXDate,103),112);
If the data engine can't convert the date, due to it being an invalid format, TRY_CONVERT will return NULL; you'll need to fix those values (make them valid). Another good reason why not to use a varchar to store a date.
Maybe this helps
DECLARE #dt DATETIME= '2019-12-31 14:43:35.863';
SELECT CONVERT(VARCHAR(10), #dt, 11) s1,
CONVERT(VARCHAR(10), #dt, 111) s2;
You could always just use substring for this. Example is shown below.
DECLARE #samples TABLE
(
Dates NVARCHAR(50)
)
INSERT INTO #samples VALUES
('10/11/2018'),
('12/11/2018'),
('15/11/2018'),
('17/11/2018'),
('19/11/2018'),
('20/11/2018')
SELECT
SUBSTRING(Dates, 7,4) +
SUBSTRING(Dates, 4,2) +
SUBSTRING(Dates, 1,2) as [NewDate]
FROM #samples
Output will be a list of strings with your required format. This won't validate for you, but you can always try convert these to a date using CAST and it should correctly cast the yyyyMMdd to a date if the date is correct.
If you're using SQL Server 2016 or newer, you can use PARSE and FORMAT to do this quite easily:
SELECT
FORMAT(PARSE(wfm.Date AS DATE USING 'de-DE'), 'yyyyMMdd')
FROM
dbo.WhateverYourTableNameIS
The dd/mm/yyyy format is the format used e.g. in Germany, that's why I'm using the de-DE culture to convert that string to a DATE. Then you can format it using the usual formatting strings as used in C#/.NET.
select convert(varchar, convert(datetime, wfm.date, 103), 112)

Convert DDMMYYYY to YYYYMMDD date format in sql server?

I have a date in table as "26052016" in format DDMMYYYY
I want to convert this date to "YYYYMMDD" format.
Any idea
I have tried this method
select CONVERT(varchar(8),[doc-date],112) FROM C034_PDK_ParallelBillingSourceExtract
But this is gives me the same date as a result.
Please help me
I can find this way, i don't know if any other way exist or not..
declare #date nvarchar(max)='01052016'
select convert(varchar(8),cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date),112)as [YYYYMMDD]
Clear Code:
declare #date nvarchar(max)='01052016'
declare #date1 date
set #date1 =cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date)
select convert(varchar(8),#date1,112)as [YYYYMMDD]
If you are using Sql version< 2012 then you need to skip CONCAT and use + for string concatination.
SELECT CONVERT(VARCHAR(8), doc-date, 112) AS [YYYYMMDD] from
C034_PDK_ParallelBillingSourceExtract
Check ... this should work correctly.
Thanks
SELECT CONVERT(VARCHAR(8), '26052016', 112) AS [YYYYMMDD] from
C034_PDK_ParallelBillingSourceExtract
Try like this,
SELECT substring([doc-date], 5, 4) + substring([doc-date], 3, 2) + substring([doc-date], 1, 2) AS [YYYYMMDD]
FROM C034_PDK_ParallelBillingSourceExtract
There are differet ways to do it.
The best way is to use substring method as you know the character positions are going to remain same.
For Example
Suppose your date is - 31122015
Pick the portions of date using substring method and concatenate them
select SUBSTRING('31122015',5,4) + SUBSTRING('31122015',3,2) + SUBSTRING('31122015',1,2)
The result would be - 20153112
Since SQL Server 2016 we have a couple of handy tools for this:
Use DATEFROMPARTS and SUBSTRING to convert odd date formats from any arrangement within a Varchar to an actual date:
SELECT DATEFROMPARTS(SUBSTRING('31122015',5,4), SUBSTRING('31122015',3,2), SUBSTRING('31122015',1,2))
Use FORMAT to Convert an actual date to YYYYMMDD:
SELECT FORMAT(MyDate, 'yyyyMMdd')
watch out for the yyyyMMdd, that's the only part of MS SQL that is case-sensitive. Lower case mm is "minutes" and upper case MM is "Month", upper case YYYY or DD is nothing, and will just add letters to your output!

Need help on DateTime Conversion in Sql Server 2005

I am facing problem in DateTime converstion.
My input is 09/22/2011, I need to convert into 20110922.
I tried the below one, but failed.
Select CONVERT(VARCHAR(8),'09/22/2011' , 112) as DateConv
Here you go:
SET DATEFORMAT mdy
SELECT CONVERT(VARCHAR(8), CAST('09/22/2011' as DATETIME) , 112) AS DateConv
If your input is actually a dateTime variable like you said (but didn't show in your code example), you can simplify this down to:
SELECT CONVERT(VARCHAR(8), #myDate , 112) AS DateConv
This will do the trick:
select year('09/22/2011') * 10000 + month('09/22/2011') * 100 + day('09/22/2011')
Without any other information, SQL is interpreting the '09/22/2011' as a varchar already, and just passes the data through (ignoring CONVERT's style argument). If you use the following line:
SELECT CONVERT (VARCHAR (8), CAST ('09/22/2011' as DATETIME), 112) as DateConv
it should work as expected since it will then view the '09/22/2011' as a date value. If you were getting the date from a column in a table, it would already know the type and you will not need to worry about the CAST portion of the expression, just use the column name.

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

Datetime display using month name in T-SQL

There is a field which is date type in a table. The format of this field is mm/dd/yy. Is there any way to convert it to dd-Month name-yy?
Best Regards,
Without any hassle, you can use CONVERT to get "dd MONTHNAME yyyy" format:
SELECT CONVERT(VARCHAR, GETDATE(), 106)
e.g. "25 Jan 2010"
If you want your exact format, you may need a bit of manual fiddling around like:
SELECT CAST(DAY(GETDATE()) AS VARCHAR) + '-' + LEFT(DATENAME(mm, GETDATE()), 3) + '-' + RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR), 2)
e.g. "25-Jan-10"
Update:
In fact, a shorter way to achieving this is:
SELECT REPLACE(CONVERT(VARCHAR, GETDATE(), 6), ' ', '-')
e.g. "25-Jan-10"
Take a look at the CAST and CONVERT functions:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
example:
select convert( char(8), getdate(), 5 )
output:
dd-mm-yy
Source: compuspec date format conversion
you can try convert() ?
Maybe you need to differentiate between storing datetime values in SQL Server and displaying them. Here is a great article that explains what's going on behind the scenes: The ultimate guide to the datetime datatypes

Resources