Related
What is the most efficient way to create a dynamic sp where im checking the param NAME either its null or has value.
my GOAL here is to select the specific name and their datas else just display all name with datas if param NAME is null or has no value submitted.
Here's my 2 solution:
option 1
IF (SELECT call_transactions.`called_name` IS NULL FROM call_transactions) THEN
SELECT a.`called_name` , DATE_FORMAT(a.`start_datetime`,"%m/%d/%Y %T") AS start_datetime , DATE_FORMAT(a.`end_datetime`,"%m/%d/%Y %T") AS end_datetime, SEC_TO_TIME(a.`duration`) AS duration
FROM call_transactions a
WHERE a.`user_id` = pUSERID AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
ORDER BY a.start_datetime DESC;
ELSE
SELECT a.`called_name` , DATE_FORMAT(a.`start_datetime`,"%m/%d/%Y %T") AS start_datetime , DATE_FORMAT(a.`end_datetime`,"%m/%d/%Y %T") AS end_datetime, SEC_TO_TIME(a.`duration`) AS duration
FROM call_transactions a
WHERE a.`user_id` = pUSERID AND a.called_name = pNAME AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
ORDER BY a.start_datetime DESC;
END IF;
option 2
SELECT a.`called_name` , DATE_FORMAT(a.`start_datetime`,"%m/%d/%Y %T") AS start_datetime , DATE_FORMAT(a.`end_datetime`,"%m/%d/%Y %T") AS end_datetime, SEC_TO_TIME(a.`duration`) AS duration
FROM call_transactions a
WHERE a.`user_id` = pUSERID AND (a.called_name = pNAME OR pNAME = '') AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
ORDER BY a.start_datetime DESC;
Thank you in advance for the enlightenment guys. Just a curious kid here
Use your 2nd option, but test for the correct value.
WHERE a.`user_id` = pUSERID
AND (pNAME IS NULL OR a.called_name = pNAME)
AND a.duration <> 0 AND DATE_FORMAT(a.start_datetime , "%m/%d/%Y") BETWEEN pSTART AND pEND
In the second condition, note that I reversed the terms.
This is not strictly necessary, because the optimizer should prune away whichever condition is unnecessary, but this makes your short-circuit intentions clear.
Note also that NULL is not equivalent to empty string, and nothing is equal to NULL, so IS NULL should be used if NULL is what you are matching against.
I don't know what you're doing with DATE_FORMAT() here, but don't do that.
Use correct datetimes everywhere, or use STR_TO_DATE() to convert the parameters, not DATE_FORMAT() against the column. You can't compare mm/dd/yyyy "between" two values. That doesn't make sense. 12/09/2010 is "between" 12/08/2017 and 12/10/2017 because your code is comparing strings, lexcally, not comparing dates.
As a rule, you should never use a column as an argument to function in WHERE because this defeats any indexes on the column and forces a full scan.
i want to use a if condition in a sql query according to following need.
"if a year field is null then do not calculate age and if it is set then it have to execute.
here is my query.where is the problem?please consider this scenario
'if month and date is there like for example 0000-03-12'
SELECT id, name, birth_date, birth_time,
city_native, country_native, sex,
city_current, country_current, image,
if(YEAR(birth_date)='','',YEAR(CURDATE()) - YEAR(birth_date) -
(RIGHT(CURDATE(),5) < RIGHT(birth_date,5)),'') AS age
FROM birth_user u
WHERE <condition>;
You can use IFNULL() to check birth_date is NULL
CASE WHEN IFNULL(birth_date,0)=0 THEN '' ELSE YEAR(CURDATE()) - YEAR(birth_date) END as age
If your need is "if it is null", why are you comparing it to zero?
if(birth_date is null, 0, YEAR(CURDATE()) - ......) AS age
Where that 0 there is a suitable "default age".
Use NULL instead of 0000
SELECT id, name, birth_date, birth_time,
city_native, country_native, sex,
city_current, country_current, image,
if(birth_date is null,'',YEAR(CURDATE()) - YEAR(birth_date) -
(RIGHT(CURDATE(),5) < RIGHT(birth_date,5)),'') AS age
FROM birth_user u
WHERE " . implode(' AND ', $where);
I have 1 query as given below where a parameter with name "ListOfDate" which is coming by front end in form of string..i have "ENCOUNTERDATE" in mysql with datetime datatype..
SELECT d.encounterdate,
d.hospitalid,
amiop1,
amiop2,
amiop3,
amiop4,
amiop5,
amiop16
FROM factopami f,
dimpatientencounter d
WHERE d.hospitalid = 987654
AND d.measurecategory = 'AMI'
AND ( encounterdate IN ( ** listofdate ** )
OR encounterdate IS NULL )
AND d.patientid = f.patientid
AND d.id = f.patientencounterid
ORDER BY encounterdate;
after it i tried to convert ListOfDate into date:
SELECT d.encounterdate, d.hospitalid, amiop1, amiop2, amiop3, amiop4,
amiop5, amiop16
FROM factopami f, dimpatientencounter d
WHERE d.hospitalid = 987654
AND d.measurecategory = 'AMI'
AND ( encounterdate IN (str_to_date ('01-10-2012' '%m-%d-%Y'))
OR encounterdate IS NULL
)
AND d.patientid = f.patientid
AND d.ID = f.patientencounterid
ORDER BY encounterdate;
which is working for single string ...but i got list of string which is giving me error..
SELECT d.encounterdate,
d.hospitalid,
amiop1,
amiop2,
amiop3,
amiop4,
amiop5,
amiop16
FROM factopami f,
dimpatientencounter d
WHERE d.hospitalid = 987654
AND d.measurecategory = 'AMI'
AND ( encounterdate IN ( Str_to_date('01-10-2012', '01-10-2012',
'%m-%d-%Y') )
OR encounterdate IS NULL )
AND d.patientid = f.patientid
AND d.id = f.patientencounterid
ORDER BY encounterdate;
LIMIT 0, 1000 Error Code: 1582. Incorrect parameter count in the call to native function 'STR_TO_DATE' 0.000 sec
so how can i convert it to on mysql level
In short - at the moment you're converting the list to:
(encounterdate IN ( STR_TO_DATE('01-10-2012','01-10-2012', '%m-%d-%Y'))
Instead, you need to convert it to:
( encounterdate IN ( STR_TO_DATE('01-10-2012', '%m-%d-%Y')
, STR_TO_DATE('01-10-2012', '%m-%d-%Y') )
As more of an explanation, the IN operator is expecting a list of things:
encounterdate IN ( thing1, thing2, thing3 )
The thing you are passing is a date, meaning you're doing this:
encounterdate IN ( date1, date2, date3 )
Alas, you have strings instead of dates, and so you need to convert each of those strings into dates. Thus you need to some conversions.
date1 needs to be STR_TO_DATE( string1, '%m-%d-%y )
date2 needs to be STR_TO_DATE( string2, '%m-%d-%y )
date3 needs to be STR_TO_DATE( string3, '%m-%d-%y )
Putting that all together, you need to generate:
encounterdate IN ( STR_TO_DATE('01-10-2012', '%m-%d-%Y')
, STR_TO_DATE('01-10-2012', '%m-%d-%Y')
, STR_TO_DATE('01-10-2012', '%m-%d-%Y')
As an alternative, if you feel it's not possible to produce this, you could flip the conversion round and instead convert the encounterdate column to a string using DATE_FORMAT as you do the comparison.
Documentation here:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
http://www.w3schools.com/sql/func_date_format.asp
This would result in:
DATE_FORMAT( encounterdate, '%m-%d-%Y' ) IN ( '01-10-2012'
, '01-10-2012'
, '01-10-2012' )
However, you should bear in mind that this will have an impact on the indexing of encounterdate, though this might not be a problem in your use-case.
As it is explained in mysql manual, str_to_date function , get only to parameters. The first parameter is your date string, and the second one is the format of your date string.so you cann't use it like this:
STR_TO_DATE('01-10-2012','01-10-2012', '%m-%d-%Y')
You should use this:
STR_TO_DATE('01-10-2012', '%m-%d-%Y'),STR_TO_DATE('01-10-2012', '%m-%d-%Y')
I am using CONVERT(data_type(length),expression,style) function to change the format of a date in a SELECT query.
Declare #dt nvarchar(20)
Select #dt = Convert(nvarchar(20), SalesDate, 113) FROM SalesTable
The format I need is 'dd-MMM-yyyy' (eg. '05-Jul-2013') but I could not find the proper style number (eg. 113) for this particular format. Can any one help me with that please?
Try this:
Declare #dt NVARCHAR(20)
Select
#dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ')
FROM SalesTable
select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]
or else
select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')
both works fine
It doesn't look like DD-MMM-YYYY is supported by default (at least, with dash as separator). However, using the AS clause, you should be able to do something like:
SELECT CONVERT(VARCHAR(11), SYSDATETIME(), 106) AS [DD-MON-YYYY]
See here: http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
I Think this is the best way to do it.
REPLACE(CONVERT(NVARCHAR,CAST(WeekEnding AS DATETIME), 106), ' ', '-')
Because you do not have to use varchar(11) or varchar(10) that can make problem in future.
SELECT Convert(varchar(10),CONVERT(date,'columnname',105),105) as "end";
OR
SELECT CONVERT(VARCHAR(10), CAST(event_enddate AS DATE), 105) AS [end];
will return the particular date in the format of 'dd-mm-yyyy'
The result would be like this..
04-07-2016
select convert(varchar(11), transfer_date, 106)
got me my desired result of date formatted as 07 Mar 2018
My column transfer_date is a datetime type column and I am using SQL Server 2017 on azure
select FORMAT(getdate(), 'dd-MMM-yyyy') as DateToday
Other answers here seem to return columns that are quite wide.
This answer returns a varchar(11) which is all that is required for a date in dd-Mon-yyyy format.
'SalesDate' = CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),SalesDate, 106), ' ', '-')),
Try also:
select CONVERT(VARCHAR(11),REPLACE(CONVERT(VARCHAR(11),getdate(), 106), ' ', '-'))
As of today, this gives a result as per: 22-Jul-2021
I have a mysql table of 1.5 million record in a temproary table that I want to insert it into the main table with an inner join.
My code works but it stops at 103,613 records. IT does not keep going. Why would it stops? why it is not going all the way to the end?
This is my current code
INSERT INTO phone_calls(next_attempt, created_on, modified_on, status, call_subject,
account_number, call_code, last_attempt_on, total_attempts, account_id
,team_id
,campaign_id
,call_code_id
,result_code
,result_code_id
,time_zone_id
,trigger_on
,first_attempt_on
,first_attempt_by
,last_attempt_by
,modified_by
,client_id
,last_call_id
,call_notes
,owner_id
,industry_id
)
SELECT
CASE WHEN next_attempt IS NULL OR next_attempt = '' THEN STR_TO_DATE(replace(t.next_attempt,'/',','),'%m,%d,%Y %T') END as next_attempt,
CASE WHEN t.created_on IS NULL OR t.created_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.created_on,'/',','),'%m,%d,%Y %T') END as created_on,
CASE WHEN t.modified_on IS NULL OR t.modified_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.modified_on,'/',','),'%m,%d,%Y %T') END AS modified_on,
CONVERT( CASE WHEN t.status IS NULL OR t.status = '' THEN 0 ELSE t.status END, UNSIGNED INTEGER) AS status,
LEFT(IFNULL(t.call_subject, ''), 100),
t.account_number,
CONVERT( CASE WHEN t.callcode IS NULL OR t.callcode = '' THEN 0 ELSE t.callcode END , UNSIGNED INTEGER) AS callcode, STR_TO_DATE(replace(t.last_attempt_on,'/',','),'%m,%d,%Y %T') as last_attempt_on,
CONVERT( CASE WHEN t.New_Attempts IS NULL OR t.New_Attempts = '' THEN 0 ELSE t.New_Attempts END , UNSIGNED INTEGER) AS New_Attempts,
a.account_id
,0
,0
,0
,0
,0
,0
, '0000-00-00 00:00:00'
, '0000-00-00 00:00:00'
,1
,1
,1
,1
,0
,'IMPORTED FROM CRM'
,1
,1
FROM tmp_table_for_rdi_cms AS t
INNER JOIN accounts AS a ON a.account_number = t.account_number LIMIT 9999999999;
these 2 fields are indexed so it runs fast
a.account_number
t.account_number
Now, I know i am inserting no value for some fields that has a type if unsigned integer but that is okay as i will be updating this at later time.
How can i execute this INSERT INTO query without losing any record?
The "doesnt have a default value" problem is that the source table has nulls for columns on the new table that are defined S NOT NULL and which are not defined with a default value.
You have three choices to fix this:
Define the target columns as null able (leave out the NOT NULL)
Define the target columns with a default value, ie NOT NULL DEFAULT 'some value'
Provide a value if the column is null, ie SELECT ifnull(source_column, 'some value)
The Truncated incorrect INTEGER value problems and you are selecting from a char value and putting it in an int column, but some vyes are blank, so you need to handle that:
select if(source_column = '', 0, source_column)
The Data truncated for column problem is there because the source value is larger/longer than the target column can accommodate. To fix, define your target column to be larger (bigint instead of int) or longer (eg text or varchar(80) instead of varchar(20))