Convert text field to Date/time field ms access query - sql

Hi guys can i ask if it's possible to convert a text field to a date/time field?
for example i have a field called Month and the field is a short text data type.
Here is an example:
Month
Sep 2016
Nov 2016
Dec 2016
is it possible that i could convert this one to a date time?

Try this
DateValue("1 " & Month)
I'm assuming you want to put the first of the month in the date.
Output should be
9/1/2016
11/1/2016
12/1/2016
in your case

SELECT convert(datetime, 'Oct 2015 12', 0) or
SELECT convert(datetime, 'Oct 2015', 0) or
SELECT convert(datetime, 'Oct 2015')

It's so easy that it can't be simpler:
FirstOfMonth: CDate([Month])
The values will be primo of [Month].

Related

Conversion of datetime to nvarchar(8) in T-SQL

I'm trying to convert a datetime column to ISO format, as yyyymmdd. For example, I would like to be able to convert '13 dec 2018' to '20181213'.
According to Microsoft's T-SQL Docs, I should be able to do this using
convert(nvarchar(8), '13 dec 2018', 112)
however this doesn't work - I get the result '13 dec 2', which looks to be nothing more than the original string cut down to 8 characters.
I have also tried using
convert(datetime, '13 dec 2018', 112)
which gives me the result of 'Dec 13 2018 12:00AM' - again, nothing like what the function is supposed to produce.
What I am doing wrong? I could solve the problem easily enough using datepart() and concatenated strings, but I'd rather use the more elegant approach if possible.
Combine them:
convert(nvarchar(8), convert(datetime, '13 dec 2018'), 112)
I don't recommend using the format 112 for the first conversion, because it is misleading. SQL Server is very good at converting without a format. If you do use one the appropriate one is 106.
I figured it out - because I was entering the date as a string, and not converting it to a datetime value first, it was treating my date as if it were a string. When I handle it this way:
convert(nvarchar(8), cast('13 dec 2018' as datetime), 112)
I get the expected result, '20181213'.
select convert(nvarchar(8), cast(GETDATE() as datetime), 112) as TodayDate
YYYYMMDD format can obtain by datetime to varchar format.
select convert(varchar(8),convert(datetime, '13 dec 2018'),112)
The above query provides the required result

Convert Date/Time to Date/Military Time

How can I convert a varchar column in SQL Server 2008 within a table of 1 million+ records containing date/time to the following format: MM/DD/YYYY HH24:MI:SS
Example:
'Feb 14 2017 3:00PM' converted to '02/14/2017 15:00:00'
'Feb 18 2017 10:03AM' converted to '02/18/2017 10:03:00'
The date/time column header = ACTION_TM
Please note the double space between the year and time in the first example vs single space of the second. '2017 3:00' vs '2017 10:03'.
Thank you in advance
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
sample ... varchar(20) for length... and convert to 20 for format or any desired format.
select convert(varchar(20),getdate(),20)
however in your case you'll have to convert your column into a valid date so should be
select convert(varchar(20),cast('Feb 14 2017 3:00PM' as datetime),20)
select convert(varchar(20),cast('Feb 18 2017 10:03AM' as datetime),20)
the untidy way of doing it is
select convert(varchar(10),cast('Feb 14 2017 3:00PM' as datetime),101) + ' ' + convert(varchar(112),cast('Feb 14 2017 3:00PM' as datetime),114)

SQL Date Format 107 Without Leading Zeros For The Day

I am needing to convert a date/time stamp via SQL to the following format:
April 4, 2016
Using
SELECT
DATENAME(MM, GETDATE()) +
RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [PaymentDate]
gets me almost what I need, however, it puts a leading 0 on the day, leaving me with:
April 04, 2016
How can I get rid of that leading 0 on the day?
Alternatively, if you are using SQL Server 2012 or later, you can use the FORMAT() function for this:
Select Format(GetDate(), 'MMMM d, yyyy')
April 4, 2016
this will work:
QUERY:
SELECT DATENAME(MM, GETDATE())+' '+DATENAME(dd, GETDATE())+' '+DATENAME(yy, GETDATE()) AS [PaymentDate];
RESULT:

Convert string into date in SQL Server

Conversion failed when converting date and/or time from character string.
I'm getting the above error when running this statement in SQL Server:
SELECT CONVERT(datetime, 'Fri, 15 Jan 2016 17:30:05 GMT')
Actually I want to insert same string format in Datetime column
As suggested by Tim Biegeleisen, that string needs to be processed to be converted. In order to convert it you need to strip of the day (Fri,) and the GMT timezone at the end, for example:
DECLARE #date varchar(50) = 'Fri, 15 Jan 2016 17:30:05 GMT'
SELECT CONVERT(DATETIME, SUBSTRING(#date, 5, LEN(#date) - 8), 113)
This solution does strip the timezone information, have a look at this post if you want to convert it back to UTC.
If you want to insert the string 'Fri, 15 Jan 2016 17:30:05 GMT' into datetime column then you need to remove Fri, and GMT from the string before inserting.
SELECT CAST(substring(#str_date,5,len(#str_date)-8) as datetime)
DEMO

Need to identify number that is higher than a given date

I have a date column in a table. The date column is in varchar. I want to identify a particular date range from that date column. My query is like this:
SELECT *
FROM [003 AccptReg].[dbo].[SysData1]
WHERE [RegDate_Sys] > '18 jul 2013'
But the result is not giving accurate result, i.e. it gives dates which are prior of 18 jul 2013.
Is there any thing wrong I am doing?
For date column, you should compare as DATE
select * from [003 AccptReg].[dbo].[SysData1]
where CAST([RegDate_Sys] AS DATE) > CAST('18 jul 2013' AS DATE)
The problem is that you have the date as a varchar, and doesn't convert it to a date when you are doing the comparison. The database doesn't know that you see the data as dates, and will simply compare them as strings, so for example '2 jan 1736' will be larger than '18 jul 2013' because 2 comes after 1.
The best would be if you could store the data as datetime values (or date), then you don't need to do the conversion when you compare the values, which would give better performance.
If that's not possible, do the conversion in the query:
select * from [003 AccptReg].[dbo].[SysData1]
where convert(datetime, [RegDate_Sys], 106) > '18 jul 2013'
Depending on the settings on the server, you might also need to convert '18 jul 2013' in the same way for the database to understand it correctly as a date.
Convert the date to datetime format and then compare:
select * from [003 AccptReg].[dbo].[SysData1]
where convert(datetime,[RegDate_Sys]) >convert(datetime,'18 jul 2013')