Extract date field from a sentence using SQL - sql

I am trying to extract the date from a sentence like the below in SQL Server:
CC - Date of Pay - 7/26/2019;
Ideally, I would like to extract the date into its own column called DateofPay

Please try this code bellow :
SELECT SUBSTRING(RIGHT(`sentence`,10),1,9) as NewDate

Related

How do I run an access query to pull only records with the current month from a date that is formatted as a short text?

I have some tables that I cannot modify and a field "STAT_DATE" that is formatted as a short text like "yyyymmdd". I tried the Month(now()) functions and this did not work because the field is not in date format. I have also tried using the mid and monthname functions to try and match the current months name with the name of the digits that are meant to be the month in the string.
Use Like to compare your date text to the current date formatted as 'yyyymm\*'
Here is an Immediate window session to demonstrate the technique:
' give STAT_DATE today's date ...
STAT_DATE = "20220913"
' display today's date as the yyyymm* pattern ...
? Format(Date(), "yyyymm\*")
202209*
? STAT_DATE Like Format(Date(), "yyyymm\*")
True
Then use that approach in a query:
SELECT y.*
FROM [Your Table] AS y
WHERE y.STAT_DATE Like Format(Date(), 'yyyymm\*');
If you're using ADO/OleDb to run your query, use % instead of * as the wildcard:
WHERE y.STAT_DATE Like Format(Date(), 'yyyymm\%');

Database table - Converting Date to Number

I am trying to convert a date to a number.
I need to have in my table a field with default value based on a date.
So if today is 23/01/2018, I want to have a number look like this 23012018.
Any ideas?
You can use the format command like this:
=Format$(Date, "ddmmyyyy")

Date format code for MM/DD

I would like to send the date format from the database as Month/Day only. I've already stored the date in a table, now I want to send it like this format. The SQL I have is:
CONVERT(VARCHAR(15),date,102) AS date
Which sends MM/DD/YYYY. My question is what is the code to send MM/DD only?
Try this:
SELECT convert(varchar,DATEPART(mm,date_column)) + '/' +
convert(varchar,DATEPART(dd,date_column)) from your_table
SQL Fiddle Link

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...

MS Access - Select Char as Date and doing a date diff

I have two columns. ColA and ColB contains char(10) with data "20090520" and "20090521".
I want to select and get the date difference in days. I have tried using Format() and CDate()
but MS Access always display as #ERROR.
Access prefers its dates in this format:
#2009-12-01#
You can convert your date to something Access understands with:
CDate(Format([ColA], "0000-00-00"))
Or alternatively:
DateSerial(Left([ColA],4),Mid([ColA],5,2),Right([ColA],2))
And to display the result in your preferred format:
Format(<date here>, "dd-mm-yyyy")
Try using DateSerial() to convert the dates:
DateSerial(Left([FieldName],4),Mid([FieldName],5,2),Right([FieldName],2))
If at all possible, change the datatype to a date datatype. You should not store dates as character data.
I am connecting to another database which I have no control on. That is why this problem occurred. Thanks for the feedback.