using LIKE and BETWEEN in sql query - sql

alter PROCEDURE [dbo].[select_User_attendance_master_Date]
(
#From_date NVarchar(100),
#To_date NVarchar(100)
)
as
begin
select
Employee_Attendace_Code,
Convert(varchar(11),Employee_Attendance.Attendance_day,106) as Attendanceday
from Employee_Attendance
(Convert(varchar(11),Employee_Attendance.Attendance_day,106) between '%'+#From_date+'%'
AND '%'+#To_date+'%')
end
this query is not working it is getting nothing in Attendance_day values are eg 2013-11-28

Please rethink your question.
LIKE is great to operate over strings but ill not work well for dates.
depending on your data it can return a from date is greater than to date.
Try to use date/datetimes variables, if you receive dates as a string, convert that dates to date/datetime data type and just put that values in the between.
And receive that dates parameters as date/datetime types if you can.

I believe you are looking for this?
You where missing the WHERE clause and I corrected a typo for your first column selected.
ALTER PROCEDURE [dbo].[select_User_attendance_master_Date]
(
#From_date NVarchar(100),
#To_date NVarchar(100)
)
AS
BEGIN
SELECT
Employee_Attendance_Code,
Employee_Attendance.Attendance_day as Attendanceday
FROM Employee_Attendance
WHERE Employee_Attendance.Attendance_day BETWEEN #From_date AND #To_date
END

Related

Temporarily change format of a date column in SQL Query output MM/YY -> MM/DD/YYYY

I have data on my SQL Server that is formatted like so:
06/21
MM/YY
I would like to manipulate it so that all the dates are displayed as MM/DD/YYYY. So as the previous example it would look like:
06/01/2021
I was wondering if there is some sort of function that I can run in my query to display the date column differently in my output? I don't want to actually alter the way in which the Column is formatted on the back end, I just want to change how it is presented to the user in the results of a single query.
Thank you for your help!
Using STUFF
declare #var varchar(64) = '06/21 '
select stuff(#var,4,0,'01/20')
So for your table.
select stuff(yourColumn,4,0,'01/20')
From yourTable
Just replace / with 01/20:
declare #yourDate varchar(10) = '06/21'
select replace(#yourDate ,'/','/01/20')
Result:
But it is probably wiser to cast it to a proper date type:
declare #yourDate varchar(10) = '06/21'
select cast(replace(#yourDate ,'/','/01/20') as date)
you can use this solution
it works with all years 1998, .. 2000,2001 ...
declare #theDate varchar(10) = '06/21'
select convert(date,'01/'+ #theDate ,3)

Converting SQL dates to different formats

I have a task which should be easy, just converting dates in to a specfic format
2015-11-16T20:34:19+08:00
(yyyy-mm-ddThh:mm:ss+[timezone offset]) which I later export to an Excel template that requires this type for format.
Looking at the database table where all the data is stored I noticed the column where the dates are stored under is of Varchar(20) datatype. As far as I know, it's a bad thing to save dates like that.
So basically what I need is to convert the following:
SELECT TIMESTAMP AS LASTCHANGEDATE FROM TABLE1
To a yyyy-mm-ddThh:mm:ss+[timezone offset] format, but TIMESTAMP has the datatype of varchar(20)
Anyone can help with this?
EDIT
The dates are stored atm like this 23.12.2015 17:08:18
In SQL Server it is something like this:
EDIT: Try it like this:
DECLARE #dtString VARCHAR(100) = '23.12.2015 17:08:18';
DECLARE #dt DATETIME = CONVERT(DATETIME, #dtString, 104);
SELECT CONVERT(VARCHAR(100),#dt,126)+'+08:00';
The reason why I tried the direct cast was your "The dates are stored atm like this". I thought, if the occur in different formats it might be better not to specify it...
Old Code:
DECLARE #dtString VARCHAR(100) = '23.12.2015 17:08:18';
DECLARE #dt DATETIME = CAST(#dtString AS DATETIME);
SELECT CONVERT(VARCHAR(100),#dt,126)+'+08:00';
EDIT: the third parameter of CONVERT is 126. This will create a ISO8601 compliant date equivalent
The result:
2015-12-23T17:08:18+08:00
EDIT: According to your comment you might implement this like here.
DECLARE #tbl TABLE(TimeStamp VARCHAR(100),item INT);
INSERT INTO #tbl VALUES
('23.12.2015 17:08:18',1123)
,('23.12.2015 19:08:18',1123)
,('24.12.2015 17:08:18',1123)
,('22.12.2015 19:08:18',3233)
SELECT item, CONVERT(VARCHAR(100),CONVERT(DATETIME,TimeStamp,104),126)+'08:00' AS ConvertedDate
FROM #tbl
WHERE item IN (1123,3233,2342);

How do I properly SELECT WHERE Effective_Date >= 'Given_Date' in a stored procedure?

I have this select statement that returns the results I'm looking for:
SELECT *
FROM Database.dbo.Table
WHERE Effective_Date >= '04/01/2014'
AND Chain = 'MCD'
I'm looking to turn this into a stored procedure with the following variables, #EffectiveDate and #Chain so that I can simply replace the date and chain to get different results. Here is the stored procedure I've made that doesn't work correctly:
CREATE PROCEDURE Database.dbo.StoredProc
#Chain VARCHAR(255),
#EffectiveDate VARCHAR(255)
AS
SELECT *
FROM Database.dbo.Table
WHERE Effective_Date >= '+#EffectiveDate+'
AND Chain = '+#Chain+'
GO
I'd like to execute this stored procedure like this:
EXEC Database.dbo.StoredProc
#PharmacyChain = N'MCD',
#EffectiveDate = N'04/01/2014'
;
GO
In this example, Table.Effective_Date is in datetime format. When I run the SELECT statement w/o the stored proc, the date comparison works fine to only select records with effective date after '04/01/2014'. However, when it's run using the variables int he stored proc, it doesn't convert the date correctly to compare. I've tried changing the EffectiveDate variable to datetime format, but still had no luck. Any help would be greatly appreciated. Thank you
Parameters should match the column datatype
#Chain VARCHAR(255) -- what is Chain?
#EffectiveDate datetime -- or date etc
And simply do this
SELECT *
FROM dbo.Table
WHERE Effective_Date >= #EffectiveDate
AND Chain = #Chain;
You don't need 3 part object names either

How to compare smalldatetime in stored procedure

I'm writing stored procedure to compare dates but it's not working properly. How can I make it so it compares only the dates but not the time? What I'm trying to do is compare the times and if the Id is null than insert a new entry with the same name but new time. I'm keeping multiple entries with same name but different test time.
ALTER PROCEDURE [dbo].[UL_TestData]
(
#Name varchar(30),
#Test_Time smalldatetime,
#ID INT output
)
AS
Declare #UpdateTime smalldatetime
SELECT #ID=ID FROM Info_User WHERE Name=#Name AND UpdateTime= #Test_Time
IF(#ID IS NULL)
BEGIN
INSERT INTO Info_User (Name, UpdateTime) VALUES (#Name, #UpdateTime)
END
there are a lot of solutions to this depending on what type of DBMS, however here is one:
SELECT #ID=ID FROM Info_User WHERE Name=#Name AND floor(cast(#UpdateTime as float))= floor(cast(#Test_Time as float))
this works because smalldatetime's date is stored a whole numbers, where the time is stored as decimals.
I would cast the dates to a plain date which makes this solution independent of implementation details
select #ID=ID
from info_user
where Name = #Name
and cast (UpdateTime as Date) = Cast(#TestTime as Date)
However, I would either add the date part of the UpdateTime as an additional (calculated) column or split the information into a date and a time part. This makes it much easier to query entries by the plain date.
As a rule of thumb: The type of columns (in general: the table layout) greatly depends on the type of query you usually run against your data.
Edit: As attila pointed out, the date datatype only exists in version 2008 and up

Convert Parameter Value Form DateTime To Int32

I am having problems with a Stored Procedure I am writing.
I am gathering data from a number of tables, most of which hold a date value, but one holds just a month(int)
I declare the following Parameters at the beginning of my SP.
#FromDate DateTime,
#ToDate DateTime
This works fine for most of my tables, but for the table where I am just requiring the Month from the #FromDate, I run into the following error:
"Failed to convert parameter value form a DateTime to a Int32."
Here is my Select statement for the Problem Table:
SELECT Branch, Discount
FROM MonthlyPromotions
WHERE (Month = DATEPART(mm,#FromDate))
Also, in the MonthlyPromotions Table the Month Field is an Int.
Can anyone help on this ASAP??
Thankyou
To troubleshoot your problem, can you do a
PRINT CAST(CAST(DATEPART(mm, #FromDate) AS INT) AS VARCHAR(50))
RETURN
At the beginning of your SP?
If that doesn't give an error, you can proceed to:
SELECT Branch, Discount
FROM MonthlyPromotions
WHERE Month = CAST(DATEPART(mm,#FromDate) AS INT)
select cast(getdate() as int)