convert to Date format in MM/YY? - sql

I am converting date into MM/YY, but it is converted to varchar. How to change that back to datetime datatype?
select RIGHT(CONVERT(VARCHAR(8), e.[Start_Date], 3), 5) AS 'Month/Year'
from table1

I am converting date into MM/YY, but it is converted to varchar. How to change that back to datetime datatype?
does this mean you are updating back same field with varchar value.How this is possible ?
where is date field lost ?
Declare #i datetime =getdate()
select stuff(convert(varchar(10),#i,103),1,3,'')

My understanding is that you have a value like "01/13" (Jan 2013) and you want to produce a DATETIME from it.
DECLARE #mmyy VARCHAR(5) = '01/13';
SELECT CAST('20'+RIGHT(#mmyy, 2)+'-'+LEFT(#mmyy, 2)+'-01' AS DATETIME)
-- returns 2013-01-01 00:00:00.000
Of course since your year is 2 digit I have to make the assumption that it's in the 21st century.

Related

Dutch varchar date issue in SQL server 13 month

I have the following datetime format ( as varchar ) in my database 13-04-2018 1:05:00.
I need to convert it to the following format: 2018-04-13 01:05:00. As datetime.
Normal convert functions can't do this because they try to take the 13th month, and that month doesn't exist. This error:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
Does someone know how to convert this date issue?
Using datetimes is always a pain regardless of the language because of all the different formats across the world.
To sort your issue out currently, you need to use a format style which is a third parameter to CONVERT. Personally what I would suggest here is to store as a datetime, because storing datetimes as strings is never a good idea. It just gets too messy later on, but if saved in the format you would like, it would be saved as yyyy-MM-dd hh:mm:ss
SELECT CONVERT(DATETIME, '13-04-2018 1:05:00',103)
You can create your own function to format it in your desired output string.
CREATE FUNCTION FormatMyDate
(#Date DATETIME) RETURNS VARCHAR(20)
AS
BEGIN
RETURN FORMAT(#Date,'yyyy-dd-MM hh:mm:ss')
END
And then you can call it in SELECT statements like this:
SELECT dbo.FormatMyDate(yourDateCol)
FROM yourTable
this takes the date from the format where month comes before day and reverses the 2 values (month and day)
SELECT CONVERT(DATETIME, '2018-13-04 01:05:00', 103);
Results:
2018-04-13 01:05:00.000
This should work for you requirement...
SELECT FORMAT(yourdate, 'yyyy-dd-MM')
Your Solution Bro...
DECLARE #d DATETIME = GETDATE()
SELECT FORMAT ( #d, 'MM-dd-yyyy hh:mm:ss', 'de-de' ) AS 'Hoping your result'

Convert and Operation Not working as expected. Ignoring Year value

I have the following code:
CONVERT(VARCHAR(20), TimeCard_Date, 101) <
CONVERT(VARCHAR(20), dateadd(dd,-3,getdate()), 101)
The Original TimeCard_Date value = 2018-06-01
The GetDate() return = 11/14/2017
Can anyone assist as to why it thinks the Timecard_Date value set for June 2018 is less than the GetDate() minus 3 days value?
When you convert, it converts to a varchar datatype. 06/01/2018 is less than 11/14/2017 as a varchar since it is an alphabetical (or by number?) comparison. If you compare by date, the comparison is by the date datatype, which is as you expect.
You can change your code to:
TimeCard_Date < dateadd(dd,-3,getdate())
You don't need to convert DATETIME to VARCHAR in order to compare dates. Just use:
TimeCard_Date < DATEADD(dd,-3,GETDATE())
On the other hand, if you ever have to convert them to do it, you have to standarize the format (yyyyMMdd). You can check the FORMAT function https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql

How do I convert date like '31/06/2013' or '30/02/2013' of datatype varchar in SQL Server 2008

I have a column named next_due_date datatype varchar and in this column some non-existing dates like 31/06/2012 or 30/02/2013 are saved. Because of this I get an error message when I convert it to date datatype.
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
If you dates would be valid (not something like the 31st of June or 30th of February - those dates simply don't exist!), then you could easily use the CONVERT function to convert them to DATE:
DECLARE #DateTable TABLE (DateColumn VARCHAR(20))
-- **VALID** dates - 30th of June, 28th of Feb
INSERT INTO #DateTable(DateColumn) VALUES ('30/06/2012'), ('28/02/2013')
-- easily converted to DATE type using style #104
SELECT DateColumn, CONVERT(DATE, DateColumn, 104)
FROM #DateTable
You can use try_parse (which is only available on higher SQL version, from 2012 on wards). It checks if a date is valid. If so, it returns the date, else null.
I think en-GB is the culture you need:
select try_parse('30/06/2012' as date using 'en-GB') -- returns a valid date
select try_parse('31/06/2012' as date using 'en-GB') -- returns null
If you need this on lower platforms, I would suggest to parse it by hand using a procedure. Something like this:
BEGIN TRY
select CONVERT(date,'31/06/2012',103)
END TRY
BEGIN CATCH
select null
END CATCH

Convert from nvarchar to DateTime with am pm?

In my table I have myDate column of type nvarchar(50).
The result I need is to select this date/time: 07/11/2013 11:22:07
And I need to get 07/11/2013 11:22:07 am from it (add am/pm to the original date&time).
I tried everything but get only the original data without am/pm.
This is an example from my query :
select convert(dateTime,myDate,100) as Date from Info
or
select convert(dateTime,myDate,0) as Date from Info
What am I missing ?
try this !!
declare #date datetime
set #date='07/11/2013 11:22:07'
SELECT cast(convert(varchar(20),substring(convert(nvarchar(20),#date, 9), 0, 21)
+ ' ' + substring(convert(nvarchar(30), #date, 9), 25, 2),105) as datetime)
Your field is a NVARCHAR field so just return it without any conversion. In your query you convert string representation into the DATETIME type and returns it. Your browser software which shows query results convert DateTime value into string representation to show it to you and conversion format depends on this software usually you can change it changing Windows Regional Settings.
You can get AM/PM data using following query
declare #date datetime
select #date= CAST('07/11/2013 11:22:07' AS datetime)
select RIGHT ( CONVERT(VARCHAR,#date,9),2)

Query: Assigning error datetime2

I want to assign '1392-04-31' using this code:
DECLARE #t DATETIME
SET #t = '92-04-31'
I see this error:
Conversion failed when converting date and/or time from character string.
Any one know why?
The solution is:
use datetime2!
DECLARE #t datetime2
SET #t = '1392-04-30'
Because you can't use datetime:
The minimum date stored in datetime is January 1, 1753. So 1392 is not storeable.
April has 30 days.
Using formatted date with datetime:
Second, when you write a date in Sql Server, the format I prefer is {d 'YYYY-MM-DD'}, so in your case becomes:
DECLARE #t DATETIME
SET #t = {d '1992-04-30'}
To complete this discussion, if you want use hh mm ss so you must use this format: {ts 'yyy-mm-dd hh:mm:ss.mmm'}
DECLARE #t DATETIME
SET #t = {ts '1992-04-30 23:59:59.123'}
try this :
declare #t DATETIME
set #t = '1992-04-30 10:54:30'
The date you are trying to set is probably invalid.
Also there are several ways of representing dates in SQL as a string, depending on Language, Dateformat and other setting. Typically the safest way to do this is to use the 'YYYYMMDD' format.
The article below will also answer the question : Why is 1753 the earliest date for datetime?
You should read this if you would like some detailed information:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
First of all, April only has 30 days. I'm not going to take the time to look up historically whether that was the case in 1392, but either way I'm pretty sure the date 4/31/1392 is invalid for a SQL Server DATETIME.
Also, you should use the full year in the format '01-01-2013'.
Try the following and you'll get the output Jan 1 2013 12:00AM.
declare #t DATETIME
set #t = '01-01-2013'
PRINT #t
The above should work for any valid date.